MVVM Toolkit - 如果 WCF 服务失败,如何获取 MessageBox 以显示回调中的错误

发布于 2024-10-06 22:03:22 字数 1883 浏览 0 评论 0原文

我正在使用 MVVM Toolkit 构建我的 WP7 应用程序,并且尝试在 WCF 服务失败时获取 MessageBox 以显示错误。

这是我在 ServiceHelper 层中的代码:

        public void GetRandomBibleVerseById(Action<Bible, Exception> callback)
    {
        var client = new StoneFalconClient();

        client.GetRandomBibleVerseByIdCompleted += (s, e) =>
            {
                var userCallback = e.UserState as Action<Bible, Exception>;

                if (userCallback == null)
                {
                    return;
                }

                if (e.Error != null)
                {
                    userCallback(null, e.Error);
                    return;
                }

                userCallback(e.Result, null);
            };

        client.GetRandomBibleVerseByIdAsync(callback);
    }

我尝试通过执行类似的操作在服务调用周围放置一个 try/catch,但我没有收到任何显示错误的消息框。这是我的 MainViewModel:

        public MainViewModel()
    {
        if (IsInDesignMode)
        {
            BibleVerse = LoadDebugVerse();
        }
        else
        {
            try
            {
                ServiceHelper helper = new ServiceHelper();

                helper.GetRandomBibleVerseById((bibleVerse, error) =>
                {
                    Bible displayVerse = LoadDisplayVerse(bibleVerse);
                    BibleVerse = displayVerse;

                    TestamentBookChapterVerse = displayVerse.Testament + ": " + displayVerse.Book + " " + displayVerse.Chapter + ":" + displayVerse.Verse; ;
                });
            }
            catch (Exception ex)
            {
                ErrorMessage = new RelayCommand<String>(m => MessageBox.Show("Error: Click refresh to try again"));
            }
        }
    }

我将 xaml 页面中的 xaml 绑定到 viewModel。我怀疑我不应该使用 try/caatch 而是检查 e.Error 但我不知道该怎么做。

任何帮助将不胜感激,我一直在寻找答案但没有运气。

I am using the MVVM Toolkit to build my WP7 apps and I am trying to get a MessageBox to display an error if the WCF service fails.

Here is the code I have in my ServiceHelper layer:

        public void GetRandomBibleVerseById(Action<Bible, Exception> callback)
    {
        var client = new StoneFalconClient();

        client.GetRandomBibleVerseByIdCompleted += (s, e) =>
            {
                var userCallback = e.UserState as Action<Bible, Exception>;

                if (userCallback == null)
                {
                    return;
                }

                if (e.Error != null)
                {
                    userCallback(null, e.Error);
                    return;
                }

                userCallback(e.Result, null);
            };

        client.GetRandomBibleVerseByIdAsync(callback);
    }

I tried placing a try/catch around the service call by doing something like this but I don't get any message box to show up with the error. Here is my MainViewModel:

        public MainViewModel()
    {
        if (IsInDesignMode)
        {
            BibleVerse = LoadDebugVerse();
        }
        else
        {
            try
            {
                ServiceHelper helper = new ServiceHelper();

                helper.GetRandomBibleVerseById((bibleVerse, error) =>
                {
                    Bible displayVerse = LoadDisplayVerse(bibleVerse);
                    BibleVerse = displayVerse;

                    TestamentBookChapterVerse = displayVerse.Testament + ": " + displayVerse.Book + " " + displayVerse.Chapter + ":" + displayVerse.Verse; ;
                });
            }
            catch (Exception ex)
            {
                ErrorMessage = new RelayCommand<String>(m => MessageBox.Show("Error: Click refresh to try again"));
            }
        }
    }

I am binding the xaml in the xaml page to the viewModel. I suspect that I shouldn't be using the try/caatch but instead inspecting e.Error but I have no idea how to do that.

Any help would be appreciated, I have been searching forever for an answer with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-10-13 22:03:22

RJ,

您的客户端对象(类型为StoneFalconClient)应该有一个AsyncCompleted事件,您可以为其注册处理程序。在您的 OnAsyncCompleted 处理程序中,EventArgs 参数中应该有一个 Error 字段。

祝你好运
吉姆·麦柯迪

RJ,

Your client object (of type StoneFalconClient) should have an AsyncCompleted event that you can register a handler for. In your OnAsyncCompleted handler, there should be an Error field in the EventArgs parameter.

Good luck
Jim McCurdy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文