gpt4 book ai didi

c# - UWP 中带有 3 个以上按钮的自定义内容对话框

转载 作者:太空狗 更新时间:2023-10-29 22:05:06 26 4
gpt4 key购买 nike

我想显示一个内容对话框,其中包含的内容不仅仅是传统的主要和次要结果。由于我无法覆盖 ContentDialogResult 枚举并向该属性添加选项,看来我唯一的选择可能是创建我自己的自定义控件,其工作方式与 ContentDialog 类似。

对于其他上下文:通常,人们可能会在计算机/应用程序操作期间看到一个对话框,当该操作是多余的时,即将文件复制到文件夹时,计算机通常会提供一个没有 2 个选项的对话框,但是4. -> “Yes to All”、“No to All”、“Yes”、“No”。我似乎找不到任何千篇一律的方法来利用这种看似常见的做法。
我想像这样使用它就像普通的内容对话框一样:

var dialog = new MyCustomContentDialog();
var result = dialog.ShowAsync();

然后像普通的 ContentDialog 一样返回一个枚举,但让它返回 4 个选项中的 1 个,而不仅仅是 2 个。

任何帮助或建议都会很棒。谢谢。

最佳答案

I'd like to display a content dialog box that has more than the traditional Primary and Secondary results.

ContentDialog有 2 个内置按钮(主要/辅助按钮),让用户响应对话框。如果您想要更多按钮让用户响应对话框,您应该能够通过将这些按钮包含在对话框的内容中来实现。

以下是一个简单示例,展示了如何创建和使用带有 3 个按钮的自定义对话框:

MyCustomContentDialog.xaml

<ContentDialog
x:Class="ContentDialogDemo01.MyCustomContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ContentDialogDemo01"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="dialog"
Title="Delete">

<!-- Content body -->
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,20">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="200" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="3" Text="Delete file A?" Margin="5" />
<Button Grid.Row="1" Content="Yes" x:Name="btn1" Click="btn1_Click" Margin="5,0" Width="100" />
<Button Grid.Row="1" Grid.Column="1" Content="No" x:Name="btn2" Click="btn2_Click" Margin="5,0" Width="100" />
<Button Grid.Row="1" Grid.Column="2" Content="Cancle" x:Name="btn3" Click="btn3_Click" Margin="5,0" Width="100" />
</Grid>
</ContentDialog>

MyCustomContentDialog.xaml.cs

namespace ContentDialogDemo01
{
// Define your own ContentDialogResult enum
public enum MyResult
{
Yes,
No,
Cancle,
Nothing
}

public sealed partial class MyCustomContentDialog : ContentDialog
{
public MyResult Result { get; set; }

public MyCustomContentDialog()
{
this.InitializeComponent();
this.Result = MyResult.Nothing;
}

// Handle the button clicks from dialog
private void btn1_Click(object sender, RoutedEventArgs e)
{
this.Result = MyResult.Yes;
// Close the dialog
dialog.Hide();
}

private void btn2_Click(object sender, RoutedEventArgs e)
{
this.Result = MyResult.No;
// Close the dialog
dialog.Hide();
}

private void btn3_Click(object sender, RoutedEventArgs e)
{
this.Result = MyResult.Cancle;
// Close the dialog
dialog.Hide();
}
}
}

这是显示自定义对话框并使用返回的自定义结果的代码:

private async void ShowDialog_Click(object sender, RoutedEventArgs e)
{
// Show the custom dialog
MyCustomContentDialog dialog = new MyCustomContentDialog();
await dialog.ShowAsync();

// Use the returned custom result
if (dialog.Result == MyResult.Yes)
{
DialogResult.Text = "Dialog result Yes.";
}
else if (dialog.Result == MyResult.Cancle)
{
DialogResult.Text = "Dialog result Canceled.";
}
else if (dialog.Result == MyResult.No)
{
DialogResult.Text = "Dialog result NO.";
}
}

这是 entire sample .以下是输出: enter image description here

关于c# - UWP 中带有 3 个以上按钮的自定义内容对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37738128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com