Home > database >  How to pass value from ContentDialog to Page in UWP app C#?
How to pass value from ContentDialog to Page in UWP app C#?

Time:04-15

I want to implement a simple ContentDialog which has a TextBox which takes the user's name. How can I pass that values to a Page in UWP??

CodePudding user response:

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

As Hans Kesting said, you could set properties for dialog and load it with dialog instance in the page. I will provide a binding way to pass value from ContentDialog to 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

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

CodePudding user response:

As far as your code is concerned, a ContentDialog is just a class. And a class can have properties.

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.

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).

  • Related