locked
How to reference Styles and XAML resources from other project in UWP/XAML apps? RRS feed

  • Question

  • Hi,

    I'm migrating my Windows 8.1/Xaml app to Windows 10/UWP. I used this Channel 9 video for my migration path: https://channel9.msdn.com/Events/Build/2015/3-741

    Author sugested to create new App project (UWP) and move all stuff, add references and fix what is now compiling. Right now I was stopped on resolving ResourceDictionary problems in my App.xaml file.

    About projects structure:

    My solution contain several projects: Main App project (Windows 8.1) and other Windows 8.1 PCL projects (Services, ViewModels, Styles, etc). This architecture now (in Windows 8.1) allows me to easly share my code between Windows 8.1 and Windows Phone 8.1. Also I have some common styles (Brushes, DataTemplates) that I share between projects.

    In my App.xaml I have this piece of XAML code:

    <Application.Resources>
    	<ResourceDictionary>
    		<ResourceDictionary.MergedDictionaries>
    			<ResourceDictionary Source="ms-appx:///Poniat.Styles/Brushes.xaml"/>
    		</ResourceDictionary.MergedDictionaries>
    	</ResourceDictionary>
    </Application.Resources>

    As you can see, I'm accessing Brushes styles from my Poniat.Styles project with ms-appx syntax. In Windows 8.1/Phone 8.1 it was working ok. Now in UWP it's not.

    When I compile UWP app project I receive: An error occurred while finding the resource dictionary "ms-appx:///Poniat.Styles/Brushes.xaml".

    Can someone help we with it? Does syntax for Uri changed? How can I reference styles from other project?

    Best regards / Poniat


    poniat



    Friday, October 9, 2015 11:22 AM

Answers

  • Hi Poniat,

    As far as I know, syntax for URI hasn't changed. The URI schemes used in Windows 8.1 still applies to Universal Windows Platform (UWP) apps and Windows 10. And I did a simple test with both PCL for Windows 8.1 and Clsaa Library for Universal Windows. They all works with no error in my side. Following is my sample:

    In Brushes.xaml, I defined a SolidColorBrush named "MyBrush".

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush x:Key="MyBrush" Color="Red" />
    </ResourceDictionary>

    Then I added these two library in my main project and in its App.xaml:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--<ResourceDictionary Source="ms-appx:///Poniat.Styles/Brushes.xaml" />-->
                <ResourceDictionary Source="ms-appx:///Poniat.Styles.Portable/Brushes.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

    After this I can use MyBrush in my MainPage.xaml.

    <TextBlock FontSize="50" Foreground="{StaticResource MyBrush}" Text="Test" />

    So please check your References and make sure you have added your library in your project.

    Also please check your Assembly name and make sure it's the same as what you used in your App.xaml(In your case is "Poniat.Styles").

    Hope this helps.

    Regards,

    Jay

    Saturday, October 10, 2015 9:26 PM