locked
Problem with StaticResources

    Question

  • Hello,

    I'm developing an Extension SDK which offers a Page (the "main"-entry point for the integrator) with several static resources.

    The resources I am using are defined in a separate "DefaultStyles.xaml" ResourceDictionary file.

    Right now the UI Designer doesn't show anything because when opening the page with the Visual Studio UI Designer, all the StaticResources usages cannot be found.

    I know that this is because the ResourceDictionary itself doesn't get loaded from within the Page, instead I'm loading it in the App.xaml which uses the SDK later and to allow to override the defined StaticResources. (By the way... when I try to load the ResourceDictionary inside <Page.Resources> to see all the stuff in the UI Designer the App crashes when initializing its components during debugging and navigating to the page)

    Is there any other possibility to allow the integrator to customize the Page (besides of implementing a huge bunch of Dependency Properties or importing the DefaultStyles.xaml and overriding its defined Key Resources, like I'm doing right now)?

    Here a simple sample:

    I have two projects: the App and the SDK

    SDK

    - MainPage.xaml (e.g. BackgroundColorBrush is defined in DefaultStyles.xaml)

    <Page
        x:Class="xyzmo.ModernUiFatClient.Sdk.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sdk="using:xyzmo.ModernUiFatClient.Sdk"
        xmlns:utilities="using:xyzmo.ModernUi.Sdk.Utilities"
        xmlns:xaml="using:xyzmo.UniversalSdk.Utilities.Xaml"
        mc:Ignorable="d"
        x:Name="PageRoot"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Loaded="OnMainPageLoaded"
        SizeChanged="MainPage_SizeChanged">
    
        <Page.Resources>
            <utilities:LocalizationHelper x:Key="LocalizationHelper" />
            <xaml:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        </Page.Resources>
        
        <Grid Background="{StaticResource BackgroundColorBrush}" Visibility="{Binding IsPageLoaded, Converter={StaticResource BooleanToVisibilityConverter}}">
    <!-- CONTENT HERE -->
       </Grid>
    </Page>

    - DefaultStyles.xaml

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


    App

    - App.xaml (the Page from the SDK is used in the OnLaunched method in the code behind)

    <Application
        x:Class="xyzmo.SIGNificant.SignatureCapture.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xaml="using:xyzmo.UniversalSdk.Utilities.Xaml"
        RequestedTheme="Light">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Sdk/DefaultStyle.xaml"/>
                    <ResourceDictionary>
    <!-- BackgroundColorBrush from DefaultStyles.xaml gets overridden here -->
                        <SolidColorBrush x:Key="BackgroundColorBrush" Color="Green" />
                    </ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

    I know this works but I don't feel very comfortable with this... since it is always required to import the DefaultStyles.xaml inside the App's code. Otherwise the StaticResources won't be found.

    Thank you very much for your help upfront!

    Daniel

    Tuesday, March 03, 2015 1:07 PM

Answers

  • Hi Sekeepa,

    >> Is there any other possibility to allow the integrator to customize the Page (besides of implementing a huge bunch of Dependency Properties or importing the DefaultStyles.xaml and overriding its defined Key Resources, like I'm doing right now)?

    Based on my knowledge, you are using the correct way to share stuff with ResourceDictionary. In this case, you may have some advanced scenarios, you can implement a class that can have different behavior than the XAML resource reference lookup behavior. For this purpose, you implement the class CUstomXamlResourceLoader.

    You can see more information about it from https://msdn.microsoft.com/en-us/library/windows/apps/hh968442.aspx?f=255&MSPPError=-2147217396. See Custom resource lookup section.

    Try have a look at the code sample about using resource. https://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 04, 2015 6:10 AM
    Moderator