使用MVVM WPF导航 [英] WPF Navigation using MVVM

查看:140
本文介绍了使用MVVM WPF导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这个帖子提供了答案,但我一定是失去了一些东西微不足道。我定义我的的DataTemplate 译文] 的App.xaml 如下:

I'm trying to follow the answer provided in this post, but I must be missing something trivial. I've defined my DataTemplates as App.xaml as follows:

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:BlowerViewModel}">
        <v:BlowerView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type vm:HomeViewModel}">
        <v:HomeView />
    </DataTemplate>
</Application.Resources>

于是,在我的 MainWindow.xaml 我定义如下code:

Then, in my MainWindow.xaml I've defined the following code:

<Window x:Class="App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:App.UI.ViewModel"
        Title="MainWindow" SizeToContent="WidthAndHeight">
    <Window.DataContext>
        <vm:MainViewModel />
    </Window.DataContext>
    <ContentControl Content="{Binding CurrentView}" />
</Window>

在$ C $下 MainViewModel 包含属性 CurrentView ICommand的这样我就可以切换视图。定义如下:

The code for MainViewModel contains a property CurrentView and an ICommand so I can switch views. Defined as follows:

public class MainViewModel : BaseViewModel
{
    private BaseViewModel _currentView;

    public MainViewModel()
    {
        CurrentView = new HomeViewModel();
    }

    public BaseViewModel CurrentView
    {
        get { return _currentView; }
        set
        {
            if (_currentView != value)
            {
                _currentView = value;
                RaiseChangedEvent("CurrentView");
            }
        }
    }

    public ICommand SwitchView { 
        get {
            return new CommandHandler(() => SwitchBlower());
        }
    }

    protected void SwitchBlower()
    {
        CurrentView = new BlowerViewModel(); 
    }
}

在我的 HomeView.xaml ,我已经定义了一个按钮链接到 MainViewModel 执行通过SwitchView ICommand的。这如下所示。

In my HomeView.xaml, I have defined a button that links to the MainViewModel to execute the SwitchView ICommand. This is shown below.

<UserControl x:Class="App.UI.View.HomeView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:App.UI.ViewModel"
        Height="300" Width="300">
    <Grid>
        <TextBlock>This is the homeview</TextBlock>
        <Button Command="{Binding DataContext.SwitchView, RelativeSource={RelativeSource AncestorType={x:Type vm:MainViewModel}}, Mode=OneWay}" Content="Test" />
    </Grid>
</UserControl>

当我启动应用程序不注册的情况下,并点击按钮不会触发事件改变了看法。我试图把断点在两个 ICommand的获得和函数调用本身。起初,我想也许我需要定义 MainViewModel 在我的数据模板,但这样做会导致下面的错误(即使该项目构建罚款)

When I start the application it doesn't register the event, and clicking on the button does not fire the event to change the view. I've tried putting breakpoints in both the ICommand get and the function call itself. At first, I thought maybe I needed to define MainViewModel in my data templates, but doing so results in the following error (even though the project builds fine)

不能把一个窗口的样式

任何人都可以提供缺少的一块,我需要得到这个工作?

Can anyone provide the missing piece I need to get this working?

推荐答案

该AncestorType应该是主窗口不MainViewModel。 MainViewModel不是一类是视觉树的一部分。

The AncestorType should be MainWindow not MainViewModel. MainViewModel is not a class that is part of the visual tree.

这篇关于使用MVVM WPF导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆