2014-03-05 83 views
0

在我發佈了大約一週前的問題之後,我繼續閱讀了關於演示者如何工作的問題。有很多關於如何使用移動應用程序做文章和視頻的文檔和視頻,尤其是IOS,但對於DESKTOP Windows WPF應用程序並不是很多。基於N = 24的視頻,我創建了一個演示者,我從MvxSimpleWpfViewPresenter派生並繼續覆蓋函數Present(System.Windows.FrameworkElement frameworkElement),其中主窗口將顯示我的主視圖以及所有其他視圖,將顯示在我的主視圖中的內容:繼我最近發佈的一篇文章後,我建議使用A PRESENTER

public class MyPresenter : Cirrious.MvvmCross.Wpf.Views.MvxSimpleWpfViewPresenter 
    { 
    private Window _mainWindow = null; 
    private MvxWpfView _firstView = null; 

    public MyPresenter(Window mainWindow) 
     : base(mainWindow) 
    { 
     _mainWindow = mainWindow; 
    } 

    public override void Present(System.Windows.FrameworkElement frameworkElement) 
    { 
     //_mainWindow.DisplayGrid 

     if(_firstView == null && 
     frameworkElement is FirstView) 
     { 
     _firstView = frameworkElement as FirstView; 
     _mainWindow.Content = _firstView; 
     } 
     else if(_firstView != null) 
     { 
     if ((_firstView as FirstView).DisplayGrid.Children.Count > 0) 
     { 
      (_firstView as FirstView).DisplayGrid.Children.RemoveAt(0); 
     } 

     (_firstView as FirstView).DisplayGrid.Children.Add(frameworkElement); 
     } 
    } 

我的主要觀點(被稱爲FirstViewModel)看起來是這樣的:

public class FirstViewModel : MvxViewModel 
    { 
    public ICommand BlueCommand 
    { 
     get { return new MvxCommand(() => ShowViewModel<BlueViewModel>()); } 
    } 

    public ICommand RedCommand 
    { 
     get { return new MvxCommand(() => ShowViewModel<RedViewModel>()); } 
    } 
    } 

而且我的firstView看起來像這樣(在XAML):

<views:MvxWpfView 
      x:Class="WpfApplication1.Views.FirstView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:views="clr-namespace:Cirrious.MvvmCross.Wpf.Views;assembly=Cirrious.MvvmCross.Wpf" 
      mc:Ignorable="d" Height="Auto" Width="Auto"> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <Menu Grid.Row="0"> 
     <MenuItem Name="RedCommandMenuItem" Header="Red command" Command="{Binding Path=RedCommand}" /> 
     <MenuItem Name="BlueCommandMenuItem" Header="Blue command" Command="{Binding Path=BlueCommand}" /> 
    </Menu> 
    <Grid Name="DisplayGrid" Grid.Row="1"> 
    </Grid> 
    </Grid> 
</views:MvxWpfView> 

之後,我讓演示者使用了默認的演示者,並且可以在包含MENUS的主視圖中顯示紅色背景和藍色背景。因此,這將基本上與我想要做的事情一起工作。

那麼我要去哪裏?我想知道的是,這與我看到的所有其他示例都非常不同,在使用演示者的SHOW METHOD的情況下,您需要使用Model,並且需要使用Mvx.Resolve來創建視圖。Show方法是不可用於MvxSimpleWpfViewPresenter和MvxSimpleWpfViewPresenter類的重寫。通過調用這些命令,ShowViewModel調用Presenter(我假設)顯示我的新視圖,但是我不需要在這裏調用Resolve,因爲我得到了FrameworkElement。那麼,決定在哪裏以及由誰來完成?我試圖理解背後的機制,以便在出現問題時更好地進行調試。並且在上一篇文章中提到的CONTAINER有沒有鏈接?

感謝

回答

0

那麼,正在做的決心和靠誰?

這是在基地正在做MvxWpfViewPresenter

public abstract class MvxWpfViewPresenter 
    : IMvxWpfViewPresenter 
{ 
    public void Show(MvxViewModelRequest request) 
    { 
     try 
     { 
      var loader = Mvx.Resolve<IMvxSimpleWpfViewLoader>(); 
      var view = loader.CreateView(request); 
      Present(view); 
     } 
     catch (Exception exception) 
     { 
      MvxTrace.Error("Error seen during navigation request to {0} - error {1}", request.ViewModelType.Name, 
          exception.ToLongString()); 
     } 
    } 

    public abstract void Present(FrameworkElement frameworkElement); 

    public virtual void ChangePresentation(MvxPresentationHint hint) 
    { 
     MvxTrace.Warning("Hint ignored {0}", hint.GetType().Name); 
    } 
} 

https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewPresenter.cs

Show也許應該在這個類virtual - 但它是一個非常簡單的類,我總是希望WPF用戶只想寫他們自己的版本IMvxWpfViewPresenter

是否與前一篇文章中提到的CONTAINER有任何關聯?

爲WPF的默認容器是:

public class MvxWpfViewsContainer 
    : MvxViewsContainer 
    , IMvxWpfViewsContainer 
{ 
    public FrameworkElement CreateView(MvxViewModelRequest request) 
    { 
     var viewType = GetViewType(request.ViewModelType); 
     if (viewType == null) 
      throw new MvxException("View Type not found for " + request.ViewModelType); 

     // , request 
     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); 

     var viewControl = viewObject as FrameworkElement; 
     if (viewControl == null) 
      throw new MvxException("Loaded View is not a FrameworkElement " + viewType); 

     var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader>(); 
     wpfView.ViewModel = viewModelLoader.LoadViewModel(request, null); 

     return viewControl; 
    } 
} 

https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Wpf/Views/MvxWpfViewsContainer.cs

它從https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross/Views/MvxViewsContainer.cs繼承 - 這是在所有平臺上共享。

在安裝過程中,容器裝滿查看使用反射Type秒 - 見https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-overriding-view-viewmodel-associations

+0

非常感謝你的答案和你的幫助InitalizeViewLookup信息。我說的LINK的含義並不是每個人都說的容器代碼的鏈接,而是與容器的連接以及我之前說的。對不起,錯誤的選擇的話。 – tessierpat

+0

容器在我的答案在基礎主持人代碼中使用了'IMvxSimpleWpfViewLoader' - 看到接口繼承https://github.com/MvvmCross/MvvmCross/blob/b1cb973062b2eb8149f0934f331c4006bc61fa3c/Cirrious/Cirrious.MvvmCross.Wpf/Views/IMvxWpfViewsContainer。 cs#L12 – Stuart

+0

再次感謝!非常感激。 – tessierpat

相關問題