1

But why. I see to be correct how the file works:

Error in IFocus.xaml but the converter is defined before. I don't understant what is wrong.

Referance: Modern.xaml Is a referance from another project. and i like this.

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

<SolidColorBrush x:Key="C_FocusBush" Color="Red"/>
<c:ThicknessConverter x:Key="ThicknessConverter"/>

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Interfaces/IFocus/IFocus.xaml"/>
</ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

The IFocus.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:i="clr-namespace:Modern.Interfaces">
<Style TargetType="{x:Type i:IFocus}" x:Key="{x:Type i:IFocus}">
    <Setter Property="BorderBrush" Value="{DynamicResource C_FocusBush}"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="Padding" Value="2"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type i:IFocus}">

                <Grid Margin="{TemplateBinding Padding}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessConverter}"/>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

</ResourceDictionary>

The main app to include all resources:

<Application x:Class="*.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Modern;component/Modern.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

</Application>

The brush works fine but the Converter not, why not?

  • You're using `StaticResource` to retrieve the converter, but `DynamicResource` to retrieve the brush. At a guess, that XAML is being parsed at a point in time before either resource has been added defined. `DynamicResource` doesn't mind; it sticks around and provides a value when the value becomes available (and remains on the scene, in case the value changes). `StaticResource` must provide a value right then, but it can't. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 13:42
  • `ResourceDictionary.MergedDictionaries` appears below those two resource definitions in the text of your XAML file, but I would hesitate to take as a guarantee that the definitions in the XAML are actually parsed in that order. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 13:46
  • How can i make that? Strange behavior. –  May 17 '19 at 13:49
  • If these resources are specific to that Style, put them in the same XAML file. If they're widely used "theme" resources, use `DynamicResource` to retrieve them. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 13:57

2 Answers2

4

I encountered an error format the same like this after I changed my Visual Studio theme to custom (mine's OneMonokai). I just reverted it back to the default theme and surprisingly it was resolved. It may seem far from the question but you could try this fix if you want.

Kelvin
  • 41
  • 5
1

Since it is the Style in IFocus.xaml that references a Brush resource in Modern.xaml, it's IFocus.xaml that should merge Modern.xaml and not the other way around:

Modern.xaml:

<ResourceDictionary ...>
    <SolidColorBrush x:Key="C_FocusBush" Color="Red"/>

</ResourceDictionary>

IFocus.xaml:

<ResourceDictionary ...>
    <Style ...>
        <Setter Property="BorderBrush" Value="{StaticResource C_FocusBush}"/>
    </Style>

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../../Modern.xaml"/>
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

App.xaml:

<ResourceDictionary Source="pack://application:,,,/Modern;component/Interfaces/IFocus/IFocus.xaml"/>

Alternatively, you can create a separate resource dictionary with all brushes and merge this one and the one with the styles into App.xaml or another resource dictonary.

You may also want to see my answer here for more information about the order in which resource dictionaries are loaded.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Is not another method to merge the xaml in the referance and to include only one xaml in the App ? –  May 17 '19 at 14:19
  • I am not sure I understand what you mean. You shouldn't define a style in `A` that references a resource in `B` without merging `B` into `A` because `A` does have a dependency upon `B`. If you don't want to merge, you should define the resource directly in `A`. – mm8 May 17 '19 at 14:22
  • Or define the brushes in a separate dictionary and merge them both into `App.xaml`. – mm8 May 17 '19 at 14:23
  • Like. A -> Merge: Brushes, IFocus, Etc..., B -> Merge A –  May 17 '19 at 14:27
  • 1
    If you merge `Brushes` and `IFocus` into `App.xaml`in that order, it should work. – mm8 May 17 '19 at 14:28
  • I make in A: Variables with brushes, etc and Components with all Styles. And i merge both in B and work fine. Thanks. You can edit the post like this. –  May 17 '19 at 14:40
  • @And-RoMarian: I added this to my answer. – mm8 May 17 '19 at 14:44