2017-12-05 67 views
0

私はこのXamarin:廃止されたForms.ContextとAlertDialog.Builder

 AlertDialog.Builder b = new AlertDialog.Builder(Forms.Context); 

ようにそれを作成するために使用され、それがうまく働きました。しかしForms.Contextは、私は、代替コンストラクタ

 AlertDialog.Builder b = new AlertDialog.Builder(Android.App.Application.Context, Resource.Style.Theme_AppCompat_Light); 

を試してみましたが、それは

Android.Views.WindowManagerBadTokenException: Unable to add window -- token null is not for an application 
でクラッシュし、今私は今では

Android.Content.Res.Resources+NotFoundException: Resource ID #0x0 

でクラッシュし

 AlertDialog.Builder b = new AlertDialog.Builder(Android.App.Application.Context); 

にそれを変更廃止されました

だからどのようなc ontext XF2.5のAlertDialog.Builder()に渡しますか?私はこれのためのサードパーティのライブラリをインストールするような気がしません。

+0

MainActivity – cvanbeek

答えて

0

一つの方法は、より多くの詳細とこれを達成する他の方法については、この

AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.Instance); 

ようなあなたのコード内でこの

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
{ 
    internal static MainActivity Instance { get; private set; } 

    protected override void OnCreate(Bundle bundle) 
    { 
     ... 
     global::Xamarin.Forms.Forms.Init(this, bundle); 
     Instance = this; 
     LoadApplication(new App()); 
    } 
} 

のように主な活動に言及、それを静的変数を作成することsee David Britchs blog postです

+0

あなたのリンクから引用してみてください: "Xamarin.Forms.Forms.Contextはグローバルであるため廃止とマークされています理想的ではないアクティビティへの忠実な参照。 "したがって、グローバルな静的な静的なものを別のものに置き換える利点は何ですか? –

関連する問題