我在 Windows Phone 中有以下代码:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_LogIn_Click(object sender, RoutedEventArgs e)
{
Service1SoapClient web_service = new Service1SoapClient();
web_service.LogInAsync(TextBox_Username.Text, TextBox_Password.Password);
web_service.LogInCompleted += new EventHandler<LogInCompletedEventArgs>(login_complete);
}
private void login_complete(object obj, ClientWebService.LogInCompletedEventArgs e)
{
string answer = e.Result.ToString();
if (answer.Equals("Success") || answer.Equals("success"))
{
NavigationService.Navigate(new Uri("/Authenticated.xaml", UriKind.Relative));
}
else
{
MessageBox.Show("The log-in details are invalid!");
}
}
}
该代码利用 Web 服务将用户登录到系统中。登录系统正常工作。
我的问题是,我应该在哪里插入 try catch 语句以便在 Web 服务未运行时捕获异常?我在 button_click 事件处理程序中尝试无济于事,甚至在我得到结果时也在排队。