因此,在我们的 Styles.xaml 文档中,我们使用外部 xml 来提供样式的颜色。我们认为这将提供良好的抽象级别,允许非编码人员修改我们应用的外观。
它的作用是这样的:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CalManv4UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
>
<XmlDataProvider x:Key="BrandInfo" Source="/Config/BrandInfo.xml" XPath="BrandRoot" />
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxForeground}"/>
<Setter Property="Background" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxBackground}"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontFamily" Value="Verdana"/>
</Style>
</ResourceDictionary>
这一切正常,但我们需要添加几个可以重用主应用样式的辅助应用。
所以我引用了主应用程序,这样我就可以获取所有代码,然后也可以重用样式。但是当我这样做时,我得到一个 IOException“无法找到资源'config/brandinfo.xml'。”。我仔细检查了 brandinfo.xml 是否被复制,因为 mainApp 被引用了,所以我很困惑。
接下来我想我会尝试将它用作链接文件,所以我创建了一个配置文件夹并添加为链接,始终将其设置为内容副本。在我的 csproj 文件中创建此代码
<ItemGroup>
<Content Include="..\MainAppUI\Config\BrandInfo.xml">
<Link>Config\BrandInfo.xml</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
还是不行,所以最后我把它添加为一个文件。这会在我的 csproj 文件中创建此代码。
<ItemGroup>
<Content Include="Config\BrandInfo.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
所以这行得通,但现在我有两份 BrandInfo 文件,这可能会影响 future 的耐心。
最佳答案
我遇到了同样的问题。幸运的是,this有线索 - 链接文件在根目录中。
所以,假设我有以下结构: 我的程序集 -> 我的文件夹 -> MyDictionary.xaml
要从 App.xaml 加载字典,请执行以下操作:
如果 MyDictionary.xaml 是“真实”文件 -
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyFolder/MyDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
如果 MyDictionary.xaml 是链接文件 -
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
(注意:省略了 MyFolder)
警告:我在未连接到互联网的机器上测试了代码,并且代码片段是用 Notepad++ 编写的,因此拼写或语法可能不正确。
https://stackoverflow.com/questions/4730188/