Converting Asynchronous to Synchronous call in UI thread
I'm trying to implement IView for MVVM design pattern, which allows ViewModel to interact with the user using IView implemented class. The IView interface has functions such as Prompt, Alert & Confirm. I have three implementation of IView interface: CommandLineInteraction, WPFInteraction & TelerikInteraction. The first two are similar in behavior (i.e., they are synchronous). The third one works asynchronously.
I want TelerikInteraction to work synchronously. That means, the code following the invocation of RadWindow.Confirm() or RadWindow.Prompt() should wait until the user interacts.
Below is code snippet of all the three implementation:
//CommandLine Implementation
public CustomConfirmResult Confirm(string message) {
Console.WriteLine(message);
Console.WriteLine("[Y]es [N]o");
string s = Console.ReadLine();
if(s == y || s == Y)
return CustomConfirmResult.Yes;
else
return CustomConfirmResult.No;
}
//Windows Implementation
public CustomConfirmResult Confirm(string message) {
MessageBoxResult mbr = MessageBox.Show(message, "", MessageBoxButton.OKCancel);
if(mbr == MessageBoxResult.OK)
return CustomConfirmResult.Yes;
else
return CustomConfirmResult.No;
}
//Telerik Implementation
public CustomConfirmResult Confirm(string message) {
Custom开发者_开发问答ConfirmResult result;
RadWindow.Confirm(new DialogParameters{
Content=message,
Closed = (o1, e1) =>{
if(e1.DialogResult == true)
result = CustomConfirmResult.Yes;
else
result = CustomConfirmResult.No;
}
});
return result; //Executed before user interacts with the confirm dialog
}
How do I make these implementations similar in behavior?
Thanks,
Sunil Kumar
Silverlight is designed to work asynchronously. Trying to make it work synchronously will restrict the responsiveness of your product.
You should instead stop using MessageBox and move to a fully Async coding model instead.
Having helper methods that takes onCancel and onConfirm delegates or actions (or onYes, onNo or whatever you prefer) is the easiest way to code up the sort of question & answer situation you are after.
精彩评论