2014-08-28 63 views
0

我看到MvvmCross touch支持使用MvxViewModelRequest從ViewModel對象創建視圖。從ViewModel對象創建視圖

但在MvvmCross WPF,我可以用

Mvx.Resolve<IMvxSimpleWpfViewLoader>().CreateView(viewmodelRequest) 

但是,我無法找到一個方法來創建一個視圖模型對象視圖只創建一個從MvxViewModelRequest看法?對於WPF,這是否支持MvvmCross?

回答

1

默認情況下,Wpf中不包含此功能 - 但您可以輕鬆添加它。

邏輯將類似於在https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewsContainer.cs請求爲基礎的代碼 - 是這樣的:

// Use `IMvxViewFinder` to find the type of view: 
    var viewType = Mvx.Resolve<IMvxViewFinder>().GetViewType(myViewModel.GetType()); 

    // create a view and set the data context 
    var viewObject = Activator.CreateInstance(viewType); 
    if (viewObject == null) 
    throw new MvxException("View not loaded for " + viewType); 

    var wpfView = viewObject as IMvxWpfView; 
    if (wpfView == null) 
    throw new MvxException("Loaded View does not have IMvxWpfView interface " + viewType); 

    wpfView.ViewModel = myViewModel; 

你可以建設成爲一個自定義視圖容器或如果你想自定義視圖演示這一點。

0

假設u有

public partial class LoginViewController : MvxViewController<LoginViewModel> 

比,如果我想使用視圖某處ü可以做這樣的事情

this.presentedCurrentController = Activator.CreateInstance(typeof(LoginViewController)) as LoginViewController; 
(this.presentedCurrentController as LoginViewController).ViewModel = new LoginViewModel(); 

其中

this.presentedCurrentController it's 

var NSViewController presentedCurrentController; 

太感謝了@cheesebaron和linkanother one