2017-08-21 64 views
3

我有xamarin.ios和MvvmCross一個問題,我需要顯示的MvxViewController,它取決於誰調用它有兩種方式,我得到它:MvxViewController.PresentationAttribute視圖模型之前調用加載

public partial class CustomViewController : MvxViewController<CustomViewModel>, IMvxOverridePresentationAttribute 
{ 

    public CustomViewController() : base("CustomViewController", null) 
    { 

    } 

    public MvxBasePresentationAttribute PresentationAttribute() 
    { 

     if (ViewModel.KindNavigation) //Here's the issue 
     { 
      return new MvxSidebarPresentationAttribute(MvxPanelEnum.Center, MvxPanelHintType.ResetRoot, true, MvxSplitViewBehaviour.Detail); 
     } 
     else 
     { 
      return new MvxModalPresentationAttribute 
      { 
       ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen, 
       ModalTransitionStyle = UIModalTransitionStyle.CoverVertical 
      }; 
     } 
    } 
} 

如果我做ViewModel.anything,得到一個參數來定義的表示方式,視圖模型,爲空,我不能訪問。我甚至沒有打開它,因爲這個視圖的表現類型沒有定義。

CustomViewModel:

public class CustomViewModel : MvxViewModel<string>, IDisposable 
{ 
    private readonly IMvxNavigationService _navigationService; 

    public CustomViewModel(IMvxNavigationService navigationService) 
    { 
     _navigationService = navigationService; 
    } 

    private bool _KindNavigation; 
    public bool KindNavigation 
    { 
     get => _KindNavigation; 
     set => SetProperty(ref _KindNavigation, value); 
    } 

    public void Dispose() 
    { 
     throw new NotImplementedException(); 
    } 

    public override Task Initialize(string parameter) 
    { 
     KindNavigation = Convert.ToBoolean(parameter); 
     System.Diagnostics.Debug.WriteLine("parameter: " + parameter); 

     return base.Initialize(); 
    } 
} 

回答

1

這是MvvmCross的限制,因爲視圖模型不加載之前的看法是。這也是文檔中描述:https://www.mvvmcross.com/documentation/presenters/ios-view-presenter?scroll=446#override-a-presentation-attribute-at-runtime

要在運行時重寫呈現屬性,你可以在你的視圖控制器實現IMvxOverridePresentationAttribute和確定這樣的PresentationAttribute方法呈現屬性:

public MvxBasePresentationAttribute PresentationAttribute() 
{ 
    return new MvxModalPresentationAttribute 
    { 
     ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen, 
     ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve 
    }; 
} 

如果您從PresentationAttribute返回null,則iOS View Presenter將回退到用於修飾視圖控制器的屬性。如果視圖控制器未用演示屬性修飾,它將使用默認的演示屬性(動畫子演示文稿)。

注意:請注意,您的ViewModel在PresentationAttribute期間將爲空,因此您可以在此執行的邏輯受到限制。此限制的原因是MvvmCross Presenters是無狀態的,您無法將已經實例化的ViewModel與新視圖連接起來。

+0

鏈接被打破,所以更新了鏈接,我在答案裏添加了相關信息以備將來參考:) –

相關問題