zoukankan      html  css  js  c++  java
  • Silverlight WCF RIA服务(二十四)Silverlight 客户端 5

    在客户端处理错误 当我们在客户端检索或修改数据时,我们通常需要处理错误和对错误做出反应。通过WCF RIA Services,我们为数据操作提供一个回调方法来处理错误,并且在回调方法里检查错误。使用回调方法是必需的,因为调用数据操作都是异步的,比且异常也是异步抛出的
      

    在客户端处理错误
    当我们在客户端检索或修改数据时,我们通常需要处理错误和对错误做出反应。通过WCF RIA Services,我们为数据操作提供一个回调方法来处理错误,并且在回调方法里检查错误。使用回调方法是必需的,因为调用数据操作都是异步的,比且异常也是异步抛出的。默认下,对域操作中的所有错误都抛出一个异常。RIA Services为我们提供了处理错误的方式,并且可以指定框架不抛出异常。

    当装载数据时处理错误
    当从一个查询装载数据时,我们可以选择处理错误或忽略之。明确地,我们从如下选项中选择:

    1. 使用带有回调方法做为参数的Load方法。在回调方法内,处理错误,并调用MarkErrorAsHandled方法来指定不要抛出错误。

    2. 使用带一个名为thowOnError的Boolean参数的Load方法。当调用Load方法时,设置throwOnError为false来指定我们不想为查询错误抛出异常。

    3. 当Load方法没有回调参数和布尔参数时,任何查询错误都会导致一个未处理的异常。
    下面的示例演示了如何从查询载入数据,并指定一个回调方法来检测装载操作中的错误。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    private CustomerDomainContext _customerContext = new CustomerDomainContext();
     
    public MainPage()
    {
        InitializeComponent();
     
        LoadOperation<CUSTOMER> loadOp =this._customerContext.Load(this._customerContext.GetCustomersQuery(), OnLoadCompleted,null);
        CustomerGrid.ItemsSource = loadOp.Entities;
    }
     
    private void OnLoadCompleted(LoadOperation<CUSTOMER> lo)
    {
        if (lo.HasError)
        {
            MessageBox.Show(string.Format("Retrieving data failed: {0}", lo.Error.Message));
            lo.MarkErrorAsHandled();
        }
    }



    提交数据时处理错误
    当提交数据时,我们不能像Load方法那样选择关闭异常。所有提交数据时发生的错误都会导致一个异常。明确地,我们可以选择如下选项:

    1. 使用带回调参数的SubmitChanges方法。在回调方法里,处理错误并调用MarkErrorAsHandled方法来指定不要抛出异常。

    2. 使用SubmitChanges方法,所有在提交数据时发生的错误都导致一个异常。

    下面的示例演示了如何调用带有回调参数的SubmitChanges方法。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        _customerContext.SubmitChanges(OnSubmitCompleted, null);
    }
     
    private void RejectButton_Click(object sender, RoutedEventArgs e)
    {
        _customerContext.RejectChanges();
        CheckChanges();
    }
     
    private void CustomerGrid_RowEditEnded(object sender, DataGridRowEditEndedEventArgs e)
    {
        CheckChanges();
    }
     
    private void CheckChanges()
    {
        EntityChangeSet changeSet = _customerContext.EntityContainer.GetChanges();
        ChangeText.Text = changeSet.ToString();
     
        bool hasChanges = _customerContext.HasChanges;
        SaveButton.IsEnabled = hasChanges;
        RejectButton.IsEnabled = hasChanges;
    }
     
    private void OnSubmitCompleted(SubmitOperation so)
    {
        if (so.HasError)
        {
            MessageBox.Show(string.Format("Submit Failed: {0}", so.Error.Message));
            so.MarkErrorAsHandled();
        }
        CheckChanges();
    }



    调用操作时处理错误
    当调用一个操作时,我们有像提交数据时一样的选择。如下:

    1. 当调用操作时包含一个回调参数。在回调参数内处理错误,并调用MarkErrorAsHandled方法来指定不要抛出异常。

    2. 调用不包含回调参数的操作。所有在调用操作时发生的错误都会抛出异常。

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    InvokeOperation<INT> invokeOp = customerContext.GetLocalTemperature(selectedPostalCode, OnInvokeCompleted, null);
     
    private void OnInvokeCompleted(InvokeOperation<INT> invOp)
    {
      if (invOp.HasError)
      {
        MessageBox.Show(string.Format("Method Failed: {0}", invOp.Error.Message));
        invOp.MarkErrorAsHandled();
      }
      else
      {
        result = invokeOp.Value;
      }
    }



    验证服务处理错误
    AuthenticationService类允许我们在调用下面的方法时提供回调参数:
    1.LoadUser 2.Login 3.Logout 4.SaveUser
    在回调方法中,我们可以提供代码来处置从验证服务中来的错误。 下面的例子演示如何从登陆按钮的事件处理中调用Login方法。
     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    private void LoginButton_Click(object sender, RoutedEventArgs e)
    {
        LoginParameters lp = new LoginParameters(UserName.Text, Password.Password);
        WebContext.Current.Authentication.Login(lp, this.LoginOperation_Completed, null);
        LoginButton.IsEnabled = false;
        LoginResult.Text = "";
    }
     
    private void LoginOperation_Completed(LoginOperation lo)
    {
        if (lo.HasError)
        {
            LoginResult.Text = lo.Error.Message;
            LoginResult.Visibility = System.Windows.Visibility.Visible;
            lo.MarkErrorAsHandled();
        }
        else if (lo.LoginSuccess == false)
        {
            LoginResult.Text = "Login failed. Please check user name and password.";
            LoginResult.Visibility = System.Windows.Visibility.Visible;
        }
        else if (lo.LoginSuccess == true)
        {
            SetControlVisibility(true);
        }
        LoginButton.IsEnabled = true;
    }

     

    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    用YSLOW分析页面速度
    字节与字符的区别
    五小步大幅提高firefox页面加载速度【转载】
    Asp.netUpload(大文件上传) 终于找到一个可以用的了
    (续), 这个是我比较满意的
    共享一些变态的签名,希望不太OLD
    C#入门代码
    最后是所有的附件和一些他们的文章
    JavaScript日期处理函数大全
    加密解密Url的类
  • 原文地址:https://www.cnblogs.com/Areas/p/2172186.html
Copyright © 2011-2022 走看看