Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linked Content doesn't work for resource Dictionary

So in our Styles.xaml doc we use an external xml to provide the colors for the styles. We thought this would provide a good level of abstractions to allow people who weren't coders to modify the look of our app.

It works something this:

<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>

And this works and all is well, but we needed to add a couple of helper apps that would reuse the style of the main app.

So I was referencing the main app so that I can get to all the code goo, and then also reuse the styles. But when I do this I get an IOException "Cannot locate resource 'config/brandinfo.xml'.". I double checked that the brandinfo.xml is being copied over since the mainApp is refrenced, so I was puzzled.

The next thing I thought I'd try is using it as a linked file, so I created a config folder and added as link, set it to content copy always. Which creates this code in my csproj file

<ItemGroup>
 <Content Include="..\MainAppUI\Config\BrandInfo.xml">
  <Link>Config\BrandInfo.xml</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

Still doesn't work, so finially I added it as a file. Which creates this code in my csproj file.

<ItemGroup>
 <Content Include="Config\BrandInfo.xml">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

So this works, but now I have two copies of my BrandInfo file, which could be a problem for matienance going forward.

like image 838
Joel Barsotti Avatar asked Dec 01 '25 15:12

Joel Barsotti


1 Answers

I had the same problem. Luckily, this had a clue in it - the linked file is in the root.

So, lets say I have the following structure: MyAssembly -> MyFolder -> MyDictionary.xaml

To load the dictionary form App.xaml do:

  1. if MyDictionary.xaml is a "real" file -

    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyFolder/MyDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    
  2. if MyDictionary.xaml is a linked file -

    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

(NOTE: MyFolder is ommited)

Warning: I tested the code on a machine not connected to the internet and the snippets are written in Notepad++ so spelling or syntax may be off.

like image 117
XAMeLi Avatar answered Dec 03 '25 13:12

XAMeLi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!