Windows store apps DelegateCommand<;T>;在DataTemplate中绑定时发生异常

Windows store apps DelegateCommand<;T>;在DataTemplate中绑定时发生异常,windows-store-apps,winrt-xaml,prism,Windows Store Apps,Winrt Xaml,Prism,我想我在库中发现了一个bug,但是我想知道是否有一种解决方法或者一种方法可以解决这个问题,而不必自己去做这个项目 我试图在ItemsControl中显示项目列表。每个项都有一个按钮,单击该按钮时应在父控件的数据上下文上执行命令,并将项的ID作为命令参数传递。基本上,我试图呈现一个项目列表,并为每个项目设置一个删除按钮。为了完成这一点,我遵循这一点,到目前为止,我发现这是完成这一壮举的唯一干净的方法 不幸的是,Prism for Windows Runtime实现的DelegateCommand类

我想我在库中发现了一个bug,但是我想知道是否有一种解决方法或者一种方法可以解决这个问题,而不必自己去做这个项目

我试图在ItemsControl中显示项目列表。每个项都有一个按钮,单击该按钮时应在父控件的数据上下文上执行命令,并将项的ID作为命令参数传递。基本上,我试图呈现一个项目列表,并为每个项目设置一个删除按钮。为了完成这一点,我遵循这一点,到目前为止,我发现这是完成这一壮举的唯一干净的方法

不幸的是,Prism for Windows Runtime实现的
DelegateCommand
类似乎不能很好地与XAML绑定解析器配合使用。当所有内容都已连接时,当页面尝试呈现时,我会收到此异常:

未能分配到属性“Windows.UI.Xaml.Controls.Primitives.ButtonBase.Command”。[第61行位置:49]

我创建了一个新项目并简化了我的生产示例,以测试我的理论,即这是
DelegateCommand
的问题(这是允许将参数传递给委托方法所必需的)。我实现了两个命令,一个使用
DelegateCommand
,另一个只使用
DelegateCommand
。使用
DelegateCommand
的命令不会导致异常,但我无法接受命令参数,这意味着我无法识别要删除的项目

XAML:

<ItemsControl Name="TestControl" Grid.Row="1" ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}" Command="{Binding DataContext.BrokenCommand, ElementName=TestControl}" CommandParameter="1" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

视图模型:

public DelegateCommand<int> BrokenCommand { get; set; }
public DelegateCommand WorkingCommand { get; set; }

public List<string> MyItems { get { return new List<string> {"A", "B", "C"}; } }

public MainPageViewModel(INavigationService navigationService)
{
    BrokenCommand = new DelegateCommand<int>(BrokenMethod);
    WorkingCommand = new DelegateCommand(WorkingMethod);
}

private void BrokenMethod(int i)
{
    throw new NotImplementedException();
}

private void WorkingMethod()
{
    throw new NotImplementedException();
}
public DelegateCommand<object> WorkingCommand { get; set; }

public List<string> MyItems { get { return new List<string> {"A", "B", "C"}; } }

public MainPageViewModel()
{
    WorkingCommand = new DelegateCommand<object>(WorkingMethod);
}

private void WorkingMethod(object id)
{
    throw new NotImplementedException();
}
public DelegateCommand BrokenCommand{get;set;}
公共DelegateCommand工作命令{get;set;}
公共列表MyItems{get{返回新列表{“A”、“B”、“C”};}
公共主页视图模型(INavigationService导航服务)
{
BrokenCommand=newdelegatecommand(BrokenMethod);
WorkingCommand=新的DelegateCommand(WorkingMethod);
}
私有无效代理方法(int i)
{
抛出新的NotImplementedException();
}
私有void工作方法()
{
抛出新的NotImplementedException();
}
试试这个

    BrokenCommand = new DelegateCommand<string> 
    (id => BrokenMethod(Convert.ToInt32((id));
BrokenCommand=newdelegateCommand
(id=>BrokenMethod(Convert.ToInt32((id));
我刚刚测试了它,如果你把T改成字符串,它就会工作


希望有人能解释原因:)

在从另一个答案中获得一些灵感后,我终于完成了这项工作。我不确定为什么,但委托命令的属性签名导致了异常。将
DelegateCommand
更改为
DelegateCommand
可以使一切正常工作。现在我可以将对象转换为
int
并从这里。如果有人能解释为什么会发生这个问题,那就太好了

XAML:


视图模型:

public DelegateCommand<int> BrokenCommand { get; set; }
public DelegateCommand WorkingCommand { get; set; }

public List<string> MyItems { get { return new List<string> {"A", "B", "C"}; } }

public MainPageViewModel(INavigationService navigationService)
{
    BrokenCommand = new DelegateCommand<int>(BrokenMethod);
    WorkingCommand = new DelegateCommand(WorkingMethod);
}

private void BrokenMethod(int i)
{
    throw new NotImplementedException();
}

private void WorkingMethod()
{
    throw new NotImplementedException();
}
public DelegateCommand<object> WorkingCommand { get; set; }

public List<string> MyItems { get { return new List<string> {"A", "B", "C"}; } }

public MainPageViewModel()
{
    WorkingCommand = new DelegateCommand<object>(WorkingMethod);
}

private void WorkingMethod(object id)
{
    throw new NotImplementedException();
}
public DelegateCommand工作命令{get;set;}
公共列表MyItems{get{返回新列表{“A”、“B”、“C”};}
公共MainPageViewModel()
{
WorkingCommand=新的DelegateCommand(WorkingMethod);
}
私有无效工作方法(对象id)
{
抛出新的NotImplementedException();
}

这会导致编译器错误,“不兼容的匿名函数签名”。如果我使用DelegateCommand,它会进行编译,但仍会生成原始异常。好的,我更新了此答案。请尝试此操作,并且需要使用DelegateCommand,因为T是您的int(id)谢谢!这让我找到了解决问题的正确途径。最后,我所需要做的就是将属性签名更改为
DelegateCommand
。工作代码如下所示:
BrokenCommand=new DelegateCommand(BrokenMethod);