[英]Apply MahApps.Metro theme and accent to other controls or rectangles
[英]How to show dialog box without needing a metro accent theme?
我想像這樣顯示一個metro風格的對話框:
public async void Button_Click(object sender, RoutedEventArgs e)
{
var metroWindow = (Application.Current.MainWindow as MetroWindow);
await metroWindow.ShowMessageAsync("Title", "Body");
}
但是,它一直在拋出錯誤:
mscorlib.dll中發生了未處理的“System.NullReferenceException”類型異常
使用堆棧跟蹤:
在MahApps.Metro.Controls.Dialogs.BaseMetroDialog.HandleTheme()
在MahApps.Metro.Controls.Dialogs.BaseMetroDialog.Initialize()
在MahApps.Metro.Controls.Dialogs.BaseMetroDialog..ctor(MetroWindow owningWindow,MetroDialogSettings設置)
...
在System.Threading.ThreadHelper.ThreadStart()
經過大量的擺弄后,我意識到我需要在App.xaml
資源中添加一個重點來為它提供一個配色方案。
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
但是,我已經構建了我的程序,沒有它,包括它已經搞砸了我的所有樣式。 此外,這里看到的口音總數不多,我找不到符合我方案的口音。
我一直試圖直接使用諸如的命令為ShowMessageAsync
方法提供它自己的顏色方案
metroWindow.MetroDialogOptions.ColorScheme = MetroDialogOptions.ColorScheme
// MetroDialogColorScheme.Theme
// MetroDialogColorScheme.Accented
// MetroDialogColorScheme.Inverted;
但同樣的錯誤仍在繼續出現。 有辦法解決這個問題嗎? 我怎么能讓對話框使用我自己的風格?
編輯
我實際上已經在這里下載了這個文件: https : //github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Accents/Cobalt.xaml
並在ResourceDirectory.MergedDictionaries
替換
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
同
<ResourceDictionary Source="Cobalt.xaml" />
這足以打破這個功能。 我可以看到該主題仍然有效,但是當我嘗試觸發對話框時,窗口將會淡出,但對話框不會出現,我需要重新啟動程序。 這是怎么回事?
你應該看一下ThemeManager
。 應用程序啟動時設置AppStyle
如下:
ThemeManager.ChangeAppStyle(this, ThemeManager.Accents.First(x => x.Name == "Red"), ThemeManager.DetectAppStyle().Item1);
這是一個主題可用的例子:
要使用您自己的樣式,您可以添加如下的重音:
ThemeManager.AddAccent("XpertdocBlue", new Uri("XpertdocBlue.xaml", UriKind.Relative));
嘗試添加ResourceDictionary
這是我的:
<controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Xpertdoc.PortalWordAddIn.Views;component/Resources/Icons.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="XpertdocBlue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</controls:MetroWindow.Resources>
這是我的自定義口音( XpertdocBlue.xaml
):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="HighlightColor">#FF2B579A</Color>
<Color x:Key="AccentColor">#FF2B579A</Color>
<!--60%-->
<Color x:Key="AccentColor2">#CC2B579A</Color>
<!--40%-->
<Color x:Key="AccentColor3">#992B579A</Color>
<!--20%-->
<Color x:Key="AccentColor4">#662B579A</Color>
<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}"/>
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}"/>
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}"/>
<SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}"/>
<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" />
<SolidColorBrush x:Key="AccentSelectedColorBrush" Color="White" />
<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5">
<GradientStop Color="{StaticResource HighlightColor}" Offset="0" />
<GradientStop Color="{StaticResource AccentColor3}" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" />
<SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" />
<Color x:Key="IdealForegroundColor">White</Color>
<SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{StaticResource IdealForegroundColor}"/>
</ResourceDictionary>
MessageDialog md=new MessageDialog("my message");
await md.showAsync();
如果要創建自定義消息對話框,則必須創建自己的自定義控件。 您可以在需要時輕松地將樣式添加到自定義對話框中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.