2017-01-04 5 views
4

私のapp.xaml.csに私は新しいページを作成します。Xamarin.Forms.Init()を呼び出さなければなりません。それを使用する前に

public App() 
    { 
     InitializeComponent(); 
     MainPage = new NavigationPage(new WrapLayoutPage()); 
    } 

このページでは、DependencyServiceを使用していくつかのタスクを実行する静的クラスを呼び出します。

エラーがスローライン:

var tmpTable = SqLiteHelper.GetItem<TableX>("someId"); 

にSqLiteHelper:それが何らかの形で関連するのです

You MUST call Xamarin.Forms.Init(); prior to using it

public static class SqLiteHelper 
{ 
    private static readonly SQLiteConnection DatabaseConnection = DependencyService.Get<ISqLite>().GetConnection(); 
    private static readonly object Locker = new object(); 

    public static DbObjectV3 GetItem<T>(Guid inId) where T : DbObjectV3, new() 
    { 
     lock (Locker) 
     { 
      var tmpItem = DatabaseConnection.Table<T>().FirstOrDefault(inItem => inItem.Id == inId); 
      tmpItem.IsNewObject = false; 
      return tmpItem; 
     } 
    } 
} 

これは私のInnerExceptionとTypeInitializationExceptionをスローします静的ヘルパークラスは、その呼び出しの前に、私は何の問題もなくDependencyServiceを使用することができます。

私はスプラッシュ画面を使用しています。このクラスでは、私はDependencyServiceに依存するスタートアップ作業を行います。

スプラッシュ:

[Activity(Theme = "@style/MyTheme.Splash", NoHistory = true, MainLauncher = true)] 
    public class SplashScreen : Activity 
    { 
     static readonly string TAG = "X:" + typeof(SplashScreen).Name; 

     public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState) 
     { 
      base.OnCreate(savedInstanceState, persistentState); 
      Log.Debug(TAG, "SplashActivity.OnCreate"); 
     } 
} 

マイMainActivity:

[Activity(Label = "FrameworkForms", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, Theme = "@style/MainActivityTheme", MainLauncher = false)] 
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
    { 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      Xamarin.Forms.Forms.Init(this, bundle); 
      App.ScreenWidth = (double)(Resources.DisplayMetrics.WidthPixels/Resources.DisplayMetrics.Density); 
      LoadApplication(new App()); 
     } 
    } 

今すぐFormsAppCompatActivityにスプラッシュ画面に活動を変更した後、私は別のエラーを取得します。

Call Forms.Init() before Hide Keyboard

ここには何がありますか?

+0

あなたは_Xamarin.Forms_ライブラリの同じバージョンをAndroidおよび共有プロジェクトに持っていることを確認できますか? –

+0

@PrashantCです。ちょうど最新バージョン(2.3.3.175)にアップグレードしました – greenhoorn

+0

これはむしろ変です!これは新しいプロジェクトですか?新しいプロジェクトでそれは起こりますか? [診断ビルドの出力]を共有できますか(https://developer.xamarin.com/guides/android/troubleshooting/troubleshooting/#Diagnostic_MSBuild_Output) –

答えて

3

これはかなり不幸です。 SplashScreenで間違ったOnCreate()メソッドを使用しました。

私はにスプラッシュ画面を変更:残念ながら、スプラッシュ画面の作成についてXamarinのドキュメントは、2つのパラメータでのOnCreate()メソッドを使用しています

[Activity(Theme = "@style/MyTheme.Splash", NoHistory = true, MainLauncher = true)] 
    public class SplashScreen : Activity 
    { 
     static readonly string TAG = "X:" + typeof(SplashScreen).Name; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      Xamarin.Forms.Forms.Init(this, savedInstanceState); 
      Log.Debug(TAG, "SplashActivity.OnCreate"); 
     } 


     protected override void OnResume() 
     { 
      base.OnResume(); 

      Task tmpStartupWork = new Task(() => 
      { 
       Log.Debug(TAG, "Performing some startup work that takes a bit of time."); 
       StartUpTasks.InitializeDatabaseCreation(); 
       Log.Debug(TAG, "Working in the background - important stuff."); 
      }); 

      tmpStartupWork.ContinueWith(inT => 
      { 
       Log.Debug(TAG, "Work is finished - start MainActivity."); 
       StartActivity(new Intent(Application.Context, typeof(MainActivity))); 
      }, TaskScheduler.FromCurrentSynchronizationContext()); 

      tmpStartupWork.Start(); 
     } 
    } 

+0

paramatersが付いていないものは、使用していますが、使用していないものです。 –

0

私もこの間違いを犯しました。エラーの原因は、私の名前空間を変更したいということでした。

public partial class AppDelegate : 
     global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate 

から私はそれを変更:

using Xamarin.Forms.Platform.iOS; 
public partial class AppDelegate : FormsApplicationDelegate 

その後、私は次のことを見た:

public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
{ 
    global::Xamarin.Forms.Forms.Init(); 

私は同様ということを調整することは有用であろう、と思った...何も考えずもっと正確にそして、私が書いた:

public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
{ 
    this.Init(); 

をしばらくして、私はミスを持って、苦労の原因を検索しなければなりませんでした。最後に見つけて修正しました。多分それは誰かを助けることができます:-)乾杯...

関連する問題