2011-02-24 8 views
1

Silverlightプロジェクトでは、Service Reference:DataService(ASP.NETプロジェクトで実行されるサービス)を通じて暗号化された文字列を取ります。暗号化された文字列を取得するためのTransactionServices.csからSilverlightの面白いサービスの動作

方法は次のとおりです。完成に

public void GetEncryptedString(string original) 
    { 
     DataService.DataServiceClient dataSvc = WebServiceHelper.Create(); 
     dataSvc.GetEncryptedStringCompleted += new EventHandler<SpendAnalyzer.DataService.GetEncryptedStringCompletedEventArgs>(dataSvc_GetEncryptedStringCompleted); 
     dataSvc.GetEncryptedStringAsync(original); 
    } 

、(空の値で初期化された)VARをencodedStringに結果を置く:

void dataSvc_GetEncryptedStringCompleted(object sender, SpendAnalyzer.DataService.GetEncryptedStringCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      try 
      { 
       if (e.Result == null) return; 
       this.encodedString = e.Result; 
      } 
      catch (Exception ex) 
      { 
       Logger.Error("TransactionService.cs: dataSvc_GetEncryptedStringCompleted: {0} - {1}", 
        ex.Message, ex.StackTrace); 
       MessageBox.Show(ex.ToString()); 
      } 
     } 
    } 

今度は、MainPage.xamlから次のようにエンコードされた文字列を取得したいと考えています:

TransactionService ts = new TransactionService(); 
        ts.GetEncryptedString(url); 
        Console.WriteLine(ts.encodedString); 

私はなぜts.encodedStringが空であるかを調べる。デバッグを行うと、実際には空の状態になり、空のdataSvc_GetEncryptedStringCompletedに移動して結果を取得して入力します。

私が間違ったことを指摘できますか? encodedStringがフェッチされるのを待つ方法はありますか?

ありがとうございます。

答えて

0

ts.GetEncryptedString(url);に電話すると、非同期操作を開始したばかりです。そのため、アクセスしている値はコールバックメソッドでのみ設定されます。

ただし、値がコールバックによって変更される前にアクセスします。

私は意志を使用しています解決策はfolowingのようになります。

はGetEncryptedStringメソッドシグネチャを再定義します。

public void GetEncryptedString(string original, Action callback) 
    { 
     DataService.DataServiceClient dataSvc = WebServiceHelper.Create(); 
     dataSvc.GetEncryptedStringCompleted += (o,e) => 
{ 
dataSvc_GetEncryptedStringCompleted(o,e); 
callback(); 
}    
     dataSvc.GetEncryptedStringAsync(original); 
    } 

はこのようにそれを呼び出します。

ts.GetEncryptedString(URL、OtherLogicDependantOnResult)。

OtherLogicDependantOnResultは

void OtherLogicDependantOnResult() 
{ 
//... Code 
} 
+0

であり、それは同期させる方法はありますか?私はそれが暗号化されているので、コールバックによってロードされる値を待つことができるURLにリダイレクトしたいですか? –

+0

私はしばらく解決策を探していましたが、それに慣れました。とにかく、この呼び出しを同期させることができます。 http://blog.benday.com/archive/2010/05/15/23277.aspx – v00d00

+0

ありがとうございます。ラムダ式を使用することが重要だと思われます! –

関連する問題