【问题标题】:Converting Asynchronous to Synchronous call in UI thread在 UI 线程中将异步调用转换为同步调用
【发布时间】:2011-07-29 09:24:46
【问题描述】:

我正在尝试为 MVVM 设计模式实现 IView,它允许 ViewModel 使用 IView 实现的类与用户进行交互。 IView 界面具有Prompt、Alert 和Confirm 等功能。我有 IView 接口的三个实现:CommandLineInteraction、WPFInteraction 和 TelerikInteraction。前两个在行为上相似(即它们是同步的)。第三个异步工作。

我希望 TelerikInteraction 同步工作。这意味着,调用 RadWindow.Confirm() 或 RadWindow.Prompt() 之后的代码应该等到用户交互。

下面是所有三个实现的代码sn-p:

    //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
    }

如何使这些实现在行为上相似?

谢谢,

苏尼尔·库马尔

【问题讨论】:

    标签: c# wpf silverlight telerik-window


    【解决方案1】:

    Silverlight 旨在异步工作。尝试使其同步工作会限制产品的响应能力。

    您应该停止使用 MessageBox 并改用完全异步编码模型。

    拥有接受 onCancel 和 onConfirm 委托或操作(或 onYes、onNo 或任何您喜欢的任何东西)的辅助方法是编写您所追求的那种问答情况的最简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多