MessageDialog 类

定义

表示用于向用户显示消息的对话框。

在桌面应用中,在以显示 UI 的方式使用此类的实例之前,需要将该对象与其所有者的窗口句柄相关联。 有关详细信息和代码示例,请参阅 显示依赖于 CoreWindow 的 WinRT UI 对象

重要

仅当升级使用 MessageDialog 的通用 Windows 8.x 应用,并且需要尽量减少更改或应用不是 XAML 时,才应使用 MessageDialog。 对于 Windows 10+ 中的新 XAML 应用,建议改用 ContentDialog 控件。

public ref class MessageDialog sealed
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Popups.IMessageDialogFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
class MessageDialog final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Popups.IMessageDialogFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class MessageDialog final
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Popups.IMessageDialogFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
public sealed class MessageDialog
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Popups.IMessageDialogFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class MessageDialog
function MessageDialog(content, title)
Public NotInheritable Class MessageDialog
继承
Object Platform::Object IInspectable MessageDialog
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

以下示例演示如何将命令添加到消息对话框并显示它。 有关完整的代码示例,请参阅 消息对话框示例

using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SDKTemplate;
using System;

private async void CancelCommandButton_Click(object sender, RoutedEventArgs e)
{
    // Create the message dialog and set its content
    var messageDialog = new MessageDialog("No internet connection has been found.");

    // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
    messageDialog.Commands.Add(new UICommand(
        "Try again", 
        new UICommandInvokedHandler(this.CommandInvokedHandler)));
    messageDialog.Commands.Add(new UICommand(
        "Close", 
        new UICommandInvokedHandler(this.CommandInvokedHandler)));

    // Set the command that will be invoked by default
    messageDialog.DefaultCommandIndex = 0;

    // Set the command to be invoked when escape is pressed
    messageDialog.CancelCommandIndex = 1;

    // Show the message dialog
    await messageDialog.ShowAsync();
}

private void CommandInvokedHandler(IUICommand command)
{
    // Display message showing the label of the command that was invoked
    rootPage.NotifyUser("The '" + command.Label + "' command has been selected.", 
        NotifyType.StatusMessage);
}
// MainPage.cpp
#include "pch.h"
#include "MainPage.h"

#include <winrt/Windows.UI.Popups.h>

#include "winrt/Windows.System.h"
#include "winrt/Windows.UI.Xaml.Controls.h"
#include "winrt/Windows.UI.Xaml.Input.h"
#include "winrt/Windows.UI.Xaml.Navigation.h"
#include <sstream>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::UI::Xaml;
...
void MainPage::CancelCommandButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    // Create the message dialog and set its content
    Windows::UI::Popups::MessageDialog msg{ L"No internet connection has been found." };

    // Add commands and set their callbacks.
    // Both commands use the same callback function instead of inline event handlers.
    Windows::UI::Popups::UICommand continueCommand{
        L"Try again",
        { this, &MainPage::CommandInvokedHandler} };
    Windows::UI::Popups::UICommand upgradeCommand{
        L"Close",
        { this, &MainPage::CommandInvokedHandler } };

    // Add the commands to the dialog.
    msg.Commands().Append(continueCommand);
    msg.Commands().Append(upgradeCommand);

    // Set the command that will be invoked by default.
    msg.DefaultCommandIndex(0);

    // Set the command to be invoked when escape is pressed.
    msg.CancelCommandIndex(1);

    // Show the message dialog.
    msg.ShowAsync();
}

void MainPage::CommandInvokedHandler(Windows::UI::Popups::IUICommand const& command)
{
    // Display message.
    std::wstringstream stringstream;
    stringstream << L"The '" << command.Label().c_str() << L"' command has been selected.";
    rootPage.NotifyUser(stringstream.str().c_str(), NotifyType::StatusMessage);
}
#include "pch.h"
#include "CancelCommand.xaml.h"

using namespace MessageDialogSample;

using namespace Windows::UI::Popups;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

void MessageDialogSample::CancelCommand::CancelCommandButton_Click(Platform::Object^ sender,
    Windows::UI::Xaml::RoutedEventArgs^ e)
{
    // Create the message dialog and set its content
    MessageDialog^ msg = ref new MessageDialog("No internet connection has been found.");

    // Add commands and set their callbacks.
    // Both commands use the same callback function instead of inline event handlers.
    UICommand^ continueCommand = ref new UICommand(
        "Try again", 
        ref new UICommandInvokedHandler(this, &CancelCommand::CommandInvokedHandler));
    UICommand^ upgradeCommand = ref new UICommand(
        "Close", 
        ref new UICommandInvokedHandler(this, &CancelCommand::CommandInvokedHandler));

    // Add the commands to the dialog
    msg->Commands->Append(continueCommand);
    msg->Commands->Append(upgradeCommand);

    // Set the command that will be invoked by default
    msg->DefaultCommandIndex = 0;

    // Set the command to be invoked when escape is pressed
    msg->CancelCommandIndex = 1;

    // Show the message dialog
    msg->ShowAsync();
}

void CancelCommand::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command)
{
    // Display message
    rootPage->NotifyUser("The '" + command->Label + "' command has been selected.", 
        NotifyType::StatusMessage);
}
Imports Windows.UI.Popups
Imports Windows.UI.Xaml
Imports Windows.UI.Xaml.Controls
Imports Windows.UI.Xaml.Navigation
Imports SDKTemplate

Partial Public NotInheritable Class CloseCommand
    Inherits SDKTemplate.Common.LayoutAwarePage

    ' A pointer back to the main page.  This is needed if you want to call methods in MainPage such
    ' as NotifyUser()
    Private rootPage As MainPage = MainPage.Current

    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Private Async Sub CloseCommandLaunch_Click(sender As Object, e As RoutedEventArgs)
        ' Create the message dialog and set its content and title
        Dim messageDialog = New MessageDialog("No internet connection has been found.")

        ' Add buttons and set their callbacks
        messageDialog.Commands.Add(New UICommand("Try again", Sub(command)
            rootPage.NotifyUser("The '" & command.Label & "' button has been selected.", _ 
                NotifyType.StatusMessage)
                                                              End Sub))

        messageDialog.Commands.Add(New UICommand("Close", Sub(command) 
            rootPage.NotifyUser("The '" & command.Label & "' button has been selected.", _
                NotifyType.StatusMessage)
                                                          End Sub))

        ' Set the command that will be invoked by default
        messageDialog.DefaultCommandIndex = 0

        ' Set the command to be invoked when escape is pressed
        messageDialog.CancelCommandIndex = 1

        ' Show the message dialog
        Await messageDialog.ShowAsync
    End Sub
End Class

注解

注意

此类不敏捷,这意味着需要考虑其线程模型和封送处理行为。 有关详细信息,请参阅线程和封送处理 (C++/CX) 和使用多线程环境中的Windows 运行时对象 (.NET)

对话框具有一个命令栏,可在桌面应用中最多支持三个命令,或在移动应用中支持两个命令。 如果未指定任何命令,则会添加默认命令以关闭对话框。 对话框会调暗它后面的屏幕,并阻止触摸事件传递到应用的画布,直到用户做出响应。

消息对话应谨慎使用,并且仅适用于必须阻止用户流的关键消息或简单问题。 下面是 示例部分中的代码 创建的对话框的示例。

包含两个命令的消息对话框

构造函数

MessageDialog(String)

初始化 MessageDialog 类的新实例以显示可用于询问用户简单问题的无标题消息对话框。

在桌面应用中,在以显示 UI 的方式使用此类的实例之前,需要将该对象与其所有者的窗口句柄相关联。 有关详细信息和代码示例,请参阅 显示依赖于 CoreWindow 的 WinRT UI 对象

对话框会调暗它后面的屏幕,并阻止触摸事件传递到应用的画布,直到用户做出响应。

消息对话应谨慎使用,并且仅适用于必须阻止用户流的关键消息或简单问题。

MessageDialog(String, String)

初始化 MessageDialog 类的新实例,以显示可用于询问用户简单问题的标题消息对话框。

在桌面应用中,在以显示 UI 的方式使用此类的实例之前,需要将该对象与其所有者的窗口句柄相关联。 有关详细信息和代码示例,请参阅 显示依赖于 CoreWindow 的 WinRT UI 对象

属性

CancelCommandIndex

获取或设置要用作取消命令的命令的索引。 这是当用户按 ESC 键时触发的命令。

在设置索引之前添加命令。

Commands

获取显示在消息对话框的命令栏中的命令数组。 这些命令使对话可操作。

获取此数组,并向其添加表示命令的 UICommand 对象。 如果对话框当前显示,则命令不会添加到命令栏中。

Content

获取或设置要向用户显示的消息。

DefaultCommandIndex

获取或设置要用作默认值的命令的索引。 这是用户按 Enter 键时默认触发的命令。

在设置索引之前添加命令。

Options

获取或设置 MessageDialog 的选项。

Title

获取或设置要显示在对话框上的标题(如果有)。

方法

ShowAsync()

开始显示对话框的异步操作。

适用于

另请参阅