【问题标题】:WPF with multiple views plus Prism and Unity具有多个视图以及 Prism 和 Unity 的 WPF
【发布时间】:2016-06-20 01:13:13
【问题描述】:

我必须用 WPF C# 编写一个应用程序。我的问题是我不知道如何使用多个视图。直到知道我知道如何在基本级别上使用 Prism 通过绑定将 ViewModel 连接到 View。通过重写 OnStartup 方法和使用 UnityContainer,我已经学会了一点 Unity 将 ViewModel 注册到 App.xml.cs 中的 View。

我想知道如何从视图 1 导航到视图 2,反之亦然。 我想通过一个按钮导航并且视图不同。

你能帮帮我吗?有什么建议吗?

像这样,退相干!

【问题讨论】:

  • 创建一个区域(例如<ContentControl mvvm:RegionManager.RegionName="MyRegion"/>),注册您的导航视图(IUnityContainer.RegisterTypeForNavigation),然后在绑定到您的按钮的命令中进行导航(RegionManager.RequestNavigate

标签: c# wpf mvvm unity-container prism


【解决方案1】:

我有一个我很久以前写的例子,它不需要任何框架废话,根据我的经验,WPF MVVM 框架在大多数情况下是无用的,而且往往会使简单的事情复杂化,你只需要一个 ICommand 实现和一个实现INotiftyPropertyChangedViewModelBase,这是一个简单的说明:

XML:

   <Window.Resources>
        <DataTemplate DataType="{x:Type local:ViewModel1}">
            <local:View1/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ViewModel2}">
            <local:View2/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ViewModel3}">
            <local:View3/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ContentControl Content="{Binding CurrentViewModel}">
        </ContentControl>

        <StackPanel Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Bottom">
            <Button Command="{Binding PrevViewModel}">Previouws View</Button>
            <Button Command="{Binding NextViewModel}">Next View</Button>
            <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel1}">Switch To View1</Button>
            <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel2}">Switch To View2</Button>
            <Button Command="{Binding SwitchToViewModel}" CommandParameter="{x:Type local:ViewModel3}">Switch To View3</Button>
        </StackPanel>
    </Grid>

鉴于上述情况,当CurrentViewModel 属性发生变化时,将根据DataTemplate 资源选择视图,并将窗口的DataContext 设置为MainViewModel

主 ViewModel 如下所示:

 public class MainViewModel : ViewModelBase
    {
        //add instances to all the ViewModels you want to switch between here, and add templates for them in your resources specifying the x:type and the view or data template to be used
        public ObservableCollection<ViewModelBase> ViewModels { get; set; } = new ObservableCollection<ViewModelBase>();

        private ViewModelBase _currentViewModel;

        public ViewModelBase CurrentViewModel {
            get { return _currentViewModel; }
            set { SetField(ref _currentViewModel, value); }
        }

        private ICommand _nextViewModel;
        public ICommand NextViewModel
        {
            get
            {
                return _nextViewModel = _nextViewModel ?? new RelayCommand(p =>
                  {
                      CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) +1];
                  }, p =>
                  {
                      return ViewModels.IndexOf(CurrentViewModel) + 1 != ViewModels.Count && CurrentViewModel != null;
                  });
            }
        }

        public ICommand _prevViewModel;
        public ICommand PrevViewModel
        {
            get
            {
                return _prevViewModel = _prevViewModel ?? new RelayCommand(p =>
                {
                    CurrentViewModel = ViewModels[ViewModels.IndexOf(CurrentViewModel) - 1];
                }, p =>
                {
                    return ViewModels.IndexOf(CurrentViewModel) != 0 && CurrentViewModel!=null;
                });
            }
        }

        private ICommand _switchToViewModel;
        public ICommand SwitchToViewModel
        {
            get
            {
               return _switchToViewModel = _switchToViewModel ?? new RelayCommand(p =>
                {
                    CurrentViewModel = this.ViewModels.FirstOrDefault(vm=>vm.GetType()==p as Type);
                }, p =>
                {
                    return this.ViewModels.FirstOrDefault(vm => vm.GetType() != p as Type) != null;
                });
            }
        }
    } 

结果看起来像

【讨论】:

  • @Adrian 抱歉,您以为您在问wpf,因为它在问题的标签中,我不知道与 IOS 相关的任何内容,请编辑您的问题并询问您到底需要什么并等待相关答案,问题是 wpf,不是 iphone 或 mobile 或任何表明您没有使用 wpf 的迹象。
  • 谢谢。没关系。你可能有一部智能手机。我问了这个问题,因为我想知道我是否可以在 WPF 中实现相同的行为。以防万一,我会将此页面添加为书签。
  • @Adrian 是的,你可以!这就是上面的想法,你需要对上面的唯一更改是隐藏按钮而不是禁用它们,如果你需要让我知道,让我知道你到底想要什么
  • 我发了一张图片。我需要单独的视图。
【解决方案2】:

使用 Prism Navigation 非常容易做到这一点,并且不需要您创建对 ViewModel 的依赖关系,或者使用动态 DataTemplates 引入性能滞后。我建议阅读有关 Prism 导航的文档。

https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/60-Navigation.md#view-based-navigation

基本上你使用 ReqestNavigate、GoBack 和 GoForward 的组合。

这里还有一个示例供您学习:

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/View-Switching%20Navigation_Desktop

您还应该观看本课程,该课程将引导您完成您的要求:

https://app.pluralsight.com/library/courses/prism-introduction/table-of-contents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多