使用MenuItem和不同的ViewModel进行Caliburn.Micro动作冒泡

时间:2015-01-23 09:46:40

标签: c# mvvm menuitem caliburn.micro

我创建了一个菜单用户控件视图,我希望使用Action冒泡在其他视图中重用该菜单。所以我的想法是,使用每个MenuItems的x:Name元素,并在MasterView中声明该函数。 这是代码:

//MenuView.xaml
<UserControl x:Class="CaliburnTestApp.MenuView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:cal="http://www.caliburnproject.org">
    <Grid >
        <Menu  IsMainMenu="True">
            <MenuItem  Header="_File" >
                <MenuItem Header="_New..."  x:Name="MenuItem_New"/>
                <MenuItem Header="_Open..." x:Name="MenuItem_Open"/>
                <Separator />
                <MenuItem Header="_Save" x:Name="MenuItem_Save"/>
                <MenuItem Header="_Save As..." x:Name="MenuItem_SaveAs"/>
                <Separator />
                <MenuItem Header="_Close"   cal:Message.Attach="[Event Click] = [Action MenuItem_Close()]"/>
            </MenuItem>
            <MenuItem Header="?">
                <MenuItem Header="_Help" x:Name="MenuItem_Help" />
                <MenuItem Header="_About" x:Name="MenuItem_About" />
            </MenuItem>
        </Menu>
        <Button Content="Button" cal:Message.Attach="[Event Click] = [Action MenuItem_Close()]"  HorizontalAlignment="Left" Margin="123,-1,-99,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</UserControl>

及其ViewModel

//MenuViewModel.cs
class MenuViewModel
{
    public MenuViewModel()
    {}

    public void MenuItem_Help()
    {
        Console.WriteLine("MenuItem_Help Clicked");
    }
}

这里是容器:

//SetupView.xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:cal="http://www.caliburnproject.org"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="CaliburnTestApp.SetupView"
 Height="703.693" Width="813" WindowStartupLocation="CenterScreen"   >
<Grid Margin="0,0,0,0" Background="#FF837F80">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="487"/>
        <ColumnDefinition Width="111*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition Height="90"/>
        <RowDefinition Height="10"/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <ContentControl  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"  x:Name="MenuView" VerticalAlignment="Top" />

    </Grid>

及其SetupViewModel类

//SetupViewModel.cs
public interface ISetup {}
class SetupViewModel : Screen, ISetup
{
    MenuViewModel _menu;

    public MenuViewModel MenuView
    {
        get { return this._menu; }
        set { this._menu = value; this.NotifyOfPropertyChange(); }
    }

    public SetupViewModel()
    {
        this._menu = new MenuViewModel();
    }

    public void MenuItem_Close()
    {
        Console.WriteLine("MenuItem_Close");
    }
}

问题是单击“未找到方法MenuItem_Close的目标”时的MenuItem Close get expection。 但是使用按钮关闭,SetupViewModel中的函数MenuItem_Close()被正确调用。 所以我不明白错误在哪里。

0 个答案:

没有答案