2014-03-29 21 views
2

我想覆蓋我的應用程序中720和1080p的樣式。現在,我在頁面的.xaml文件中聲明resourcedictionaries:如何在Windows Phone 8中覆蓋不同分辨率的樣式?

<phone:PhoneApplicationPage.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="../Styles/MapViewStyles.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</phone:PhoneApplicationPage.Resources> 

我要重新定義頁面的構造樣式是這樣的:

public MapView() 
{ 
    InitializeComponent(); 

    if (ScreenSizeHelper.IsHd) 
    { 
     Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("/App;component/Styles/MapViewStyles.Hd.xaml", UriKind.Relative)}); 
    } 
} 

我在調試檢查,我的字典中加入MergedDictionaries,但樣式沒有覆蓋。我如何刷新MergedDictionaries或重新初始化頁面?

回答

0

過了一段時間我想出了一些有趣的解決方案。我創建了一個特殊的類名爲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。

相關問題