WPF下自定義MessageBox訊息提示
阿新 • • 發佈:2019-02-01
使用系統MessageBox與自己專案風格存在明顯差異,定義自己風格的MessageBox
首先看一下確認視窗,
再看提示視窗
使用與系統MessageBox類似,下面說一下程式碼
1.確認視窗程式碼,MessageBoxOKCancel.xaml.cs
public partial class MessageBoxOKCancel : Window { public MessageBoxOKCancel() { InitializeComponent(); } public MessageBoxOKCancel(string message) { InitializeComponent(); this.message.Text = message; } private void Confirm_Click(object sender, RoutedEventArgs e) { try { this.DialogResult = true; } catch (Exception ex) { } this.Close(); } private void Cancle_Click(object sender, RoutedEventArgs e) { try { this.DialogResult = false; } catch (Exception ex) { } this.Close(); } }
2.提示視窗程式碼, MessageBoxOK.xaml.cs
public partial class MessageBoxOK : Window { public MessageBoxOK() { InitializeComponent(); } public MessageBoxOK(string mess) { InitializeComponent(); message.Text = mess; } private void Button_Click(object sender, RoutedEventArgs e) { try { this.DialogResult = false; } catch (Exception ex) { } this.Close(); } }
3.測試程式碼
private void Button_Click(object sender, RoutedEventArgs e) { //測試 string display = this.textBox.Text; if (MyMessageBox.ShowDialog(display, MyMessageBox.OKCANCLE).Value == true) { MyMessageBox.Show("確認"); } else { MyMessageBox.Show("取消"); } }
使用確認窗,MyMessageBox.ShowDialog(display, MyMessageBox.OKCANCLE);
使用提示窗,MyMessageBox.Show("確認");