Why are Style Resources from a ResourceDictionary not applied?

余生颓废 提交于 2020-01-06 10:25:45

问题


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>

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?

Edit:

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

Is this allowed?


回答1:


In your App.xaml change the ResourceDictionary merge as below

<ResourceDictionary Source="pack://application:,,,/MyProject;component/MyStyleTemplates.xaml"/>



回答2:


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.

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.



来源:https://stackoverflow.com/questions/19982330/why-are-style-resources-from-a-resourcedictionary-not-applied

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