過了一段時間我想出了一些有趣的解決方案。我創建了一個特殊的類名爲ResourceResolutionManager:
public class ResourceResolutionManager
{
public static DependencyProperty ResolutionProperty =
DependencyProperty.RegisterAttached("Resolution", typeof(Resolutions?), typeof(ResourceResolutionManager),
new PropertyMetadata(null, ResolutionChanged));
private static void ResolutionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null && (Resolutions)e.NewValue != ScreenExtension.CurrentResolution)
{
var dict = obj as ResourceDictionary;
if (dict != null)
{
dict.Clear();
}
}
}
public static void SetResolution(DependencyObject obj, Resolutions? resolution)
{
obj.SetValue(ResolutionProperty, resolution);
}
public static Resolutions? GetResolution(DependencyObject obj)
{
return (Resolutions?)obj.GetValue(ResolutionProperty);
}
}
我確定屏幕分辨率與我的助手類:
public enum Resolutions { Wvga, Wxga, Hd };
public class ScreenExtension
{
public static bool IsWvga
{
get
{
return Application.Current.Host.Content.ScaleFactor == 100;
}
}
public static bool IsWxga
{
get
{
return Application.Current.Host.Content.ScaleFactor == 160;
}
}
public static bool IsHd
{
get
{
return Application.Current.Host.Content.ScaleFactor == 150;
}
}
public static Resolutions CurrentResolution
{
get
{
if (IsWvga) return Resolutions.Wvga;
if (IsWxga) return Resolutions.Wxga;
if (IsHd) return Resolutions.Hd;
throw new InvalidOperationException("Unknown resolution");
}
}
}
,而且我用ResourceResolutionManager中的.xaml文件是這樣的:
<ResourceDictionary Source="Styles/Common.Hd.xaml">
<core:ResourceResolutionManager.Resolution>
<extensions1:Resolutions>Hd</extensions1:Resolutions>
</core:ResourceResolutionManager.Resolution>
</ResourceDictionary>
在ResourceResolutionManager我檢查ResourceDictionary中的解析是否匹配解析,如果沒有,我清除ResourceDictionary。