问题
这个摇号软件的C# WPF代码可以通过编译,但是在使用Display()的第一个重载时程序会闪退,如何使自定义弹窗MyMessageBox正常显示?
代码
// MyMessageBox.xaml.cs
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Lottery
{
/// <summary>
/// Interaction logic for MyMessageBox.xaml
/// </summary>
public enum MyMessageBoxStyles
{
Information,
Warning,
Error
}
public partial class MyMessageBox : Window
{
private readonly Button conb = new Button
{
Margin = new Thickness(3),
Content = "Continue",
Style = (Style)Application.Current.FindResource("ButtonStyle")
};
private void Set(Brush start, Brush mid, Brush end)
{
Ani.ButtonBind(b, start, mid, end);
Ani.ButtonBind(c, start, mid, end);
Ani.ButtonBind(sb, start, mid, end);
}
private void SetMore(Brush start, Brush mid, Brush end)
{
Set(start, mid, end);
Ani.ButtonBind(conb, start, mid, end);
}
private void Register(string title, string content, Window owner, bool co, MyMessageBoxStyles style)
{
InitializeComponent();
cb.ItemsSource = new int[] { 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 };
cb.SelectedIndex = 4;
cb.SelectionChanged += (s, e) => { t.FontSize = int.Parse(cb.SelectedItem.ToString()); };
b.Click += (s, e) => { Close(); };
c.Click += (s, e) => { Clipboard.SetText(t.Text); };
sb.Click += (s, e) =>
{
SaveFileDialog dialog = new SaveFileDialog()
{
Title = "Save File",
Filter = "Text Files (*.txt)|*.txt"
};
if (dialog.ShowDialog() == true)
{
try { File.WriteAllText(dialog.FileName, t.Text); }
catch (Exception ex) { Display("Error", $"Error message: {ex.Message}", this, MyMessageBoxStyles.Error); }
}
};
switch (style)
{
case MyMessageBoxStyles.Information:
if (co) SetMore(Brushes.DeepSkyBlue, Brushes.DodgerBlue, Brushes.CornflowerBlue);
else Set(Brushes.DeepSkyBlue, Brushes.DodgerBlue, Brushes.CornflowerBlue);
break;
case MyMessageBoxStyles.Warning:
if (co) SetMore(Brushes.Orange, Brushes.DarkOrange, Brushes.Coral);
else Set(Brushes.Orange, Brushes.DarkOrange, Brushes.Coral);
break;
case MyMessageBoxStyles.Error:
if (co) SetMore(new SolidColorBrush(Color.FromRgb(255, 75, 75)), Brushes.Red, Brushes.Crimson);
else Set(new SolidColorBrush(Color.FromRgb(255, 75, 75)), Brushes.Red, Brushes.Crimson);
break;
}
Title = title;
Owner = owner;
t.Text = content;
}
public MyMessageBox(string title, string content, Window owner, MyMessageBoxStyles style) { Register(title, content, owner, false, style); }
public MyMessageBox(string title, string content, Window owner, Action action, MyMessageBoxStyles style)
{
conb.Click += (s, e) => {
Close();
action();
};
RowDefinition newRow = new RowDefinition { Height = new GridLength(36) };
g.RowDefinitions.Add(newRow);
Grid.SetRow(conb, 4);
Grid.SetColumnSpan(conb, 2);
g.Children.Add(conb);
MinHeight = 236;
Register(title, content, owner, true, style);
}
public static void Display(string title, string content, Window owner, Action action, MyMessageBoxStyles style = MyMessageBoxStyles.Information)
{
MyMessageBox m = new MyMessageBox(title, content, owner, action, style);
m.ShowDialog();
}
public static void Display(string title, string content, Window owner, MyMessageBoxStyles style = MyMessageBoxStyles.Information)
{
MyMessageBox m = new MyMessageBox(title, content, owner, style);
m.ShowDialog();
}
}
}
// Ani.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Lottery
{
internal class Ani
{
public static void ButtonBind(Button b, Brush start, Brush mid, Brush end)
{
b.Background = mid;
b.Foreground = Brushes.White;
b.MouseEnter += (s, e) => { ScaleAniShow(b, 1, 1.05); b.Background = start; };
b.MouseLeave += (s, e) => { ScaleAniShow(b, 1.05, 1); b.Background = mid; };
b.PreviewMouseDown += (s, e) => { ScaleAniShow(b, 1.05, 0.95); b.Background = end; };
b.PreviewMouseUp += (s, e) => { ScaleAniShow(b, 0.95, 1.05); b.Background = start; };
}
public static void TextBoxBind(TextBox t)
{
t.PreviewMouseDown += (s, e) => { ScaleAniShow(t, 1, 0.95); };
t.PreviewMouseUp += (s, e) => { ScaleAniShow(t, 0.95, 1); };
}
public static void ScaleAniShow(UIElement element, double Sizefrom, double Sizeto, double RenderX = 0.5, double RenderY = 0.5, int power = 5)
{
ScaleTransform scale = new ScaleTransform();
element.RenderTransform = scale; // Define the central position of the circle.
element.RenderTransformOrigin = new Point(RenderX, RenderY); // Define the transition animation, 'power' is the strength of the transition.
DoubleAnimation scaleAnimation = new DoubleAnimation()
{
From = Sizefrom, // Start value
To = Sizeto, // End value
FillBehavior = FillBehavior.HoldEnd,
Duration = TimeSpan.FromMilliseconds(250), // Animation playback time
EasingFunction = new PowerEase() // Ease function
{
EasingMode = EasingMode.EaseInOut,
Power = power
}
};
scale.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation);
}
}
}
<Window x:Class="Lottery.MyMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="260" Width="300" MinHeight="200" MinWidth="250" WindowStartupLocation="CenterOwner">
<Grid Margin="3" Name="g">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="27"/>
<RowDefinition Height="36"/>
<RowDefinition Height="36"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="0" Margin="3" Grid.ColumnSpan="2">
<TextBlock Name="t" FontSize="17" TextWrapping="Wrap"/>
</ScrollViewer>
<ComboBox Name="cb" IsReadOnly="True" Grid.Row="1" Margin="3" Grid.Column="1" ToolTip="Change the font size of the text above."/>
<Label Content="Font Size:" FontSize="15" Grid.Row="1" Grid.Column="0" ToolTip="Change the font size of the text above."/>
<Button Style="{DynamicResource ButtonStyle}" Content="Copy" Name="c" Grid.Row="2" Grid.Column="0" Margin="3"/>
<Button Style="{DynamicResource ButtonStyle}" Content="Close" Name="b" Grid.Row="2" Grid.Column="1" Margin="3"/>
<Button Style="{DynamicResource ButtonStyle}" Content="Save" Name="sb" Grid.Row="3" Margin="3" Grid.ColumnSpan="2"/>
</Grid>
</Window>