Question:
How to display a message directly from MainPage, I'm using MessageDialog, according to the code below:
namespace App4
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var saida = new MessageDialog("Exibir Mensagem");
//Debug.WriteLine(saida);
await saida.ShowAsync();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
The error is on this line:
await saida.ShowAsync();
Answer:
The await
keyword
can only be used in methods that have the async
keyword
. However, the builders do not allow the async
keyword
, only the methods.
In your case I recommend that you use the event loaded
to run its features:
namespace App4
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
this.NavigationCacheMode = NavigationCacheMode.Required;
}
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var saida = new MessageDialog("Exibir Mensagem");
//Debug.WriteLine(saida);
await saida.ShowAsync();
}
More information about the await operator: MSDN|await (C# Reference)