Wpf 资源的动态变化,Windows Phone

Wpf 资源的动态变化,Windows Phone,wpf,xaml,resources,windows-phone-8.1,Wpf,Xaml,Resources,Windows Phone 8.1,我正在运行时对资源进行一些更改,以便为不同(物理)屏幕大小的应用程序做好准备: var info = DisplayInformation.GetForCurrentView(); Size screenSize = new Size(Math.Round(Window.Current.Bounds.Width * info.RawPixelsPerViewPixel), Math.Round(Window.Current.Bounds.Height * info.RawPixelsPe

我正在运行时对资源进行一些更改,以便为不同(物理)屏幕大小的应用程序做好准备:

var info = DisplayInformation.GetForCurrentView();
Size screenSize = new Size(Math.Round(Window.Current.Bounds.Width * info.RawPixelsPerViewPixel),
    Math.Round(Window.Current.Bounds.Height * info.RawPixelsPerViewPixel));

Application.Current.Resources["SmallerText"] = (1.0 / 15.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["SmallText"] = (1.0 / 12.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["MediumText"] = (1.0 / 10.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["LargeText"] = (1.0 / 5.0) * screenSize.Height / info.RawDpiY * 72;
var displayMargin = (1.0 / 320.0) * screenSize.Height;
Application.Current.Resources["DisplayMargin"] = new Thickness(displayMargin);
现在,当我尝试使用修改过的字体大小时,它可以工作:


但当我尝试使用修改后的厚度时,应用程序崩溃:


错误消息相当神秘:

An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in MyApp.exe but was not handled in user code

WinRT information: Failed to assign to property '%0'. [Line: 17 Position: 50]

Additional information: The text associated with this error code could not be found.



Failed to assign to property '%0'. [Line: 17 Position: 50]

If there is a handler for this exception, the program may be safely continued.
为什么我不能使用修改后的厚度?如果我不从代码后面修改它,一切都正常工作


编辑:变通方法

解决办法#1:

解决办法#2:



你在哪里调用此代码?@ChubosaurusSoftware App.xaml.csI我已经尝试了你的代码,它在我的手机上似乎工作正常--你能检查一下吗?
var displayMargin = (1.0 / 320.0) * screenSize.Height;
Application.Current.Resources.Remove("DisplayMargin");
Application.Current.Resources.Add("DisplayMargin", new Thickness(displayMargin));
<Grid Grid.Row="0" Margin="{ThemeResource DisplayMargin}">