我正面临上述错误的可怕问题。我正在开发一个 WPF 应用程序,我只想为我的应用程序显示一个自定义的消息框。所以我在参考资料中包含了 ExtendedWPF 工具包。我已经在 App.Xaml 中声明了 Messagebox 的样式。它如下:
<!--MessageBox Style-->
<SolidColorBrush x:Key="MyButtonHoverBrush" Color="Firebrick"/>
<SolidColorBrush x:Key="MyButtonPressedBrush" Color="DarkRed"/>
<Style x:Key="MyCloseButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00:0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonHoverBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00:0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonPressedBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Background" CornerRadius="0,0,2,0" Background="Blue">
<Border Margin="1,0,1,1" Background="Green" BorderThickness="1" CornerRadius="0,0,1,0"/>
</Border>
<ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
<Path x:Name="path" Fill="Red" Margin="0,0,0,1" Visibility="Collapsed" Height="6" Width="7" Stretch="Fill" Opacity="1"
Data="M 2,6 C2,6 3,6 3,6 3,6 3,5 3,5 3,5 4,5 4,5 4,5 4,6 4,6 4,6 5,6 5,6 5,6 7,6 7,6 7,6 7,5 7,5 7,5 6,5 6,5 6,5 6,4 6,4 6,4 5,4 5,4 5,4 5,2 5,2 5,2 6,2 6,2 6,2 6,1 6,1 6,1 7,1 7,1 7,1 7,0 7,0 7,0 5,0 5,0 5,0 4,0 4,0 4,0 4,1 4,1 4,1 3,1 3,1 3,1 3,0 3,0 3,0 2,0 2,0 2,0 0,0 0,0 0,0 0,1 0,1 0,1 1,1 1,1 1,1 1,2 1,2 1,2 2,2 2,2 2,2 2,4 2,4 2,4 1,4 1,4 1,4 1,5 1,5 1,5 0,5 0,5 0,5 0,6 0,6 0,6 2,6 2,6 z"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type controls:MessageBox}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Yellow" Offset="0"/>
<GradientStop Color="Tomato" Offset="0.543"/>
<GradientStop Color="Turquoise" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="WindowOpacity" Value="0.7"/>
<Setter Property="Height" Value="170"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="CaptionForeground" Value="White"/>
<Setter Property="WindowBorderBrush" Value="Blue"/>
<Setter Property="WindowBackground">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Blue" Offset="0.014"/>
<GradientStop Color="Blue" Offset="1"/>
<GradientStop Color="White" Offset="0.514"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="CloseButtonStyle" Value="{StaticResource MyCloseButtonStyle}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontFamily" Value="Palatino Linotype"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
并且我已经在全局类中声明了 MessageBox 到其中传递消息字符串的位置,如下所示:
public static void msgWarning(string msg)
{
string caption = "Warning";
MessageBoxButton btn= MessageBoxButton.OK;
MessageBoxImage img = MessageBoxImage.Warning;
MessageBoxResult res = Microsoft.Windows.Controls.MessageBox.Show(msg, caption, btn, img);
}
但是当我引用样式时,在调用 MessageBox 期间,我在 MessageBoxResult Line 中得到了提到的错误。但是如果我评论 Style 然后执行 MessageBox 将被显示。
请帮助我摆脱困境。
提前致谢。