简体   繁体   English

如何在 UWP 应用程序 C# 中将值从 ContentDialog 传递到页面?

[英]How to pass value from ContentDialog to Page in UWP app C#?

I want to implement a simple ContentDialog which has a TextBox which takes the user's name.我想实现一个简单的ContentDialog ,它有一个采用用户名的TextBox How can I pass that values to a Page in UWP??如何将该值传递给 UWP 中的页面?

As far as your code is concerned, a ContentDialog is just a class. And a class can have properties.就您的代码而言,ContentDialog 只是一个 class。而 class 可以具有属性。

So you can set those properties just before you show the dialog.因此,您可以在显示对话框之前设置这些属性。 And read them after the dialog is closed.并在对话框关闭后阅读它们。

So you will need to add a property to your ContentDialog that is filled from that textbox - which you can do "automatically" if you set up the databinding correctly.因此,您需要向从该文本框填充的 ContentDialog 添加一个属性 - 如果您正确设置了数据绑定,则可以“自动”执行此操作。

EDIT编辑
So instead of something like所以而不是像

if (await new MyDialog().ShowAsync() == DialogResult.Primary) ...

do something like做类似的事情

// first create an instance (this doesn't show anything yet)
var myDialog = new MyDialog();

// optionally set some properties
myDialog.Username = "some default value";

// now show that dialog
var res = await myDialog.ShowAsync();

// and when it is closed again ...
if (res == DialogResult.Primary)
{
   // read the properties of your dialog instance
   var theUsername = myDialog.Username;
   // and do something with 'theUsername' ...
}

and in the XAML of that dialog, bind that Username property to the Text of your textbox (that is the easiest way to make sure the property's value is updated with the latest state of your textbox).并在该对话框的 XAML 中,将该用户名属性绑定到文本框的文本(这是确保属性值更新为文本框的最新 state 的最简单方法)。

How to pass value from ContentDialog to Page in UWP app C#?如何在 UWP 应用程序 C# 中将值从 ContentDialog 传递到页面?

As Hans Kesting said, you could set properties for dialog and load it with dialog instance in the page.正如 Hans Kesting 所说,您可以设置对话框的属性并在页面中使用对话框实例加载它。 I will provide a binding way to pass value from ContentDialog to Page.我将提供一种绑定方式来将值从 ContentDialog 传递到 Page。

public sealed partial class LoginDialog : ContentDialog ,INotifyPropertyChanged
{
    public LoginDialog()
    {
        this.InitializeComponent();
    }

    private string _username;
    private string _password;

    public string UserNameValue { get { return _username; } set { _username = value; OnPropertyChanged(); } }


    public string PasswordValue { get { return _password; } set { _password = value; OnPropertyChanged(); } }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string name = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }


    private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
    }

    private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
    }
}

Xaml Code Xaml 代码

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBox
        x:Name="UserName"
        Grid.Row="0"
        Margin="0,0,0,12"
        Text="{x:Bind UserNameValue, Mode=TwoWay}" />
    <TextBox
        x:Name="Password"
        Grid.Row="1"
        Text="{x:Bind PasswordValue, Mode=TwoWay}" />
</Grid>

Usage用法

var dialog = new LoginDialog();
var res = await dialog.ShowAsync();
if (res == ContentDialogResult.Primary)
{
    var password = dialog.PasswordValue;
    var userName = dialog.UserNameValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM