我必须在WPF C#中编写应用程序。我的问题是我不知道如何处理多个视图。直到知道我知道如何使用Prism通过绑定将ViewModel连接到View,在基本级别。通过重写OnStartup方法和UnityContainer的使用,我学会了一点Unity以在App.xml.cs中注册ViewModel到View。WPF多视图加棱镜和统一
我想知道如何从视图1导航到视图2,反之亦然。 我想浏览一个按钮,视图不同。
你能帮助我吗?一些建议?
我必须在WPF C#中编写应用程序。我的问题是我不知道如何处理多个视图。直到知道我知道如何使用Prism通过绑定将ViewModel连接到View,在基本级别。通过重写OnStartup方法和UnityContainer的使用,我学会了一点Unity以在App.xml.cs中注册ViewModel到View。WPF多视图加棱镜和统一
我想知道如何从视图1导航到视图2,反之亦然。 我想浏览一个按钮,视图不同。
你能帮助我吗?一些建议?
这是非常容易与棱镜导航做到这一点,它不需要你在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 MVVM框架是没用的大部分,而且往往简单的事情复杂化所有你需要的是一个ICommand
实现和ViewModelBase
它实现INotiftyPropertyChanged
,这里是一个简单的例子:
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
属性更改,视图将得到一个基于选择资源,并且窗口的DataContext设置为MainViewModel
。
主视图模型如下所示:
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或手机或任何迹象表明你没有使用'wpf'。 –
谢谢。没关系。你可能有一部智能手机。我问这个问题,因为我想知道我是否可以在WPF中实现相同的行为。为了以防万一,我会将此页面加入书签。 – Adrian
@Adrian是的,你确定你可以!,这是上述想法,你需要上述的唯一变化是隐藏按钮,而不是禁用它们,如果这就是你需要让我知道,让我知道你是如何准确地 –
创建一个区域(例如,' '),注册您的导航视图('IUnityContainer.RegisterTypeForNavigation'),然后执行导航命令绑定到你的按钮('RegionManager.RequestNavigate') –
Haukinger