2017-07-24 87 views
4

我想知道在現有的Xamarin.Forms共享項目中實現Prism的有效方法。我相信其他人也想知道,如果他們能夠從Prism所提供的產品中獲益,而無需將他們現有的項目轉換爲PCL項目。我發現的所有現有示例都顯示了一個PCL項目(​​可能是出於很好的原因)。在現有的Xamarin.Forms共享應用程序中實現Prism

要嘗試在我的項目中實施棱鏡我安裝Prims.Unity.Forms nuget到每個平臺項目。 我試圖從PrismApplication繼承:

public partial class App : PrismApplication 
{ } 

但是,這是行不通的。應用程序類不允許我從不同的基類繼承。

添加以下行到我的Android項目並沒有幫助:

protected override void OnCreate(Bundle bundle) 
{  
    LoadApplication(new App(new AndroidInitializer())); 
} 

public class AndroidInitializer : IPlatformInitializer 
{ 
    public void RegisterTypes(IUnityContainer container) 
    { } 
} 

也許我缺少的東西,或嘗試一些在結構上是不可能的。

回答

10

是的,這是完全可能的。

首先,確保安裝了nuget軟件包。

例如,對於棱鏡和統一,你需要:

<package id="Prism.Core" version="6.3.0" ... /> 
    <package id="Prism.Forms" version="6.3.0" .../> 
    <package id="Prism.Unity.Forms" version="6.3.0" ... /> 
    <package id="Unity" version="4.0.1" ... /> 

添加缺少的文件夾(只爲序)

enter image description here

並移動現有頁面的Views文件夾, 但記住要調整名稱空間或者你的綁定不起作用。

更改應用程序基類

記住改變代碼和XAML應用基本類型。

using Prism.Unity; 
using Xamarin.Forms; 

namespace XamPrismShared 
{ 
    public partial class App : PrismApplication 
    { 
     public App (IPlatformInitializer platformInitializer):base(platformInitializer) 
     {      
     } 

    } 
} 

設置的第一頁和視圖模型

實施OnInitialized和RegisterTypes。請記住,您需要將每個您想要用於Prism的類型註冊爲頁面。

using Prism.Unity; 
using Xamarin.Forms; 

namespace XamPrismShared 
{ 
    public partial class App : PrismApplication 
    { 
     public App (IPlatformInitializer platformInitializer):base(platformInitializer) 
     {      
     } 

     protected override void OnInitialized() 
     { 
      InitializeComponent(); 
      NavigationService.NavigateAsync("MainPage"); 
     } 

     protected override void RegisterTypes() 
     { 
      Container.RegisterTypeForNavigation<NavigationPage>(); 
      Container.RegisterTypeForNavigation<MainPage>();    
     }     
    } 
} 

如果現有的網頁

添加ViewModelLocator.AutowireViewModel="True"到你存在的意見,以使棱鏡自動綁定各自的視圖模型

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
      prism:ViewModelLocator.AutowireViewModel="True" x:Class="x:Class="XamPrismShared.Views.MainPage"" Title="MainPage"> 
    <Label Text="{Binding Title}" 
      VerticalOptions="Center" 
      HorizontalOptions="Center" /> 
</ContentPage> 

添加缺少的視圖模型

enter image description here

using Prism.Mvvm; 

namespace XamPrismShared.ViewModels 
{ 
    public class MainPageViewModel : BindableBase 
    { 
     public MainPageViewModel() 
     { 
      Title = "Hi from Prism."; 
     } 

     public string Title { get; set; } 
    } 
} 

添加平臺初始化各個平臺項目

添加缺少的平臺初始化和修復Xamarin.Forms負荷。

  • 對於Android,
 
    using Android.App; 
    using Android.Content.PM; 
    using Android.OS; 
    using Microsoft.Practices.Unity; 
    using Prism.Unity; 

    namespace XamPrismShared.Droid 
    { 
     [Activity (Label = "XamPrismShared", Icon = "@drawable/icon", Theme="@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
     public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
     { 
      protected override void OnCreate (Bundle bundle) 
      { 
       TabLayoutResource = Resource.Layout.Tabbar; 
       ToolbarResource = Resource.Layout.Toolbar; 

       base.OnCreate (bundle); 

       global::Xamarin.Forms.Forms.Init (this, bundle); 
       LoadApplication (new XamPrismShared.App(new AndroidPlatformInitializer())); 
      } 
     } 

     public class AndroidPlatformInitializer : IPlatformInitializer 
     { 
      public void RegisterTypes(IUnityContainer container) 
      { 

      } 
     } 
    } 

  • 對於iOS,
 

    using Foundation; 
    using Microsoft.Practices.Unity; 
    using Prism.Unity; 
    using UIKit; 

    namespace XamPrismShared.iOS 
    { 
     [Register("AppDelegate")] 
     public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate 
     { 
      public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
      { 
       global::Xamarin.Forms.Forms.Init(); 
       LoadApplication (new XamPrismShared.App(new iOSPlatformInitializer())); 

       return base.FinishedLaunching (app, options); 
      } 
     } 

     public class iOSPlatformInitializer : IPlatformInitializer 
     { 
      public void RegisterTypes(IUnityContainer container) 
      { 

      } 
     } 
    } 

enter image description here

如果您有任何問題或要檢查,可以檢查代碼中Github上https://github.com/jesulink2514/Xamarin-Prism-shared-project

+0

@DanielBkr我希望這可以幫助。告訴我們您的問題是否已解決。 :) –

+1

謝謝,這個答案確實爲我解決了它。我錯過了Xaml命名空間和unity包。格拉西亞斯阿米戈! – DanielBkr

+0

太棒了! @DanielBkr,請考慮將此標記爲答案:) –

相關問題