2016-06-29 4 views
0

XamarinフォームのAzureの通知ハブを使用してプッシュ通知を実装しようとしています。現在、Android用にGCMを使用しています。私はthis linkに従っています。しかし、チュートリアルの「ドロイドプロジェクトにプッシュ通知を追加する」のステップ5では、modifier static is not valid for this itemというエラーが表示されます。このステップの後、これは私のコードは次のようになります。修飾子staticはC#のこの項目エラーには無効です

protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     // Create a new instance field for this activity. 
     static MainActivity instance = null; 
     // Set the current instance of MainActivity. 
     instance = this; 

     global::Xamarin.Forms.Forms.Init (this, bundle); 
     Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init(); 
     LoadApplication (new App()); 
     try 
     { 
      // Check to ensure everything's setup right 
      GcmClient.CheckDevice(this); 
      GcmClient.CheckManifest(this); 

      // Register for push notifications 
      System.Diagnostics.Debug.WriteLine("Registering..."); 
      GcmClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS); 
     } 
     catch (Java.Net.MalformedURLException) 
     { 
      CreateAndShowDialog("There was an error creating the Mobile Service. Verify the URL", "Error"); 
     } 
     catch (Exception e) 
     { 
      CreateAndShowDialog(e.Message, "Error"); 
     } 
     private void CreateAndShowDialog(String message, String title) 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     builder.SetMessage(message); 
     builder.SetTitle(title); 
     builder.Create().Show(); 
    } 
    // Return the current activity instance. 
    public static MainActivity CurrentActivity 
    { 
     get 
     { 
      return instance; 
     } 
    } 
} 

私はC#とXamarinフォームに新しいですし、私が間違ってつもり場所を把握することはできません。

+0

基本的なC#の構文を学んでください...それはコンパイル時のエラー(つまり、C#の基本を知らないことを意味する)のように思えますし、正確に何が間違っているかを示します... – Selvin

+1

[ C#は静的ローカル変数の使用をサポートしていますか?](http://stackoverflow.com/questions/2393156/does-c-sharp-support-the-use-of-static-local-variables) – Selvin

答えて

2

関数外で静的変数を宣言する必要があります。

private static MainActivity instance = null; 
// Return the current activity instance. 
public static MainActivity CurrentActivity 
{ 
    get 
    { 
     return instance; 
    } 
} 

そして単にinstance = this;OnCreate内部。

+0

静的変数をOnCreateメソッドはエラーを削除しました。しかし、その後、次のメソッドの最後に括弧 '}'が出ます:catch(Exception e) { CreateAndShowDialog(e.Message、 "Error"); } – frank

+0

'Private CreateAndShowDialog(String message、String title)'の行のように、 'OnCreate'メソッドを閉じるには'} 'を閉じる必要があります。 – tsandy

+1

ええ、ありがとうございました。それが助けになりました。私はいつかこれで固まった。ありがとう、大きな時間! – frank

0

問題は、メソッドの内部に静的変数を作成しようとしていることです。そのリンクの例をもう一度見てみると、変数がメソッドの外にあることに気付くでしょう。

関連する問題