简体   繁体   English

在WPF库上应用Aero风格

[英]Apply Aero style on WPF library

I want to force the Aero style on my forms. 我想在我的表单上使用Aero风格。 I created a "WPF Class Library" and added a form and controls to this form. 我创建了“ WPF类库”,并向该表单添加了表单和控件。 As the library will be called by 3rd party C# applications running on different OS, I want to force to always apply the Aero style. 由于该库将由运行在不同操作系统上的第三方C#应用程序调用,因此我想强制始终应用Aero样式。

But as it is a WPF class library I have no App.xaml file where I could put my Resource Dictionary. 但是因为它是WPF类库,所以我没有App.xaml文件,可以在其中放置资源字典。 I placed it in a dedicated custom Styles.xaml therefore and in my form I reference it like 我将其放置在专用的自定义Styles.xaml因此在我的表单中我将其引用为

<Window.Resources>
    <ResourceDictionary Source="Styles.xaml">
    </ResourceDictionary>
</Window.Resources>

where Styles.xaml looks like Styles.xaml看起来像

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyLib">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/PresentationFramework.Aero;V4.0.0.0;component/themes/Aero.NormalColor.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>

and is set to " Build action: Embedded resource " and is stored in the main project folder. 并设置为“ Build action: Embedded resource ”,并存储在主项目文件夹中。 I also added a reference to PresentationFramework.Aero in my project. 我还在项目中添加了对PresentationFramework.Aero的引用。

When I call my library form from the C# application, I get an error saying 当我从C#应用程序调用库表单时,出现错误提示

Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' 设置属性“ System.Windows.ResourceDictionary.Source”引发了异常。 Line number 'x' and line position 'y' 行号“ x”和行位置“ y”

What did I do wrong? 我做错了什么?

This should work for you. 这应该为您工作。

<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />

Refere this MSDN article for more details. 有关更多详细信息,请参阅此MSDN文章。

UPDATE UPDATE

Include ResourceDictionary in PCL 在PCL中包括ResourceDictionary

Create a ResourceDictionary 创建一个ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>

  <!-- store here your styles -->

</ResourceDictionary>

You can use it from Your PCL in WPF App 您可以在WPF应用程序的PCL中使用它

<Window x:Class="Test.Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window"
        Height="300"
        Width="300">

  <Window.Resources>
    <ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/YourResDictionaryFolder/Dictionary1.xaml" />
  </Window.Resources>

  <Grid>

  </Grid>
</Window>

Your.Base.AssemblyName = Dll name Your.Base.AssemblyName = DLL名称

YourResDictionaryFolder = Folder where you created your ResourceDictionary YourResDictionaryFolder =您在其中创建ResourceDictionary的文件夹

Dictionary1.xaml = File name which you created above Dictionary1.xaml =您在上面创建的文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM