MVVM Toolkit - 如果 WCF 服务失败,如何获取 MessageBox 以显示回调中的错误
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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