Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP - Reference StaticResource from different style resource dictionary: Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'

Hello my team and I recently started developing an win10 uwp application. Application will have a lot of views and components so heavy use of styles is expected, so we need to organize our styles through file/folder structure we did this using following structure (unfortunately I cannot embed images yet see the link):

https://i.stack.imgur.com/XvW0u.png

Anyways my Resource.xaml merges all other dictionaries as following:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Resources/Colors.xaml" />
        <ResourceDictionary Source="/Resources/Icons.xaml" />
        <ResourceDictionary Source="/Resources/Fonts.xaml" />
        <ResourceDictionary Source="/Resources/Converters.xaml" />
        <ResourceDictionary Source="/Resources/Buttons.xaml" />
        <ResourceDictionary Source="/Resources/RadioButton.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

And in my App.xaml I reference this dictionary:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </ResourceDictionary>
</Application.Resources>

Now I managed to find the source of the problem in my RadioButton.xaml I reference a brush defined in Colors.xaml using StaticResource lookup:

<Setter Property="Foreground" Value="{StaticResource TopMenuTextBrush}" />

If I remove this line everything will start but with it I get following exception:

  • Exception {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.

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: 28 Position: 37]} System.Exception {Windows.UI.Xaml.Markup.XamlParseException}

Interesting thing is when I start the app with this line commented and uncomment it visual studio will recognize the brush and apply it correctly, it only breaks on application start. We used same approach before when developing WPF, so I'm thinking it might have to do with something regarding application deployment.

All help is greatly appreciated.

like image 468
ivke Avatar asked Nov 14 '17 11:11

ivke


1 Answers

Exception = {Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.

The problem is that you have used wrong ResourceDictionary source . I found the Resources.xaml and other xaml file stored in the same level directory in your screenshot. So you could not declare the parent directory of these xaml files within source. Please modify ResourceDictionary like the following

<ResourceDictionary Source="Colors.xaml"/>

For more you could refer to ResourceDictionary and XAML resource references.

like image 147
Nico Zhu - MSFT Avatar answered Oct 17 '22 01:10

Nico Zhu - MSFT