简体   繁体   English

Windows Phone 8.1 MessageDialog结果

[英]Windows Phone 8.1 MessageDialog results

I have a problem with getting selected option from MessageDialog in Windows Phone 8.1. 我在从Windows Phone 8.1的MessageDialog中选择选项时遇到问题。 What I want to do is to wait for user to choose the option and then get the chosen option and process it. 我要做的是等待用户选择选项,然后获取所选的选项并进行处理。

I do this with: 我这样做:

... initializing MessageDialog object called dialog ...
answer = (int)dialog.ShowAsync().GetResults().Id

However answer variable does not get assigned since GetResults returns immediately without waiting for user action and returns null. 但是, answer变量未分配,因为GetResults立即返回而无需等待用户操作,并且返回null。

I have to get result synchronously because this code is inside a property, and more importantly inside a catch block. 我必须同步获取结果,因为此代码在属性内,更重要的是在catch块内。

You need to wait for the task to complete before GetResults will have a valid result. 您需要等待任务完成,GetResults才会具有有效结果。 The easy way is to use await to wait for the dialog to finish: 一种简单的方法是使用await等待对话框完成:

var cmd = await dialog.ShowAsync();
answer = (int)cmd.Id;

You can't call an async function in a property and you can't block the UI thread to make the MessageDialog synchronous. 您不能在属性中调用异步函数,也不能阻塞UI线程以使MessageDialog同步。

Instead return a stub answer and call another function to get the async result. 而是返回一个存根答案并调用另一个函数以获取异步结果。 When the result is available at the property and fire a change notification. 当该结果在属性中可用时,并发出更改通知。

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

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