在消息对话框中输入文本? ContentDialog? [英] Text input in message dialog? ContentDialog?

查看:184
本文介绍了在消息对话框中输入文本? ContentDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是允许用户在Windows 10通用应用程序(忘记密码系统)中向MessageDialog中输入文本的最佳方法。根据我所做的研究,使用MessageDialog似乎无法做到这一点,但是可以通过ContentDialog来完成。到目前为止,我已经找到



页面:

 使用系统; 
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;

命名空间App1
{
公共密封局部类MainPage
{
public MainPage()
{
InitializeComponent();
已加载+ = MainPage_Loaded;
}

私有异步void MainPage_Loaded(对象发送者,RoutedEventArgs e)
{
var dialog1 = new ContentDialog1();
var result = await dialog1.ShowAsync();
if(结果== ContentDialogResult.Primary)
{
var text = dialog1.Text;
}
}
}
}

对话框(代码):

使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;

命名空间App1
{
公共密封子类ContentDialog1:ContentDialog
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
文本,typeof(字符串),typeof(ContentDialog1),新的PropertyMetadata(默认(字符串)));

public ContentDialog1()
{
InitializeComponent();
}

公共字符串Text
{
get {return(string)GetValue(TextProperty); }
set {SetValue(TextProperty,value); }
}

私有无效ContentDialog_PrimaryButtonClick(ContentDialog sender,ContentDialogBu​​ttonClickEventArgs args)
{
}

私有无效ContentDialog_SecondaryButtonClick(ContentDialog sender,ContentDialogBu​​ttonClickEventArgs 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
PrimaryButtonClick = ContentDialo g_PrimaryButtonClick
PrimaryButtonText = Button1
SecondaryButtonClick = ContentDialog_SecondaryButtonClick
SecondaryButtonText = Button2
mc:Ignorable = d>

< Grid>
< TextBox Text = {Binding ElementName = ContentDialog,Path = Text,Mode = TwoWay} />
< / Grid>
< / ContentDialog>


I am wondering what is the best way to allow a user to input text into a MessageDialog in a Windows 10 universal app.(Forgot password system). From the research I've done this doesn't seem possible with a MessageDialog but can be accomplished with a ContentDialog. So far I've found this site which explains roughly how to use the ContentDialog, but not with text input, and and this article on MSDN which does show how to use a textbox with a ContentDialog but the method shown seems quite complex to me.

So, does anyone know of any more simplistic way of doing this or is the MSDN way the simplest its going to get?

Thanks for any help

Nathan

解决方案

Yes, here's the strict minimum to achieve what you're looking for :

Page :

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;
            }
        }
    }
}

Dialog (code) :

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)
        {
        }
    }
}

Dialog (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>

这篇关于在消息对话框中输入文本? ContentDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆