我想知道允许用户在Windows 10通用应用程序中将文本输入到MessageDialog的最佳方式是什么。(忘记密码系统)。从我已经完成的研究来看,使用MessageDialog似乎无法做到这一点,但可以使用ContentDialog完成。到目前为止,我发现this网站大致解释了如何使用ContentDialog,但没有使用文本输入,而and this article on MSDN确实展示了如何使用带有ContentDialog的文本框但是显示的方法对我来说似乎很复杂。
那么,有没有人知道更简单的方法呢?或者MSDN最简单的方式是什么?
感谢您的帮助
森
答案 0 :(得分:16)
是的,这是达到您所需要的最低要求:
页面
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var dialog1 = new ContentDialog1();
var result = await dialog1.ShowAsync();
if (result == ContentDialogResult.Primary)
{
var text = dialog1.Text;
}
}
}
}
对话框(代码):
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class ContentDialog1 : ContentDialog
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof (string), typeof (ContentDialog1), new PropertyMetadata(default(string)));
public ContentDialog1()
{
InitializeComponent();
}
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
}
}
对话(XAML):
<ContentDialog x:Class="App1.ContentDialog1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="ContentDialog"
Title="TITLE"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
PrimaryButtonText="Button1"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick"
SecondaryButtonText="Button2"
mc:Ignorable="d">
<Grid>
<TextBox Text="{Binding ElementName=ContentDialog, Path=Text, Mode=TwoWay}" />
</Grid>
</ContentDialog>
答案 1 :(得分:13)
我使用此功能向用户请求文字:
private async Task<string> InputTextDialogAsync(string title)
{
TextBox inputTextBox = new TextBox();
inputTextBox.AcceptsReturn = false;
inputTextBox.Height = 32;
ContentDialog dialog = new ContentDialog();
dialog.Content = inputTextBox;
dialog.Title = title;
dialog.IsSecondaryButtonEnabled = true;
dialog.PrimaryButtonText = "Ok";
dialog.SecondaryButtonText = "Cancel";
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
return inputTextBox.Text;
else
return "";
}
及其用法:
string text = await InputTextDialogAsync("Title");
答案 2 :(得分:0)
代替Height = 32,使用此:
public static async Task<string> ShowAddDialogAsync(string title)
{
var inputTextBox = new TextBox { AcceptsReturn = false };
(inputTextBox as FrameworkElement).VerticalAlignment = VerticalAlignment.Bottom;
var dialog = new ContentDialog
{
Content = inputTextBox,
Title = title,
IsSecondaryButtonEnabled = true,
PrimaryButtonText = "Ok",
SecondaryButtonText = "Cancel"
};
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
return inputTextBox.Text;
else
return "";
}