如何编写风格化但与操作系统无关的WPF控件。使用Microsoft.Windows.Themes

问题描述 投票:0回答:1

我正在寻找一篇文章,描述能够在不同版本的OS下工作的编写程式化控件的理论。Windows 10、8、7,经典主题。

我正在尝试在我的XAML代码中使用DataGridHeaderBorder类。

如果我这样写

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:theme="clr-namespace:Microsoft.Windows.Themes"
                    xmlns:local="clr-namespace:EhLib.WPF">

... 
          <Grid>
            <theme:DataGridHeaderBorder SortDirection="{TemplateBinding local:DataGridColumnHeaderCell.SortDirection}"
                                        IsHovered="{TemplateBinding UIElement.IsMouseOver}"

并将程序集PresentationFramework.Aero2.dll添加到“引用”部分,然后XAML引发错误。

标记'DataGridHeaderBorder'在XML名称空间'clr-namespace:Microsoft.Windows.Themes'中不存在。第193行的位置14. EhLib.WPF 1 C:... Lib.WPF \ Themes C:..... WPF \ Themes \ Generic.xaml 193 14如果我这样写:

 xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"

然后错误消失,但是我无法在Windows 7上运行该应用程序

在启动时,程序本身确定要加载和使用Aero,Aero2,Luna,Classic或Royale的资源组合。

问题:我还可以选择所需的程序集并将其替换为字符串吗?

  xmlns: theme = "clr-namespace: Microsoft.Windows.Themes; assembly =% PresentationFramework.XXX%"

最终应用程序的开发人员可以使用以下行覆盖样式装配:。

中的ResourceDictionary来源
<Application x:Class="EhLibTestApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:EhLibTestApp"
             StartupUri="MainForm.xaml">
    <Application.Resources>
      <ResourceDictionary Source="/PresentationFramework.Classic, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/classic.xaml" />
  </Application.Resources>
</Application>

如果他将使用我的控件,那么对于对DataGridHeaderBorder类的正确引用,该控件应引用所选的样式集。

如何考虑到操作系统版本和Application.Resources ResourceDictionary Source覆盖而正确地做到这一点?

我希望使用DataGridHeaderBorder的控件看起来与标准WFP控件相同。特别是DataGrid。

DataGridHeaderBorder使用的DataGrid标头以自己的方式在不同的操作系统和设置下看起来,并且与当前Windows主题相对应。如果我在WPF控件中使用DataGridHeaderBorder,则它看起来总是相同的,并且可能与Windows主题不符。

enter image description here

c# wpf windows-7 windows-10 themes
1个回答
0
投票

您可以在主题文件夹中为每个主题定义一个ResourceDictionary,只要定义了控件的程序集标记有ThemeInfoAttribute并具有docs中描述的所有其他附加特征:

[assembly:ThemeInfoAttribute(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

然后您的资源词典将被命名为:

  • themes \ luna.normalcolor.xaml
  • themes \ luna.homestead.xaml
  • themes \ luna.metallic.xaml
  • themes \ royale.normalcolor.xaml
  • themes \ aero.normalcolor.xaml
  • themes \ classic.xaml

如果包含控件样式的资源字典是在与控件类分开的程序集中定义的,则应将ResourceDictionaryLocationThemeInfoAttribute设置为ResourceDictionaryLocation.ExternalAssembly,并命名外部程序集<YourControlAssembly>.<ThemeName>.dll。这就是PresentationFramework.dll所做的。具有此属性的市场:

[assembly:ThemeInfo(ResourceDictionaryLocation.ExternalAssembly, ResourceDictionaryLocation.None)]

并且样式在PresentationFramework.Aero.dllPresentationFramework.Classic.dll等中定义

模板通过命名约定进行查找。除了docs,您还可以参考Ian Griffith's blog post以获得更多有关此信息。

© www.soinside.com 2019 - 2024. All rights reserved.