UWP MergedDictionary Style that references other style throws error

Related searches

Having real problems creating a Merged Dictionary of styles that need to use values from another MergedDictionary (Brushes.xaml).

When I try and reference it from another file I get the following error:

"Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: x Position: y]"

E.g. in a Resource Dictionary I have the following that works..

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="Button">
        <Setter Property="Background" Value="{StaticResource customBrush1}" />
        <Setter Property="Foreground" Value="{StaticResource customBrush2}" />

However, this isn't ideal as I'd need to reference Brushes.xaml in every style resource dictionary. So what I want to do is declare the Brushes.xaml in App.xaml but everything I try results in the error above. It basically does not recognise the resources defined in Brushes.xaml unless I add them to each individual style.

E.g. What I want (to be able to do in App.xaml)...

<Application x:Class="Test.UWP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ms-appx:///Test.Xamarin.Forms.Themes.UWP/Brushes.xaml" />
                <ResourceDictionary Source="ms-appx:///Test.Xamarin.Forms.Themes.UWP/CustomStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

CustomStyles.xaml contains

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles\Button\BackButton.xaml" />
        <ResourceDictionary Source="Styles\Button\Default.xaml" />
        <ResourceDictionary Source="Styles\Button\HyperlinkBasicButton.xaml" />
        <ResourceDictionary Source="Styles\Button\HyperlinkButton.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

And e.g. Styles\Button\Default.xaml contains..

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Background" Value="{StaticResource CustomBrush1}" />
        <Setter Property="Foreground" Value="{StaticResource CustomBrush2}" />

Where CustomBrush1 is defined in Brushes.xaml.

I've tried so many things e.g. Tried adding Brushes.xaml to the CustomStyles.xaml - doesn't work. Tried changing the order as this states they should be in inverse order - this doesn't work.

Any help is much appreciated, thanks

As per comment, here is the Brushes.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="CustomBrush1"
                     Color="Black}" />
    <SolidColorBrush x:Key="CustomBrush2"
                     Color="White}" />
</ResourceDictionary>

From your description, this behavior is by-design, derive this document we could find.

The lookup sequence then checks the next parent object in the runtime object tree of the app....

If you place Brushes.xaml at patent level, the sub level Default.xaml can not get the brush resource and throw exception. The best practice is refer Brushes.xaml in each resource like the first scenario mentioned above.

And if Brushes.xaml and Default.xaml are not in the same folder , we need use ms-appx:// scheme to refer it .

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp.Styles.Button"
    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ms-appx:///Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="Button">
        <Setter Property="Background" Value="{StaticResource CustomBrush1}" />
        <Setter Property="Foreground" Value="{StaticResource CustomBrush2}" />
    </Style>  
</ResourceDictionary>

Then add above to CustomStyles.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp"
    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles/Button/Default.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

At last, we just need to add CustomStyles into MergedDictionaries within App.xaml file

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="CustomStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

ResourceDictionary and XAML resource references, However, the XAML framework also looks for implicit style resources (those which use You access members of the resource dictionary like any other dictionary. MergedDictionaries> <x:String x:Key="greeting">Hello world</x: String> If all possible resource lookup attempts fail, an error often results. A named style must be explicitly applied to specific controls; a general style is applied to any control that matches the specified TargetType. In this example, the first style has an x:Key attribute and its target type is Button. The first button's Style property is set to this key, so this style is a named style and must be applied explicitly.

I found a solution to this problem. The problem being that one resource dictionary cannot reference anything in another if they are both declared in App.Xaml (unless the value is in the ControlTemplate - see below)

The solution I have found is to remove all resource dictionaries from App.Xaml, so App.Xaml basically looks like this:

<Application x:Class="Dwp.CRMobile.UWP.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             RequestedTheme="Light">
</Application>

And then instead add them to the OnLaunched method of App.Xaml.cs as below. Note you will also need to add this to OnActivated if the app could also be run by PowerShell etc.

protected override void OnActivated(IActivatedEventArgs e)
{
    var eventArgs = e as ProtocolActivatedEventArgs;
    var data = new ActivationArgs(e.Kind, eventArgs.Uri.Query);

    AddResources();

    Run(e, data);
}

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if RELEASE
    var data = new ActivationArgs(e.Kind, e.Arguments);
#else
    var data = new ActivationArgs(ActivationKind.Protocol, e.Arguments);
#endif

    AddResources();

    Run(e, data);
}

private static void AddResources()
{
    var applicationMergedDictionaries = Application.Current.Resources.MergedDictionaries;
    applicationMergedDictionaries.Add(GetResourceDictionary("ms-appx:///Test.Xamarin.Forms.Themes.UWP/Brushes.xaml"));
    applicationMergedDictionaries.Add(GetResourceDictionary("ms-appx:///Test.Xamarin.Forms.Themes.UWP/CustomStyles.xaml"));
    applicationMergedDictionaries.Add(GetResourceDictionary("ms-appx:///Test.HTMobile.Core.UWP/Styles.xaml"));
    applicationMergedDictionaries.Add(GetResourceDictionary("ms-appx:///Test.CRFramework.Xamarin.Forms.UWP/Root.xaml"));
}

It's an odd one - something to do with when resources are loaded. Because if I stick to the original method in App.xaml and use a value from Brushes.xaml in a style defined in CustomStyles.xaml it will work if the value is set in a ControlTemplate, but it will not if it is in a standard Setter Property. This method above solved this problem for me, so hopefully it will help someone else.

ResourceDictionary Class (Windows.UI.Xaml), Defines a repository for XAML resources, such as styles, that your app uses. references the Style in the resource dictionary to provide a value for the Style property For info on how merged dictionary resources are resolved, see the " Merged into a ResourceDictionary, this throws a XAML parse exception or a Windows� These are just a few of the goodies that come with UWP. To learn more, see Build great experiences with Windows. Add a UWP project. First, add a UWP project to your solution. Then, from your UWP project, add a reference the .NET Standard 2.0 library project. Build your pages. Add XAML pages and call the code in your .NET Standard 2.0 library.

How do I reference one XAML MergedDictionary from another , Resources> <ResourceDictionary Source="Styles/Colours.xaml"/> < ResourceDictionary Source="Styles/Text.xaml"/> </Application. files are in a Styles/ subfolder, they reference each other just using the file name but getting an compile error This is how UWP and WPF used to work but there is again� Style for Windows apps. 03/23/2020; 2 minutes to read; In this article. Style is what makes your app stand out. The style guidelines in this section help you create a Fluent Design through color, typography, and new features such as acrylic material and reveal highlight.

WPF Merged Dictionary problems and solutions, Now I'd like a new file to hold all my TextBlock styles. We see that in App.xaml when placing one merged dictionary below another, the XAML file in the 2nd� As long as your default style is in Generic.xaml itself and not in a merged dictionary. If you define a default style in a merged dictionary out of Generic.xaml, the resources defined in App.xaml will work with DynamicResource, but you won’t be able to use resources defined in Generic.xaml or a merged dictionary out of Generic.xaml; Solutions

Getting Started with UWP Diagram control, Learn here about getting started with Syncfusion UWP Diagram (SfDiagram) control Add the below required assembly reference to the project, Syncfusion. with height, width, shape, shape style,specific position, size and Annotation. We can add multiple Nodes with different shapes into diagram as NodeViewModel. Problem2 Well, it says references are missing but does not point to which one. However, the suggestion is to build the NuGet cache. I opened up an app which had worked in another instance of VS 2017.

For a Style that is used only by other UI items defined in the same XAML page, you typically define the Style in the FrameworkElement.Resources collection (Page.Resources if your root element is a Page). For a Style that is used by more than one page in your app, you typically define the Style in the Application.Resources collection.

Comments
  • I think this is not the problem of merging dictionary! one of your <x:String ...></x:String> must change to <x:Uri></x:Uri>
  • I don't understand - which string? Thanks
  • Can you upload your Brushes.xaml and CustomStyles.xaml files anywhere which I can download and see them?
  • CustomStyles.xaml is in the original post and I've just added Brushes.xaml
  • This is what I have done, kind of. But it means that in every style referenced in CustomStyles.xaml I will need a reference to Brushes.xaml. That isn't ideal, it also exhibits unpredictable behaviour that I won't go into now. But anyway I've found a solution which I'll post next. Also as I understand it using ms-appx:/// is only required if the file is in a different assembly.