سؤال

I would like for the user of my WPF application to be able to choose between two styles: the default WPF style and my own custom style. I've made a Resource Dictionary which includes all the style elements of the custom style. The resource dictionary is located in the same project.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Practicum21_Ben_Vandenberk">

...

    <Style TargetType="Window">
    <Setter Property="Background" Value="Black"></Setter>
</Style>
<Style TargetType="{x:Type local:MainWindow}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:BestellingDetail}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:CategorieDetail}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:KlantDetail}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:LeverancierDetail}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:ProduktDetail}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:WerknemerDetail}" BasedOn="{StaticResource {x:Type Window}}"/>

When I try to load this dictionary to App.Current.Resources using the following code, I get an XAMLParseException: "creating a Type from the text local:MainWindow failed".

ResourceDictionary dic = new ResourceDictionary();
dic.Source = new Uri(path, UriKind.Absolute);
App.Current.Resources.MergedDictionaries.Clear();
App.Current.Resources.MergedDictionaries.Add(dic);

I've been searching for over two hours now.

WPF Loose XAML ResourceDictionary

On several threads people suggest to add "assembly=" to the xmlns at the top of the loose XAML. If I do this though, the XAML file itself won't build anymore.

Error: 'Cannot create unknown type '{clr-namespace:NameSpace.Properties}Settings'.'

Here people suggested to set the build action of the XAML to 'page' but this was already the case for my XAML file...

I'm pretty new to programming and my understanding is pretty limited, but I'm usually able to solve problems by browsing the web. Not this time though.

I figure it has to be something trivial which I can't find due to my lack of understanding. Or am I tackling my original problem (letting the user choose between 2 styles) in an altogether wrong fashion?

هل كانت مفيدة؟

المحلول

If you're going to be loading the Xaml file at runtime, you shouldn't need to compile it. Try changing the build action to 'Resource', then add the assembly= qualifiers to your namespace imports like the linked answer suggests.

Alternatively, build the Xaml file as 'Page', but give it a class name so you can instantiate it with a constructor:

<ResourceDictionary x:Class="Practicum21_Ben_Vandenberk.AlternateResources"
  ...>

App.Current.Resources.MergedDictionaries.Add(new AlternateResources());

If you go that route, you shouldn't need to add assembly= qualifiers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top