具有多个视图的 WPF 加上 Prism 和 Unity
WPF with multiple views plus Prism and Unity
我必须用 WPF C# 编写应用程序。我的问题是我不知道如何使用多个视图。直到知道我知道如何使用 Prism 在基本级别通过绑定将 ViewModel 连接到 View。通过覆盖 OnStartup 方法和使用 UnityContainer,我学会了一些 Unity 来注册 ViewModel 到 App.xml.cs 中的视图。
我想知道如何从视图 1 导航到视图 2,反之亦然。
我想通过一个按钮导航,但视图不同。
你能帮帮我吗?有什么建议吗?
像这样,退相干!
我有一个我很久以前写的例子,它不需要任何框架废话,根据我的经验,WPF MVVM 框架在大多数情况下是无用的,并且往往会使简单的事情复杂化你所需要的只是一个 ICommand
实现和实现 INotiftyPropertyChanged
的 ViewModelBase
,这是一个简单的说明:
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
资源选择视图,并设置 Window 的 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;
});
}
}
}
结果看起来像
使用 Prism Navigation 可以非常容易地做到这一点,并且不需要您创建对 ViewModel 的依赖项,也不需要使用动态 DataTemplates 引入性能滞后。我建议阅读有关 Prism 导航的文档。
本质上,您结合使用了 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
我必须用 WPF C# 编写应用程序。我的问题是我不知道如何使用多个视图。直到知道我知道如何使用 Prism 在基本级别通过绑定将 ViewModel 连接到 View。通过覆盖 OnStartup 方法和使用 UnityContainer,我学会了一些 Unity 来注册 ViewModel 到 App.xml.cs 中的视图。
我想知道如何从视图 1 导航到视图 2,反之亦然。 我想通过一个按钮导航,但视图不同。
你能帮帮我吗?有什么建议吗?
像这样,退相干!
我有一个我很久以前写的例子,它不需要任何框架废话,根据我的经验,WPF MVVM 框架在大多数情况下是无用的,并且往往会使简单的事情复杂化你所需要的只是一个 ICommand
实现和实现 INotiftyPropertyChanged
的 ViewModelBase
,这是一个简单的说明:
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
资源选择视图,并设置 Window 的 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;
});
}
}
}
结果看起来像
使用 Prism Navigation 可以非常容易地做到这一点,并且不需要您创建对 ViewModel 的依赖项,也不需要使用动态 DataTemplates 引入性能滞后。我建议阅读有关 Prism 导航的文档。
本质上,您结合使用了 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