簡體   English   中英

在WPF中創建UI設置的正確方法是什么?

[英]What is the proper way to create UI settings in WPF?

我現在正在XAML中使用基本樣式,其中包括用於我的標准HandledWindow類型的模板。 該樣式包括一些本地資源,例如顏色,字體和其他變量,這些樣式稍后可以在我的樣式中重復使用。

我在考慮用戶的UI設置,因此他可以根據需要更改顏色和大小等內容。 但是后來我發現,更改本地資源不會更改樣式本身,而只會更改當前的HandledWindow實例。因此,這不是UI設置的合適方法,因為應用程序中可能會有更多正在運行的窗口。

然后我意識到我必須相對於HandledWindow類的模板綁定變量,該模板將包括所有可更改的設置作為公共和靜態屬性。 但是后來我遇到了靜態屬性綁定的問題,因為我無法引發僅適用於實例的PropertyChanged事件。 而且窗口本身不會更新其樣式。

另外,我試圖使樣式做出反應並立即進行更新,而無需重新啟動。

WPF是“以資源為中心”的。 您可以在資源中定義所有UI樣式,畫筆和模板,並且在運行時很容易啟用包含您提到的所有屬性的應用程序范圍的主題更改。 這是我的MainViewModel通過它的SettingsViewModel從“設置”窗口接收到一條消息后在其內部執行的操作:

private void ApplyTheme()
{          
    Application.Current.Resources.MergedDictionaries.Clear();

    var rd = new ResourceDictionary { { "Locator", locator } };
    Application.Current.Resources.MergedDictionaries.Add(rd);

    switch (theme)
    {
        case "Blue":
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
            break;
        case "Summer":
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) });
            Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) });
            break;
        }
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Brushes.xaml", UriKind.RelativeOrAbsolute) });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/ControlTemplates.xaml", UriKind.RelativeOrAbsolute) });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/DataTemplates.xaml", UriKind.RelativeOrAbsolute) });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Styles.xaml", UriKind.RelativeOrAbsolute) });
    }

顯然,我使用的是Telerik控件,因此我加載了它們的字典,但是在方法的底部,您會注意到我也加載了我自己的資源,例如Br​​ushes,Styles等。

總之,使用WPF不會更容易在應用程序范圍內更改主題。

暫無
暫無

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

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