使用 Caliburn.Micro cal:Message.Attach 执行 No target found for method

Using Caliburn.Micro cal:Message.Attach does a No target found for method

我用最近打开的文件夹创建了动态菜单项。这很好用。 现在,因为这些 MenuItem 是动态创建的,所以当我单击一个 MenutItem 时,我想引发一个操作并将 MenuItem 的 header 作为参数。 所以这是我的 "MainView.xaml"

        <Window
    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:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:common="clr-namespace:Common;assembly=RecentFileListLib" 
    xmlns:cal="http://www.caliburnproject.org" 
    xmlns:self="clr-namespace:MainUI.Models"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ViewModels="clr-namespace:MainUI.ViewModels" xmlns:local="clr-namespace:MainUI.Views" x:Name="window" x:Class="MainUI.Views.MainView"
    mc:Ignorable="d"
    Title="MainView" Height="450" Width="800">
<Window.Resources>
    <self:DebugDummyConverter x:Key="DebugDummyConverter" />
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="20"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" Margin="0">
        <MenuItem x:Name="menuItem"   Header="_File">
            <MenuItem Header="_Open" x:Name="FileOpen"/>

            <MenuItem x:Name="RecentProject" Header="Recents Projects" >
                <MenuItem.ItemTemplate   >
                    <DataTemplate >
                        <MenuItem Header="{Binding DisplayPath, Converter={StaticResource DebugDummyConverter}}" cal:Message.Attach="Remove($dataContext)"/>
                    </DataTemplate>
                </MenuItem.ItemTemplate>

            </MenuItem>
        </MenuItem>
    </Menu>
    <StackPanel Orientation="Vertical" Grid.Row="2" Grid.Column="1" Grid.RowSpan="1" Margin=" 0 0 10 0" >
        <Button x:Name="LoadUser" Content="Load User Page" />

    </StackPanel>
    <ContentControl Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="1" Grid.RowSpan="2" x:Name="ActiveItem"/>
    <StatusBar Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2">
        <StatusBar.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="100" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </StatusBar.ItemsPanel>
        <StatusBarItem Grid.Column="1">
            <TextBlock Text="{Binding FolderPath}" />
        </StatusBarItem>
    </StatusBar>
</Grid>

为此我使用 cal:Message.Attach="Remove($dataContext) 在我的视图模型中 "MainViewModel"

public partial class MainViewModel : Conductor<Object>
{

    public BindableCollection<RecentFile> RecentProject { get; private set; } = new BindableCollection<RecentFile>();

    public string FolderPath { get; set; }


    public MainViewModel()
    {
        Persister = new RegistryPersister();

        MaxNumberOfFiles = 9;
        MaxPathLength = 50;
        MenuItemFormatOneToNine = "_{0}:  {2}";
        MenuItemFormatTenPlus = "{0}:  {2}";
        RemoveMenuItems();
        LoadRecentFiles();
    }

    public void Remove(Object child)
    {

    }

通过这样做,当我点击我的菜单项时,我会收到一条消息 "No target found for method Remove."

如果有人可以帮助我。

提前致谢

您可以使用 cal:Action:TargetWithoutContext。例如,

cal:Action.TargetWithoutContext="{Binding ElementName=RecentProject, Path=DataContext}"

完整代码

<MenuItem x:Name="RecentProject" Header="Recents Projects" Tag="{Binding}">
                    <MenuItem.ItemTemplate   >
                        <DataTemplate >
                            <MenuItem Header="{Binding DisplayPath}" cal:Message.Attach="Remove($dataContext)" cal:Action.TargetWithoutContext="{Binding ElementName=RecentProject, Path=DataContext}"/>
                        </DataTemplate>
                    </MenuItem.ItemTemplate>
</MenuItem>