【问题标题】:How to switch between views in MVVM application如何在 MVVM 应用程序中切换视图
【发布时间】:2018-02-06 04:17:28
【问题描述】:

成功登录后我有一个登录视图,它应该打开菜单视图,因为它有不同的选项卡。此外,我希望选项卡在菜单视图本身内打开,并且一旦打开另一个视图,该视图应该关闭。 我参考了以下链接: Changing the View for a ViewModelswitching views in MVVM wpf。 我做过这样的事情:

MainWindow.xaml

<Window x:Class="JGC_ngCMS_Win.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:JGC_ngCMS_Win.View"
    xmlns:VM="clr-namespace:JGC_ngCMS_Win.ViewModel"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <VM:MainWindowViewModel/>
</Window.DataContext>
<Window.Resources>
    <DataTemplate x:Key="View1Template" DataType="{x:Type VM:LoginViewModel}">
        <local:LoginView></local:LoginView>
    </DataTemplate>
    <DataTemplate x:Key="View2Template" DataType="{x:Type VM:MenuViewModel}">
        <local:MenuView />
    </DataTemplate>
    <DataTemplate x:Key="View3Template" DataType="{x:Type VM:UserModuleMapViewModel}">
        <local:UserModuleMapView />
    </DataTemplate>
</Window.Resources>
<Grid>
    <ContentPresenter Content="{Binding ViewModelsView.CurrentItem}" Grid.Row="1"/> 
    <ContentControl Content="{Binding  }">
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SwitchView}" Value="1">
                        <Setter Property="ContentTemplate" Value="{StaticResource View2Template}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
    <ContentControl Content="{Binding ViewModel}" />
</Grid>

MainWindowViewModel.cs

public class MainWindowViewModel : ViewModelBase
{
    private ViewModelBase _currentViewModel;

    readonly static LoginViewModel _loginViewModel = new LoginViewModel();
    readonly static MenuViewModel _menuViewModel = new MenuViewModel();
    readonly static UserModuleMapViewModel _usermodulemapViewModel = new UserModuleMapViewModel();

    public ViewModelBase CurrentViewModel
    {
        get
        {
            return _currentViewModel;
        }
        set
        {
            if (_currentViewModel == value)
                return;
            _currentViewModel = value;
            OnPropertyChanged("CurrentViewModel");
        }
    }



    public ICommand FirstViewCommand { get; private set; }
    public ICommand SecondViewCommand { get; private set; }


    public MainWindowViewModel()
    {

        CurrentViewModel = MainWindowViewModel._menuViewModel;
        FirstViewCommand = new RelayCommand(() => ExecuteFirstViewCommand());
        SecondViewCommand = new RelayCommand(() => ExecuteSecondViewCommand());

        //ViewModels = new ObservableCollection<ViewModelBase>()
        //  {
        //     new LoginViewModel(),
        //        new MenuViewModel()
        //        //new ViewModel3()
        //  };
        //ViewModelsView = CollectionViewSource.GetDefaultView(ViewModels);
    }





    public void ExecuteFirstViewCommand()
    {
        CurrentViewModel = MainWindowViewModel._usermodulemapViewModel;
    }

    private void ExecuteSecondViewCommand()
    {
        CurrentViewModel = MainWindowViewModel._menuViewModel;
    }

我的第一个屏幕是完美的登录视图,但成功登录后菜单视图应该打开。我犯了什么错误?

【问题讨论】:

  • 你好。您的DataTrigger 指的是SwitchView,但我在您的MV 代码中找不到它。
  • 实际上,绑定是一个临时情况,我设置了 1 或 0 值,它可以完美地工作..但是为视图编写怎么样..我坚持在那部分

标签: wpf mvvm views


【解决方案1】:

这是可能的答案之一。 我试图让它尽可能简单,例如避免使用样式。

XAML 代码(我创建了 LoginViewModel 和 MenuViewModel 作为 UserControls 来测试它)

<Window x:Class="JGC_ngCMS_Win.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:JGC_ngCMS_Win.View"
        xmlns:VM="clr-namespace:JGC_ngCMS_Win.ViewModel"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ContentPresenter Content="{Binding CurrentViewModel}">
            <ContentPresenter.Resources>
                <DataTemplate  DataType="{x:Type VM:LoginViewModel}">
                    <local:LoginView/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type VM:MenuViewModel}">
                    <local:MenuView />
                </DataTemplate>
            </ContentPresenter.Resources>
        </ContentPresenter>
    </Grid>
</Window>

这是 ModelView 类的最少可测试代码

class ViewModelBase:INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

class MainWindowViewModel:ViewModelBase
{
    readonly LoginViewModel _loginViewModel = new LoginViewModel();
    readonly MenuViewModel _menuViewModel = new MenuViewModel();

    private ViewModelBase _currentViewModel;
    public ViewModelBase CurrentViewModel
    {
        get
        {
            return _currentViewModel;
        }
        set
        {
            if (_currentViewModel == value)
                return;
            _currentViewModel = value;
            OnPropertyChanged("CurrentViewModel");
        }
    }

    //Just for test
    public void switchView()
    {
        if (CurrentViewModel == _loginViewModel) { CurrentViewModel = _menuViewModel; }
        else { CurrentViewModel = _loginViewModel; }
    }

}

class LoginViewModel:ViewModelBase
{
}

class MenuViewModel:ViewModelBase
{
}

那么你只需要指定DataContext:

    ViewModel.MainWindowViewModel mainVM = new ViewModel.MainWindowViewModel();
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = mainVM;
    }

备注:我为 DataContext 使用了显式实例,但如果需要,您可以使用静态属性。

【讨论】:

  • 感谢您的努力...我必须尝试...我是新人,所以您的帮助将是我准备工作的关键
  • 你能帮我理解成功登录后如何重定向到菜单视图
  • 您只需要通过直接分配更改您的CurrentViewModelCurrentViewModel = _menuViewModel; 就可以了。
  • 它的工作方式就像登录视图在应用程序启动时非常完美...登录成功,然后我调用了切换视图...currentviewmodel 被分配了 _menuViewModel 但视图没有更改为菜单视图跨度>
  • 在这种情况下你能帮帮我吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多