xaml ResourceDictionary inside User Control Library

你离开我真会死。 提交于 2020-02-23 03:43:06

问题


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


回答1:


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}"



回答2:


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




回答3:


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?



来源:https://stackoverflow.com/questions/36447046/xaml-resourcedictionary-inside-user-control-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!