2
votes

how is it possible to define a ResourceDictionary inside a User Control Library and access them via Xaml-Code.

I've created something like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                >


    <Style x:Key="NormalStyle" TargetType="{x:Type Control}">
        <Setter Property="Foreground" Value="Black" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="FontStyle" Value="Normal" />
    </Style>
    .
    .
    .
</ResourceDictionary

And now I want to use this "NormalStyle" with a Control

 Style="{StaticResource NormalStyle}"

But Visual Studio says "The resource "NormalStyle" could not be resolved." Did i miss or forget something?

Thanks for helping me

3

3 Answers

3
votes

You will have to include or merge your ResourceDictionary with UserControl.Resources like below. Here in Source give path to your ResourceDictionary.

<UserControl.Resources>
        <ResourceDictionary Source="MyResourceDictionary.xaml"/>
</UserControl.Resources>

OR

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

Then you can use the resources in the dictionary inside your UserControl

Style="{StaticResource NormalStyle}"
0
votes

Please add style reference in App.xamal therfore so that it loads when you run the application. When you add any external resource file you need to add it in App.xamal to get it's reference in your app else you need to manually add reference to every xamal form to access style and templates

This is how you going to add it in App.xamal using MergedDictionaries than where ever you refer any style in you application it will have it's reference

 <ResourceDictionary.MergedDictionaries>

                    <!-- 
                        Styles that define common aspects of the platform look and feel
                        Required by Visual Studio project and item templates
                     -->
                    <ResourceDictionary Source="Common/CommonStyles.xaml"/>

                    <ResourceDictionary Source="Project/Common/CommonStyles.xaml"/>
                </ResourceDictionary.MergedDictionaries>

Hope that helps

-1
votes

Alright, so I got it half to work. Inside my UserControlLibrary i made a new Xaml-File "UIStandards", where put all my Styles into. Now in my Main Project, i want to have access to these Styles and I think i should use a MergedDictionary.

What i did was this:

<Application x:Class="MentorPlusClientWindowsWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:MentorPlusClientWindowsWPF"
         xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
         Startup="Application_Startup">
<!--StartupUri="MainWindow.xaml"-->

<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/CrossProjectUserControls;component/UIStandards.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

As you can see, i referenced the UISTandards.xaml but inside a UserControl inside my MainProject, none of my Styles can be found. Any solutions?