Skip to content

Commit 5434f22

Browse files
Yu Leng (from Dev Box)claude
andcommitted
[PowerDisplay] Use ResourceMap.TryGetValue for bullet-probe loop
Fix dialog never showing: MRT++ ResourceLoader.GetString throws "NamedResource Not Found" when the key is absent — not return empty string like the legacy WinRT loader. The Item{N} probe loop hit that the moment it walked past the last real bullet, the exception bubbled through ConfirmAndEnableModuleAsync's try/catch, the dialog never showed, and the ToggleSwitch snapped back to OFF. Replace the loop's GetString with ResourceMap.TryGetValue on a direct handle to the Settings PRI's "Resources" subtree — returns null for missing keys, no exception, no exception-driven control flow. Other GetString calls in the constructor stay (those keys must exist; a miss is a real bug worth surfacing). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9bf807d commit 5434f22

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/settings-ui/Settings.UI/SettingsXAML/Views/DangerousFeatureWarningDialog.xaml.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using Microsoft.PowerToys.Settings.UI.Helpers;
77
using Microsoft.UI.Xaml.Controls;
8+
using Microsoft.Windows.ApplicationModel.Resources;
89

910
namespace Microsoft.PowerToys.Settings.UI.Views
1011
{
@@ -14,19 +15,24 @@ namespace Microsoft.PowerToys.Settings.UI.Views
1415
/// resource key prefix; the dialog loads
1516
/// "{prefix}_WarningTitle/Header/Description/WarningList_Item{N}/Confirm".
1617
/// Bullets are prepended in code so translators only see the body text; the
17-
/// item loop reads <c>_WarningList_Item1</c>, <c>_Item2</c>, ... until a missing
18-
/// key returns empty, so adding a 4th bullet only requires a new resw entry.
18+
/// item loop probes <c>_WarningList_Item1</c>, <c>_Item2</c>, ... until a missing
19+
/// key is hit, so adding a 4th bullet only requires a new resw entry.
1920
/// </summary>
2021
public sealed partial class DangerousFeatureWarningDialog : ContentDialog
2122
{
2223
// Visual decorations are applied in code so translators only see body text.
2324
private const string WarningHeaderPrefix = "⚠️ ";
2425
private const string BulletPrefix = "• ";
2526

26-
// Hard cap on bullets in case a future ResourceLoader change ever returns a
27-
// non-empty value for a missing key; a real dialog never approaches this.
27+
// Hard cap on bullet probes; a real dialog never approaches this.
2828
private const int MaxBulletItems = 10;
2929

30+
// Direct ResourceMap handle so the bullet loop can probe for missing keys with
31+
// TryGetValue (returns null) instead of ResourceLoader.GetString (throws
32+
// "NamedResource Not Found").
33+
private static readonly ResourceMap ResourceMap =
34+
new ResourceManager("PowerToys.Settings.pri").MainResourceMap.GetSubtree("Resources");
35+
3036
public DangerousFeatureWarningDialog(string resourceKeyPrefix)
3137
{
3238
InitializeComponent();
@@ -39,19 +45,16 @@ public DangerousFeatureWarningDialog(string resourceKeyPrefix)
3945
PrimaryButtonText = loader.GetString("PowerDisplay_Dialog_Enable");
4046
CloseButtonText = loader.GetString("PowerDisplay_Dialog_Cancel");
4147

42-
// ResourceLoader.GetString returns string.Empty for missing keys (see
43-
// FriendlyDateHelper.cs for the same pattern), so the loop stops cleanly
44-
// at the first absent _Item{N}. The upper bound is a defensive cap.
4548
var items = new List<string>();
4649
for (int i = 1; i <= MaxBulletItems; i++)
4750
{
48-
var item = loader.GetString($"{resourceKeyPrefix}_WarningList_Item{i}");
49-
if (string.IsNullOrEmpty(item))
51+
var candidate = ResourceMap.TryGetValue($"{resourceKeyPrefix}_WarningList_Item{i}");
52+
if (candidate == null || string.IsNullOrEmpty(candidate.ValueAsString))
5053
{
5154
break;
5255
}
5356

54-
items.Add(BulletPrefix + item);
57+
items.Add(BulletPrefix + candidate.ValueAsString);
5558
}
5659

5760
WarningList.ItemsSource = items;

0 commit comments

Comments
 (0)