简体   繁体   中英

How to add PrimaryButtonCommad to ContentDialog in WindowsPhone 8.1?

I am showing a TextBox in ContentDialog and when i press ok i want to get the value of textbox and call a method. I can't find anything related to it. This is my code.

 var box = new ContentDialog()
                    {
                            Title = "File Name",                                
                            Content = fileName,
                            PrimaryButtonText = "Ok",
                            PrimaryButtonCommand = ,
                            SecondaryButtonText = "Cancel"
                    };

                    await box.ShowAsync(); 

PrimaryButtonCommand is a property and you can put there a object which has an ICommand interface. If you had added a BasicPage to your project, then VS should also add Common folder with some templates, there you will find a RelayCommand class, which you can use for your purpose, a sample can look like this:

private async void secondBtn_Click(object sender, RoutedEventArgs e)
{
    var box = new ContentDialog()
            {
                Title = "File Name",
                Content = fileName,
                PrimaryButtonText = "Ok",
                PrimaryButtonCommand = new RelayCommand(myAction),
                SecondaryButtonText = "Cancel"
            };
    await box.ShowAsync();
}

private async void myAction()
{
    await (new MessageDialog("User clicked ok")).ShowAsync();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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