2012-04-24 32 views
7

我一直在跨平臺移動項目中使用MvvmCross,並且在使用相同共享視圖模型的MonoTouch項目中擁有2個不同視圖,並且不知道如何去構建我的代碼以導航到在MvvmCross中使用相同的視圖模型的不同視圖。MvvmCross - 爲多個視圖共享視圖模型

回答

11

MvvmCross平臺使用的默認約定是使用反射自動註冊所有視圖。

這是在基礎設置類來完成 - 在https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Platform/MvxBaseSetup.cs

protected virtual void InitializeViews() 
    { 
     var container = this.GetService<IMvxViewsContainer>(); 

     foreach (var pair in GetViewModelViewLookup()) 
     { 
      Add(container, pair.Key, pair.Value); 
     } 
    } 

其中GetViewModelViewLookup返回視圖模型類型的字典來查看方式:

protected virtual IDictionary<Type, Type> GetViewModelViewLookup(Assembly assembly, Type expectedInterfaceType) 
    { 
     var views = from type in assembly.GetTypes() 
        let viewModelType = GetViewModelTypeMappingIfPresent(type, expectedInterfaceType) 
        where viewModelType != null 
        select new { type, viewModelType }; 

     return views.ToDictionary(x => x.viewModelType, x => x.type); 
    } 

在通用的iPad/iPhone應用程序,你做偶爾想要爲每個視圖模型包含多個視圖 - 使用iPad中的一個視圖和iPhone中的一個視圖。

要做到這一點,現在(從字面上剛纔!)一些可用來標記你的意見爲「非常規」的屬性 - 它們是:

最後的這些屬性可能是你在這種情況下,想要的東西 - 你可以實現簡單的iPhone/iPad的切換使用兩種意見MainViewModel聲明如下:

[MvxFormFactorSpecificView(MvxTouchFormFactor.Phone)] 
public class MyIPhoneView : BaseView<MainViewModel> 
{ 
    // iphone specific view ... 
} 

[MvxFormFactorSpecificView(MvxTouchFormFactor.Pad)] 
public class MyIPadView : BaseView<MainViewModel> 
{ 
    // ipad specific view ... 
} 

或者,如果您需要非常自定義的配置,則可以覆蓋所有'基於約定'的行爲 - 您可以實施自己的替代GetViewModelViewLookup - 例如:

protected override IDictionary<Type, Type> GetViewModelViewLookup(Assembly assembly, Type expectedInterfaceType) 
{ 
    if (IsIPad) 
    { 
     return new Dictionary<Type, Type>() 
     { 
      { typeof(HomeViewModel), typeof(IPadHomeView) }, 
      { typeof(DetailViewModel), typeof(IPadDetailView) }, 
      { typeof(AboutViewModel), typeof(SharedAboutView) }, 
     }; 
    } 
    else 
    { 
     return new Dictionary<Type, Type>() 
     { 
      { typeof(HomeViewModel), typeof(IPhoneHomeView) }, 
      { typeof(DetailViewModel), typeof(IPhoneDetailView) }, 
      { typeof(AboutViewModel), typeof(SharedAboutView) }, 
     }; 
    } 
} 

,最終你可能會認爲你需要額外的ViewModels以及用於iPad的應用程序視圖 - iPad的了,畢竟,一個更大的屏幕 - 在這種情況下,你可以手動添加它們。最終,當您的應用觸及幾百萬用戶時,您甚至可能決定將平板電腦代碼徹底分離出電話代碼 - 但通常可以等到您達到幾百萬分鐘的時候...

+0

Android有沒有簡單的方法?我有一個通用的ViewModel,並希望不同的基於axml的視圖共享相同的ViewModel。 – j7nn7k 2015-02-21 19:45:45

+0

對不起,不知道:)如果別人正在找那個:Overview'OnViewModelSet()' – j7nn7k 2015-02-21 19:57:16

0

另一種方法它是先走,並創建2周的ViewModels,但讓他們既繼承一個抽象的視圖模型,如下:

FirstViewViewModel : BaseViewModel 
SecondViewViewModel : BaseViewModel 

使用命名相應的視圖:

FirstView.xaml 
SecondView.xaml 

這種方式,你能夠把一些共享行爲在BaseViewMod el,而2個子類真的只是爲了滿足MvvmCross的視圖獲取約定。

0

我最近開始使用MvvmCross,我正在使用v4.2.1。看來有些名字已經改變了。我正在使用一個ViewModel,具有以下獨立的iPhone和iPad視圖:

[MvxFormFactorSpecific(MvxIosFormFactor.Phone)] 
public class MyIPhoneView : BaseView<MainViewModel> 
{ 
    // iphone specific view ... 
} 

[MvxFormFactorSpecific(MvxIosFormFactor.TallPhone)] 
public class MyTallIPhoneView : BaseView<MainViewModel> 
{ 
    // tall iphone specific view ... 
} 

[MvxFormFactorSpecific(MvxIosFormFactor.Pad)] 
public class MyIPadView : BaseView<MainViewModel> 
{ 
    // ipad specific view ... 
} 
相關問題