Using resource dictionary from a separate file gives me a designer error: Failed to set Source

1.2k views Asked by At

I'm trying to add a resource dictionary to a page but I keep getting a designer error in my xaml. The app runs fine with no issue but the designer error bothers me.

Here's how I did it. Both works fin at run time. But it's saying Failed to set "Source". Any clues?

enter image description here enter image description here

That also gives an error to all the static resource I used from the resource dictionary.

Update:

This is another approach. Instead of directly adding it to the Page's resources, I added it to the Application.Resources still cant resolve the styles. I'm using VS2017 v15.4.4

Steps to reproduce:

  1. List item
  2. Create a new UWP Application Project (App1)
  3. Create a new UWP Class Library (ClassLibrary1)
  4. In ClassLibrary1, add a new ResourceDictionary (Dictionary1.xaml).
  5. In Dictionary1.xaml, add a Style, let's say a button style.

    <Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1"> <Setter Propeprty="Background" Value="Red" /> </Style>

  6. In the ClassLibrary1, add a new BlankPage (BlankPage1)

  7. In the BlankPage1, add a button and use ButtonStyle1 `
  8. In App1, merge the resource dictionary to App.Resources (inside App.xaml). <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionary> <ResourceDictionary Source="ms-appx:///ClassLibrary1/Dictionary1.xaml" /> </ResourceDictionary.MergedDictionary> </ResourceDictionary> </Application.Resources>
  9. Make the BlankPage1 the start page, go to App.xaml.cs and and change MainPage to BlankPage1: if (e.PrelaunchActivated == false) { if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter rootFrame.Navigate(typeof(BlankPage1), e.Arguments); } // Ensure the current window is active Window.Current.Activate(); }

  10. Run the application. It will run just fine. But the problem is in the designer where it cannot recognize the style from the resource dictionary.

1

There are 1 answers

2
Ahmed Rashad Mohamed On

I followed your steps and found a typo and an issue.

The typo: In Style step (Perhaps only in the sample), There is typo in the word 'Proeprty':

 <Style TargetType="Button" x:Name="ButtonStyle1" x:Key="ButtonStyle1">
        <Style.Setters>
            <Setter Property="Background" Value="Red" />
        </Style.Setters>
 </Style>

The issue in the way you are merging dictionary, it should be using the following syntax:

<Page.Resources>
        <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary Source="Dictionary1.xaml" />
           </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Page.Resources>

After fixing both issues, Visual studio designer didn't complain about anything.

Did it fix the issue for you? If not then your steps to reproduce aren't complete.