Stevens Miller
Stevens Miller

Reputation: 1480

Moving merged XAML ResourceDictionaries to Generic.xaml causes error

I have created two custom controls, both inherited from System.Windows.Controls.Button. One is called XLButton and the other is XLBox. They have identical XAML styles/templates in two separate ResourceDictionary objects in two separate .xaml files, and identical code-behind files, except that "XLButton" appears in the XLButton files where "XLBox" appears in the XLBox files, and vice versa.

I have created a simple test window with a two-row Grid. I merge the the ResourceDictionary files into a the Window.Resources of that test window and create an instance of each custom control, one in the top row, one in the bottom. This works fine. Here's the test window's XAML:

<Window x:Class="ScratchPadWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExLuminaControls"
        mc:Ignorable="d"
        Title="ScratchPadWindow" Height="118" Width="145">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/XLBox.xaml" />
                <ResourceDictionary Source="/Styles/XLButton.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <local:XLBox Content="Hi!"/>
        <local:XLButton Grid.Row="1" Content="Yeah!"/>
    </Grid>
</Window>

This works just fine. But, when I comment out the ResourceDictionary.MergedDictionaries section and copy it in its original form to Themes\Generic.xaml, so it looks like this:

<ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExLuminaControls"
        mc:Ignorable="d">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Styles/XLButton.xaml" />
        <ResourceDictionary Source="/Styles/XLBox.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I get a "Cannot locate resource 'styles/xlbutton.xaml' error, associated with this line:

<local:XLBox Content="Hi!"/>

That doesn't make sense to me, but what's more confusing is that the problem goes away if i click "Disable project code" in the designer.

I'm using Blend 2017 Community.

Can anyone help me understand this?

Thanks!

Upvotes: 1

Views: 787

Answers (1)

GCamel
GCamel

Reputation: 622

it's path problem, when in generic, use it like this

<ResourceDictionary Source="/AssemblyName;Component/Styles/XLButton.xaml" />

or have to go up with "../../"

  1. themes and generic lowercase
  2. assembly info
  3. app.xaml if nothing works

    [assembly: ThemeInfo( ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located //(used if a resource is not found in the page, // or application resource dictionaries) ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located //(used if a resource is not found in the page, // app, or any theme specific resource dictionaries) )]

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
    
            <ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
            ...
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

Definition and style

#region --------------------CONSTRUCTORS--------------------

    static WaitSpin()
    {
        FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(WaitSpin),
            new FrameworkPropertyMetadata(typeof(WaitSpin)));
    }

    /// <summary>
    /// LoadingAnimation constructor.
    /// </summary>
    public WaitSpin()
    {
        this.DefaultStyleKey = typeof(WaitSpin);
    }

    #endregion


<Style x:Key="{x:Type local:WaitSpin}" TargetType="{x:Type local:WaitSpin}">

Upvotes: 1

Related Questions