簡體   English   中英

基本窗口的WPF樣式未在App.xaml中應用,但在Themes / Generic.xaml中

[英]WPF Style for base window not applied in App.xaml, but is in Themes/Generic.xaml

我正在為我的大多數窗口創建一個基本窗口類。 顯然,對此最好的解決方案是單獨的類,以及適用於它的樣式。

問題是我在App.Resources沒有應用<Style ../> 也就是說,如果它在外部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>

使用ThemeWindowMainWindow只是以下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的類型,然后回退到Generic.xaml並加載它。 這並不能解釋為什么如果<Style />在合並的ResourceDictionary可用,它將無法加載。 注意如果它確實工作MergedDictionary放入Generic.xaml ,原因顯而易見。

我完全可以將ResourceDictionary合並到Generic.xaml如果這是我必須做的事情。 我只想了解技術細節,了解為什么它需要像這樣。

這個不工作/工作的屏幕截圖: 破碎的圖像工作形象

我有一個簡單的解決方法,允許您在app.xaml中設置樣式。

在app.xaml中定義你的風格,如下所示:

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

並將你的TheWindow改為:

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屬性,如上例所示。

我討論了stackoverflow中已經討論過的以下解決方案。 它應該在加載應用程序時添加加載組件。

參考解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM