簡體   English   中英

WPF:App.xaml和Generic.xaml有什么區別?

[英]WPF: What's the difference between App.xaml and Generic.xaml?

請告訴我App.xaml和Generic.xaml之間的區別,我在這兩者之間感到困惑!

App.xaml是Application類的XAML部分 - 您可以在其中定義應用程序范圍的邏輯和資源的單一集中位置。 Generic.xaml位於項目的Themes目錄中,是一個字典,您可以在其中為所有自定義控件定義默認樣式。 Themes文件夾中沒有Windows主題特定字典時,將使用此字典。 例如,您可能具有以下Themes目錄的結構:

MyProject
   - Themes
     - Generic.xaml // Default styles if current theme is non of the themes below
     - Classic.xaml // Styles for “Classic” Windows 9x/2000 look on Windows XP.
     - Luna.NormalColor.xaml // Styles for default blue theme on Windows XP.
     - Luna.Homestead.xaml //  Styles for olive theme on Windows XP.
     - Luna.Metallic.xaml // Styles for silver theme on Windows XP.
     - Royale.NormalColor.xaml // Styles for default theme on Windows XP Media Center Edition.
     - Aero.NormalColor.xaml // Styles for default theme on Windows Vista

如果您希望自定義控件在任何Windows主題上看起來相同,則只需創建Generic.xaml。

所以,基本上你應該只使用Generic.xaml為你的自定義控件定義樣式,為其他所有東西定義App.xaml (例如你的畫筆,顏色等,或者你自己的標准控件樣式)。

另請參閱這個問題的答案: Generic.xaml有什么特別之處?

App.xaml用於應用程序范圍的資源,並始終使用。

Generic.xaml用於自定義控件的模板和樣式,並且在您未在控件級別指定其他樣式或模板時使用。

App.xaml是應用程序級資源的容器。

Generic.xaml是所有控件的資源文件,不基於自定義或默認主題。

App.xaml用於應用程序范圍的資源,因此可以包含對其他XAML資源的引用。

<Application
             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"  x:Class="App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Themes/ValidationStyles.xaml"/>
                <ResourceDictionary Source="Themes/ControlStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

這允許您在給定的XAML文件中隔離樣式以實現可管理性,然后在應用程序中使用該文件,因為它在運行時被合並到應用程序中。

generic.xaml用作默認自定義控件默認樣式的容器。 在解析給定類型的樣式時,框架將在Themes目錄中查找generic.xaml

暫無
暫無

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

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