简体   繁体   English

将App.xaml样式移动到资源字典

[英]Moving App.xaml Styles to Resource Dictionary

I have a custom control that uses styles that are in a resource dictionary that is in linked in the app.xaml. 我有一个自定义控件,它使用在app.xaml中链接的资源字典中的样式。 If I take the link off and add the link to the page that contains the control it doesn't work. 如果我关闭该链接并将链接添加到包含该控件的页面,则该链接不起作用。 Why is that? 这是为什么? Why would my control (a dll) need the styles to be in the app.xaml and not just on the page that the control is contained in? 为什么我的控件(一个DLL)需要样式在app.xaml中,而不仅仅是在包含控件的页面上?

Why would my control (a dll) need the styles to be in the app.xaml and not just on the page that the control is contained in? 为什么我的控件(一个DLL)需要样式在app.xaml中,而不仅仅是在包含控件的页面上?

Custom controls need a default style. 自定义控件需要默认样式。 This default style is set within the constructor. 此默认样式在构造函数中设置。 eg: 例如:

public CustomControl()
{
    DefaultStyleKey = typeof(CustomControl);
}

When this is set it looks within the containing assembly for this style. 设置此项后,它将在此样式的包含程序集中查找。 If the control is within an application, then it looks within the App.xaml. 如果控件在应用程序中,则它在App.xaml中查找。 If the control is within a class library, it looks within a file Generic.xaml that must be placed within a folder "Themes". 如果控件位于类库中,它将在文件Generic.xaml中查找,该文件必须放在文件夹“Themes”中。 You do not need to place the style within either of these files. 您无需将样式放在这些文件中的任何一个中。 You can create a separate file that contains the style and reference it from either App.xaml or Themes/Generic.xaml (based on where the control is defined). 您可以创建一个包含该样式的单独文件,并从App.xaml或Themes / Generic.xaml引用它(基于定义控件的位置)。 To do this you create a MergedDictionary within one of those files. 为此,您可以在其中一个文件中创建MergedDictionary。 If your control is defined in your application you would do 如果你的控件是在你的应用程序中定义的那么你可以

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Controls/CustomControl.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    <Application.Resources>
</Application>

If your control is defined within a class library the Themes/Generic.xaml should look like this 如果您的控件是在类库中定义的,则Themes / Generic.xaml应该如下所示

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/My.Custom.Assembly;component/FolderLocationOfXaml/CustomControl.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

No matter where your custom control is placed the xaml for this will always look the same 无论您的自定义控件放在何处,xaml都将始终保持相同

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:My.Custom.Assembly.Controls">
        <Style TargetType="local:CustomControl">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomControl">
                    <Grid>
                        <! -- Other stuff here -->
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Without this default style defined, there is no way to determine what style to override. 如果未定义此默认样式,则无法确定要覆盖的样式。 Once the default style is defined you can change the style within your app or anywhere else the control is being used. 定义默认样式后,您可以更改应用程序中的样式或使用控件的任何其他位置。

Try moving the style into the control to verify that all of the required references are in place for your control to use items from the dictionary. 尝试将样式移动到控件中以验证所有必需的引用是否适合您的控件使用字典中的项目。 Make sure that the project containing your UserControl has a reference to the project that contains the resource dictionary. 确保包含UserControl的项目具有对包含资源字典的项目的引用。 Verify your Source path to the dictionary: 验证字典的源路径:

<ResourceDictionary Source="/AssemblyName;component/StylesFolderName/ResourceDictionaryName.xaml" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问其他xaml文件中App.xaml资源字典中的变量? - How to access variables in the App.xaml resources dictionary in other xaml files? WP7]用于主题感知的动态颜色,或者如何在App.xaml的自定义资源中为属性定义动态值? - WP7]Dynamic Color for theme awareness OR How to define a dynamic value for a property in a custom resource in App.xaml? 什么是App.xaml在Silverlight中使用? - What is App.xaml used for in Silverlight? Silverlight从动态XAP加载导入App.xaml - Silverlight import App.xaml from dynamically xap loaded 拆分app.xaml的最佳方法是什么? - What's the best approach to split app.xaml? 我可以在一个silverlight项目中使用2个App.XAML吗? - Can I use 2 App.XAML in one silverlight project? Silverlight:如何在启动时不从app.xaml加载MergedDictionaries? - Silverlight: how to not load MergedDictionaries from app.xaml at startup? 将App.xaml中的Parm传递给ViewModel-Silverlight MVVM - Passing Parm from App.xaml to ViewModel - Silverlight MVVM Silverlight App.xaml中的“元素已经是另一个元素的子元素”错误 - 'Element is already the child of another element' error in Silverlight App.xaml 在app.xaml中定义的Silverlight ViewModelLocator需要对DI容器的引用 - Silverlight ViewModelLocator defined in app.xaml needs a reference to the DI container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM