Is there a MessageBox equivalent in WPF?

2019-01-07 04: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条回答
Animai°情兽
2楼-- · 2019-01-07 05:02

WPF contains the following MessageBox:

if (MessageBox.Show("Do you want to Save?", "Confirm", 
    MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{

}
查看更多
贼婆χ
3楼-- · 2019-01-07 05:06

The equivalent to WinForms' MessageBox in WPF is called System.Windows.MessageBox.

查看更多
趁早两清
4楼-- · 2019-01-07 05:11

As the others say, there is a MessageBox in the WPF namespace (System.Windows).

The problem is that it is the same old messagebox with OK, Cancel, etc. Windows Vista and Windows 7 have moved on to use Task Dialogs instead.

Unfortunately there is no easy standard interface for task dialogs. I use an implementation from CodeProject KB.

查看更多
老娘就宠你
5楼-- · 2019-01-07 05:12

The MessageBox in the Extended WPF Toolkit is very nice. It's at Microsoft.Windows.Controls.MessageBox after referencing the toolkit DLL. Of course this was released Aug 9 2011 so it would not have been an option for you originally. It can be found at Github for everyone out there looking around.

查看更多
forever°为你锁心
6楼-- · 2019-01-07 05:12

Maybe the code here below helps:

using Windows.UI.Popups;
namespace something.MyViewModels
{
    public class TestViewModel
    {
        public void aRandonMethode()
        {
            MyMessageBox("aRandomMessage");
        }

        public async void MyMessageBox(string mytext)
        {
            var dialog = new MessageDialog(mytext);
            await dialog.ShowAsync();
        }
    }
}
查看更多
迷人小祖宗
7楼-- · 2019-01-07 05:16

You can use this:

MessageBoxResult result = MessageBox.Show("Do you want to close this window?",
                                          "Confirmation",
                                          MessageBoxButton.YesNo,
                                          MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
    Application.Current.Shutdown();
}

For more information, visit MessageBox in WPF.

查看更多
登录 后发表回答