2016-10-26 21 views
0

私はXamarin.FormsとWPF(このように:https://github.com/C0D3Name/XamFormsWpf)とMVVMライトを組み合わせて動作するクロスプラットフォームアプリケーションを作ろうとしています。MVVM Light for XamarinフォームとWPF

MVVM Lightは私には新しく、私は何をしたいのか明確なチュートリアルは見つかりませんでした。 Xamarin.Forms上でDependencyInjectionが行われます

SQLiteConnection database = DependencyService.Get<ISQLite>().GetConnection("db.sqlite"); 

これはMVVMのライトでどのように行われますか? ISQLiteの異なるプラットフォーム実装をパラメータとして渡す必要がありますか?

私はすでに私のPCLでViewModelLocatorを作成しました:

public class ViewModelLocator 
{ 
    /// <summary> 
    /// Register all the used ViewModels, Services et. al. witht the IoC Container 
    /// </summary> 
    public ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     SimpleIoc.Default.Register<MainViewModel>(); 

     // My DataService is using the connection from ISQlite 
     SimpleIoc.Default.Register<IDataService, DataService>(); 
    } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", 
     "CA1822:MarkMembersAsStatic", 
     Justification = "This non-static member is needed for data binding purposes.")] 
    public MainViewModel Main 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<MainViewModel>(); 
     } 
    } 
} 

public interface ISQLite 
{ 
    SQLiteConnection GetConnection(string sqliteFilename); 
} 

は、私の知る限りSimpleIocを理解できるように、私は具体的な実装にインターフェイスを登録する必要がありますが、どのように私のDataServiceは正しいISQLite実装について知っているん?

私の質問が分かりやすいと思います。公式ドキュメントから

答えて

0

最後に、私は実用的な解決策を見つけました。 (非同期)Sqliteを-接続が私のViewModelLocator

public class App : Application 
{ 
    private static ViewModelLocator locator; 
    public static ViewModelLocator Locator 
    { 
     get 
     { 
      if (locator == null) 
      { 
       var connection = DependencyService.Get<ISQLite>().GetConnection("db.sqlite"); 
       locator = new ViewModelLocator(connection); 
      } 
      return locator; 
     } 
    } 
} 

のためのパラメータとして使用...とロケータがDataServiceコンストラクタへの接続注入されています

public class ViewModelLocator 
{ 
    public ViewModelLocator(SQLiteConnectionWithLock connection) 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 
     SimpleIoc.Default.Register<IDataService>(() => new DataService(connection)); 

     // ViewModels 
     SimpleIoc.Default.Register<MainViewModel>(); 
    } 

    public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>(); 
} 
1

SimpleIoc - A very simple IOC container with basic functionality needed to register and resolve instances.

DependencyService - Xamarin.Forms allows developers to define behavior in platform-specific projects. DependencyService then finds the right platform implementation, allowing shared code to access the native functionality.

ですから、(例えばIDataService)あなたのPCL依存性注入のグラフを作成するためにSimpleIocを使用したいと思う

そして、 DependencyServiceを使用してプラットフォーム固有の機能を提供する必要があります。 DependencyServiceを使用する1つの例は、デバイス上にあるhtmlファイルをwebviewにロードすることです。 iOSとAndroidのアセットの場所は異なるため、ベースURLのプラットフォーム固有の実装を追加してから、pclでDependencyServiceを使用します。別の例はIOです。

ISQliteのプラットフォーム固有の実装が必要な場合は、DependencyServiceを使用する必要があります。そうでない場合は、SimpleIocを使用して依存関係グラフにISQliteインターフェイスの具体的な実装を追加する(またはしない)ことができます。

希望に役立ちます。

Akavacheもご覧ください。 Xamarin.Formsプロジェクトで大きな成功を収めています。

関連する問題