我正在将一个使用MahApps.Metro风格的wpf.netframework 4.7应用程序升级为wpf.netcoreapp3.1应用程序。迁移后我得到了:
System.Windows.Markup.XamlParseException:"System.Windows.StaticResourceExtension"上的提供值引发异常
异常:找不到名为"MetroButton"的资源。资源名称区分大小写
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Button.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Styles.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary>
<!--Button-->
<Style x:Key="SmallButtonStyle" TargetType="Button" BasedOn="{StaticResource MetroButton}">
<Setter Property="Height" Value="40" />
<Setter Property="Width" Value="200" />
<Setter Property="Margin" Value="5,2" />
<Setter Property="FontSize" Value="14" />
</Style>
<Style x:Key="StandardButtonStyle" TargetType="Button" BasedOn="{StaticResource MetroButton}">
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="350" />
<Setter Property="Margin" Value="10,5" />
<Setter Property="FontSize" Value="20" />
</Style>
这适用于.netframework 4.7下的MahApps.Metro 1.65版本,但对于MahApps.Matro 2.0.0-alpha0748,我得到了例外。
解决方案:将MetroButton更改为MahApps.Styles.Button
<Style x:Key="SmallButtonStyle" TargetType="Button" BasedOn="{StaticResource MahApps.Styles.Button}">
<Setter Property="Height" Value="40" />
<Setter Property="Width" Value="200" />
<Setter Property="Margin" Value="5,2" />
<Setter Property="FontSize" Value="14" />
</Style>