如何在C#中创建像Groove音乐一样的消息框

时间:2016-10-06 02:59:27

标签: c# uwp messagebox

我需要在消息框中显示如下信息:

messagebox

如何创建类似的消息框?

2 个答案:

答案 0 :(得分:3)

如果您只想创建一个包含标题,文本内容和可自定义按钮(命令)的简单消息框,请使用MessageDialog类。

private void showDialog()
{
    var dialog = new MessageDialog("You have unsaved changes. Continue?", "Warning");
    dialog.Commands.Add(new UICommand("OK", handler));
    dialog.Commands.Add(new UICommand("Cancel", handler));
    dialog.ShowAsync();
}

private void handler(IUICommand command)
{
    System.Diagnostics.Debug.WriteLine($"The user clicked {command.Label}");
}

MessageDialog screenshot

如果您需要更多自定义内容,那么您需要继承ContentDialog类。右键单击解决方案资源管理器中的项目>添加> New Item,然后从列表中选择Content Dialog。您可以使用XAML编辑器自定义对话框的外观,选择要显示的按钮等。

<ContentDialog
    x:Class="App9.MyDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App9"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    PrimaryButtonText="OK"
    SecondaryButtonText="Cancel">

    <Grid>
        <TextBlock TextWrapping="Wrap">
            <Run>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquet iaculis sapien, in lacinia sem auctor quis.
                Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam finibus eu sapien quis
                ultricies. In tristique nec magna quis fermentum. Pellentesque elit urna, tincidunt sit amet suscipit non, lacinia
                vitae orci. Etiam vel tortor neque.
            </Run>
            <LineBreak/>
            <LineBreak/>
            <Run>
                Integer dictum tortor at quam feugiat, vel eleifend neque accumsan. Maecenas
                pellentesque, augue sed vulputate placerat, erat urna aliquam erat, a posuere mi massa vitae velit. Donec gravida
                dui quis tortor semper, vel efficitur leo ultricies. Sed tincidunt ultrices dui, vitae congue purus venenatis a.
                Ut aliquam neque leo, a vehicula nisl fermentum elementum. In euismod, erat ac congue fermentum, dui nunc ultrices
                tellus, vitae fringilla risus felis at tortor.
            </Run>
        </TextBlock>
    </Grid>
</ContentDialog>

您可以显示如下对话框:

private async Task displayDialogAsync()
{
    var dialog = new MyDialog();  // Whatever you named your ContentDialog subclass
    var result = await dialog.ShowAsync();

    if (result == ContentDialogResult.Primary)
    {
        // User clicked OK
    }
    else if (result == ContentDialogResult.Secondary)
    {
        // User clicked Cancel
    }
}

ContentDialog screenshot

答案 1 :(得分:0)

您不必将ContentDialog子类化以使用它:

ContentDialog CD_Popup = new ContentDialog();

CD_Popup.Title = "Title";
CD_Popup.Content = "Content";
CD_Popup.PrimaryButtonText = "Primary button text";
CD_Popup.PrimaryButtonClick += (sender, args) =>
{
    // Primary button method
};
CD_Popup.SecondaryButtonText = "Secondary button text";
CD_Popup.SecondaryButtonClick += (sender, args) =>
{
    // Secondary button method
};

await CD_Popup.ShowAsync();