C# 在UI线程中将异步调用转换为同步调用
我正在尝试实现IViewforMVVM设计模式,该模式允许ViewModel使用IView实现的类与用户交互。IView界面具有提示、提醒、确认等功能。我有三个IView接口的实现:CommandLineInteraction、WPFinInteraction和TelerikinInteraction。前两个在行为上相似(即,它们是同步的)。第三个异步工作 我希望远程交互同步工作。这意味着,调用RadWindow.Confirm()或RadWindow.Prompt()之后的代码应该等待用户交互 以下是所有三种实现的代码片段:C# 在UI线程中将异步调用转换为同步调用,c#,wpf,silverlight,telerik-window,C#,Wpf,Silverlight,Telerik Window,我正在尝试实现IViewforMVVM设计模式,该模式允许ViewModel使用IView实现的类与用户交互。IView界面具有提示、提醒、确认等功能。我有三个IView接口的实现:CommandLineInteraction、WPFinInteraction和TelerikinInteraction。前两个在行为上相似(即,它们是同步的)。第三个异步工作 我希望远程交互同步工作。这意味着,调用RadWindow.Confirm()或RadWindow.Prompt()之后的代码应该等待用户交互
//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) {
CustomConfirmResult 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
}
如何使这些实现在行为上相似
谢谢
Sunil KumarSilverlight设计为异步工作。试图使其同步工作将限制产品的响应能力 您应该停止使用MessageBox,转而使用完全异步的编码模型
让助手方法接受onCancel和onConfirm委托或操作(或onYes、onNo或您喜欢的任何操作)是编写您所追求的问答情况的最简单方法。Silverlight设计为异步工作。试图使其同步工作将限制产品的响应能力 您应该停止使用MessageBox,转而使用完全异步的编码模型 让助手方法接受onCancel和onConfirm委托或操作(或onYes、onNo或任何您喜欢的操作)是编写您所追求的问答情况的最简单方法