WPF样式为基极窗口中App.xaml中不施加,但在主题/ Generic.xaml(WPF Sty

2019-08-07 04:52发布

我在我的大部分窗户从派生创建基本窗口类的过程。 显然,我们的最佳解决方案是一个单独的类,并适用于它的样式。

问题是, <Style ../>我还没有被当它在应用App.Resources 。 也就是说,如果在外部的已定义ResourceDictionary ,并合并成App.xaml的资源,或者本地词典和合并,或内联放入App.Resources 。 所述<Style ../>是,然而,应用时,它被放置到Themes/Generic.xaml

这个问题可以在不脱离重写做什么特别的,在所有的基本窗口,除了证明DefaultStyleKeyProperty

下面是ThemeWindow

public class ThemeWindow : Window
{
    static ThemeWindow()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ThemeWindow), new FrameworkPropertyMetadata(typeof(ThemeWindow)));
    }
}

这里是非常简单<Style ../>我想申请(它使Window背景的红色,仅此而已):

<Style TargetType="{x:Type testing:ThemeWindow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type testing:ThemeWindow}">
                <Grid>
                    <Grid.Background>
                        <SolidColorBrush Color="Red"/>
                    </Grid.Background>
                    <AdornerDecorator>
                        <ContentPresenter />
                    </AdornerDecorator>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

MainWindow使用ThemeWindow ,简直就是下面的XAML代码:

<testing:ThemeWindow x:Class="Testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:testing="clr-namespace:Testing"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="125,83,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</testing:ThemeWindow>

现在,如前所述,如果你把那个Style在它自己ResourceDictionary的,包括它是这样的:

<App.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Themes/ThemeWindow.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</App.Resources>

.. 这是行不通的。 如果你直接内嵌样式到App.Resources ,这是行不通的。

我能找到它的工作的唯一情况就是调用ResourceDictionary XAML Generic.xaml ,并将其放置到Themes/应用程序的目录。

我想知道究竟为什么发生这种情况。

我唯一的理论是,当看到WPF控件类型,将头向Themes ,并扫描所有ResourceDictionary S为类型,然后回落到Generic.xaml并加载它。 这并不能解释为什么,如果它不能加载<Style />可在合并ResourceDictionary虽然。 注意如果它确实工作MergedDictionary放入Generic.xaml ,原因显而易见。

我具有合并完全正常ResourceDictionaryGeneric.xaml如果这是我必须做的。 我只是想在技术细节趴下,为什么它需要这样。

这不工作/工作的截图:

Answer 1:

我有一个简单的解决办法,将允许你在你的App.xaml设置你的风格。

定义在App.xaml中这样你的风格:

<Style x:Key="{x:Type testing:ThemeWindow}" TargetType="{x:Type testing:ThemeWindow}">

改变你的ThemWindow这样的:

public class ThemeWindow : Window
{
    static ThemeWindow()
    {
        StyleProperty.OverrideMetadata(typeof(ThemeWindow), new FrameworkPropertyMetadata(GetDefautlStyle()));
    }

    private static Style GetDefautlStyle()
    {
        if (defaultStyle == null)
        {
            defaultStyle = Application.Current.FindResource(typeof(ThemeWindow)) as Style;
        }
        return defaultStyle;
    }

    private static Style defaultStyle = null;
}

它并没有真正解决问题的,但是这将让你实现你需要的东西!

编辑:纵观DefaultStyleKey参考,它明确表示,它用于主题样式查找 。 这也解释了为什么它不会在App.xaml中或其他任何字典中找到它。 它不仅会在主题词典搜索。 所以,你要么有一个主题字典来定义自己的风格,或直接使用Style属性,在上面的例子。



Answer 2:

我来到我身边的计算器已经讨论了以下解决方案。 这将需要在负载组件的应用的负载时添加。

请参阅解决方案



文章来源: WPF Style for base window not applied in App.xaml, but is in Themes/Generic.xaml