0

我有一个资源字典,其中为我的应用程序定义了样式和控件模板。现在我想定义更多资源字典来定位不同的屏幕分辨率,每个屏幕分辨率一个。如何检测客户端屏幕分辨率并在 App.XAML 中加载特定的资源字典?

我当前的 App.XAML:

<ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources\BlueYellow\BlueYellowTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
4

2 回答 2

1

我认为您可以制作 markupExtension,它根据分辨率返回适当的资源字典 uri,并像这样使用它:

<ResourceDictionary Source="{ThemeUriAccordingToCurrentResolution}" />
于 2012-06-01T12:09:09.287 回答
1

我可以看到能够根据屏幕分辨率切换资源的价值,您显然必须做一些工作才能将大量分辨率降低到可管理的短列表中,并找到最适合当前的分辨率,但是干得好。

应用程序.cs

protected override void OnStartup(StartupEventArgs e)
{
  // Get the width and height, you might want to at least round these to a few values.
  var width = System.Windows.SystemParameters.PrimaryScreenWidth;
  var height = System.Windows.SystemParameters.PrimaryScreenHeight;

  // make the resource path from them.
  string resourceName = string.Format("Themes\resource{0}x{1}", width, height);

  // Add the resource to the app.
  Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(resourceName, UriKind.Relative)));

  base.OnStartup(e);
}
于 2012-06-01T12:47:27.827 回答