Load style library into WPF

1

Long Question:

I downloaded a style library (Selen.Wpf) via NuGet. Now I want to use its styles. Frankly, this little task drove me crazy by now.

First approach - Using MergedDictionarys:

Ideally I would add the styles as follows

<Application.Resources>
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary>
      <BitmapImage x:Key="ImgHelp" UriSource="pack://application:,,,/Resources/Help_32x.png"/>
      <!--Lots of our own stuff goes here too-->
    </ResourceDictionary>
    <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/Styles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

Unfortunaly this throws a runtime excpetion at startup (System.Windows.Markup.XamlParseException: "System.Windows.Media.Imaging.BitmapImage" IsFrozen must be set to false). This is not the fault of Selen.Wpf, but also happens if I move the declaration of my own Resources from <Application.Resources>...</Application.Resources> to <ResourceDictionary.MergedDictionarys>...</ResourceDictionary.MergedDictionarys>

The few other questions on SO regarding this problem had no answers, merly workarounds. With no idea how to debug this, I moved on...

Second approach - Using a Workaround:

If I get a XamlParseExpcetion doing it the proper way, then just do the ugly code behind hack, I thought.

I added the following function:

private void LoadSelen()
{
  ResourceDictionary selenDict = new ResourceDictionary()
  {
    Source = new Uri("pack://application:,,,/Selen.Wpf.SystemStyles;component/Styles.xaml")
  };

  App.Current.Resources.MergedDictionaries.Add(selenDict);
}

and called it between InitComponent and Run

[STAThread]
public static void Main()
{
  application.InitializeComponent();
  application.LoadSelen();
  application.Run();
}

But yet again this lead to a XamlParseExcpetion, but this time in DataPanel.xaml - a panel embedded into my MainWindow:

System.Windows.Markup.XamlParseException
HResult=0x80131501
Message = Specifying a value for "System.Windows.Markup.StaticResourceHolder" lead to an excpetion.
Source = PresentationFramework
Stacktrace:
 at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

Inner Excpetion 1:
FileLoadException: The File or Assembly "System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" or a dependency of it was not found. The found manifestdefinition of the assembly does not match the assembly reference (Excpetion of HRESULT: 0x80131040)

Sorry for poor formating, dont know how to do it better. Translated!

DataPanel.xaml where this Excpetion occurs:

<UserControl x:Class="GUI.DataPanel"
             x:Name="UserControlPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <DockPanel LastChildFill="True" Background="Transparent">
    <Border  DockPanel.Dock="Top" BorderBrush="DarkGray" BorderThickness="1">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Content="{Binding ElementName=UserControlPanel, Path=HeaderText}" Grid.Column="0" VerticalAlignment="Center"/>
        <ItemsControl ItemsSource="{Binding ElementName=UserControlPanel, Path=Items}" Grid.Column="1">
          <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft"/>
            </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
        </ItemsControl>
      </Grid>
    </Border>
    <TabControl x:Name="TabControl" ItemContainerStyle="{DynamicResource CollapseSingleTabItem}"/>
  </DockPanel>
</UserControl>

Short Question:

How can use the styles from Selen.Wpf. At this point I pretty much don't care how ugly the answer is. As long as it Compiles & Starts (Never thought I would say this outside the C++ world) I am happy.

c#
wpf
xaml
asked on Stack Overflow Jun 10, 2017 by Mohammed Li

1 Answer

3

It looks like that a reference to System.Windows.Interactivity coudn't be resolved.

FileLoadException: The File or Assembly "System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" or a dependency of it was not found. The found manifestdefinition of the assembly does not match the assembly reference (Excpetion of HRESULT: 0x80131040)

As it seems the Selen.Wpf nuget package needs this dependency but doesn't specifiy it in the packages description. (You can see that a reference in the Selen.Wpf.DemoApplication.csproj to it is added)

To resolve this problem you can add a reference to System.Windows.Interactivity by yourself (and use your first solution).

answered on Stack Overflow Jun 10, 2017 by NtFreX • edited Jun 10, 2017 by NtFreX

User contributions licensed under CC BY-SA 3.0