Is there a MessageBox equivalent in WPF?

前端 未结 9 981
耶瑟儿~
耶瑟儿~ 2020-11-29 17:49

Is there a standard message box in WPF, like WinForms\' System.Windows.Forms.MessageBox.Show(), or should I use the WinForms message box?

9条回答
  •  孤街浪徒
    2020-11-29 18:17

    If you want to have your own nice looking wpf MessageBox: Create new Wpf Windows

    here is xaml :

    
        
    
        
        
            
                
            
            
                
                    
                        
                            
                            
                        
                    
                
            
            
                
                    
                
                
                
                    
                        
                    
                    
                        
                        
                            
                        
                        
                            
                            
                        
                    
                
            
        
    
    

    for cs of this file :

    public partial class MessageboxNew : Window
        {
            public MessageboxNew()
            {
                InitializeComponent();
                //second time show error solved
                if (Application.Current == null) new Application();
                        Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            }
    
            private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
            {
                DialogResult = true;
            }
    
            private void cancel_Click(object sender, RoutedEventArgs e)
            {
                DialogResult = false;
            }
    
            private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
            {
                this.MouseDown += delegate { DragMove(); };
            }
        }
    

    then create a class to use this :

    public class Mk_MessageBox
    {
        public static bool? Show(string title, string text)
        {
            MessageboxNew msg = new MessageboxNew
            {
                TitleBar = {Text = title},
                Textbar = {Text = text}
            };
            msg.noBtn.Focus();
            return msg.ShowDialog();
        }
    }
    

    now you can create your message box like this:

    var result = Mk_MessageBox.Show("Remove Alert", "This is gonna remove directory from host! Are you sure?");
                if (result == true)
                {
                    // whatever
                }
    

    copy this to App.xaml inside

    
        
            
                
                
                
                
                
                
                
    
                
                
                
    
                
                
                
                
            
        
    
    

    -------------------------------

    My Refrence : https://www.red-gate.com/simple-talk/dotnet/net-development/using-c-to-create-powershell-cmdlets-the-basics/

    for logic how can i make my own messagebox

提交回复
热议问题