为什么不应用来自 ResourceDictionary 的样式资源? [英] Why are Style Resources from a ResourceDictionary not applied?

查看:22
本文介绍了为什么不应用来自 ResourceDictionary 的样式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将所有资源添加到资源模板文件中,并将它们合并为动态资源(Blend 也是这样做的).该程序编译并运行良好,但根本不应用样式.不过,奇怪的是,它们 应用在预览中.这是我的代码:

I tried to add all my resources to a resource template file, and incorporate them as dynamic resources (precisely the way Blend does this too). The programme compiles and runs fine, but the styles aren't applied at all. Bizarrely, they are applied in the preview, though. This is the code I have:

// MyUserControl.xaml
<UserControl x:Class="MyProject.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
         d:DesignHeight="411" d:DesignWidth="645">
     <Grid>
        <Button Content="Click here" Style="{DynamicResource MyButtonStyle}" />                
     </Grid>
 </UserControl>

-

// MyStyleTemplates.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FF263B5B" Offset="0"/>
                    <GradientStop Color="#FF65FD43" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

-

// App.xaml
<Application x:Class="MyProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyProject;component/MyStyleTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我尝试做完全相同的事情,但随后将 Style 代码放在 标记中 MyUserControl.xaml> 文件,并且再次正常工作,因此样式本身没有问题.为什么会发生这种情况?

I tried to do exactly the same but then putting the Style code within the <UserControl.Resources> tag in the MyUserControl.xaml file, and that worked fine again, so nothing is wrong with the Style itself. Why does this happen?

可能跟这个有关系,文件夹结构可能有点不合常规,如下:

As this may have something to do with it, the folder structure may be slightly unconventional, as follows:

MyProjectFolder /
    MyProject.csproj
    MyStyleTemplates.xaml
    Main /
       App.xaml
    GUI /
       MyUserControl.xaml
       MyUserControl.xaml.cs

这允许吗?

推荐答案

原因是想定义自己的Main()方法,所以改了App.xaml.csproj 文件中的 文件标记从 ApplicationDefition 到简单的 Page.这有效,但如果您还想以上述方式使用资源,则无效.

The reason was that I wanted to define my own Main() method, so I had changed the App.xaml file tag in the .csproj file from ApplicationDefition to simply Page. This works, but NOT if you also want to use resources in the manner described above.

唯一的问题是 VS 不会就此警告您,甚至正确显示预览.将标签改回 ApplicationDefition 可以解决问题,但您需要找到另一种定义自己的 main 方法的方法.

The only trouble is that VS doesn't warn you about this, and even shows the previews correctly. Changing the tag back to ApplicationDefition solves the problem, although then you need to find another way of defining your own main method.

这篇关于为什么不应用来自 ResourceDictionary 的样式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆