Answered by:
[W8.1][C#]IValueConverter can not use async/Task

Question
-
I want show MessageDialog in ConvertBack.
public async Task<object> ConvertBack(object value, Type targetType, object parameter, string language) { MessageDialog dialog = new MessageDialog("Message"); await dialog.ShowAsync(); return value.ToString(); }
This will cause an error.- Edited by Franklin ChenMicrosoft employee, Moderator Tuesday, May 12, 2015 3:17 AM Tagged Subject
Monday, May 11, 2015 4:27 PM
Answers
-
You can't do that in the IValueConverter. The IValueConverter runs synchronously and needs to return promptly.
Instead you can look for changes elsewhere and then offer to confirm or revert. I would recommend not using a modal control such as a MessageDialog for confirmation but to use a more subtle control elsewhere. Exact details will depend on the scenario and the sort of damage that an incorrect value can cause.
- Marked as answer by oneonce Wednesday, May 13, 2015 2:31 AM
Wednesday, May 13, 2015 2:07 AMModerator
All replies
-
Hi Oneonce,
Can you explain what you are trying to achieve here?
The value converter is called in-line and the response is needed immediately, so an async method internal to Convert or ConvertBack doesn't make sense. The return from ConvertBack is passed directly to the source object which uses it immediately. You cannot convert a method to a Task without converting the caller to a Task, and in general you don't control the caller of the IValueConverter.
An async function works essentially as a chain of functions. It will return at the first await and then call the next section of code when the awaited Task completes. Since the caller of ConvertBack doesn't expect to await ConvertBack it won't wait for or ever receive the final value.ToString() line.
Tuesday, May 12, 2015 7:06 AMModerator -
Thanks for your reply!
I want to do more things in IValueConverter's method.
I have a textbox, and binding it to the property "public string UserName {get; set;}", binding mode is twoway.
when user change the text of textbox, i will show a MessageDialog for user to confirm the modify.
Tuesday, May 12, 2015 8:34 AM -
You can't do that in the IValueConverter. The IValueConverter runs synchronously and needs to return promptly.
Instead you can look for changes elsewhere and then offer to confirm or revert. I would recommend not using a modal control such as a MessageDialog for confirmation but to use a more subtle control elsewhere. Exact details will depend on the scenario and the sort of damage that an incorrect value can cause.
- Marked as answer by oneonce Wednesday, May 13, 2015 2:31 AM
Wednesday, May 13, 2015 2:07 AMModerator