使用ApplicationBarButtonCommand和DelegateCommand时发生绑定错误

本文关键字:绑定 错误 ApplicationBarButtonCommand DelegateCommand 使用 | 更新日期: 2023-09-27 18:24:03

我有一个简单的WP7应用程序,我正在尝试使用Prism DelegateCommand绑定到ApplicationBarButtonCommand。

代码如下。

XAML:

    <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar.share.rest.png" Text="Add"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<wi:Interaction.Behaviors>
    <i:ApplicationBarButtonCommand ButtonText="Add" CommandBinding="{Binding SaveMessageCommand, Mode=OneWay}"
                                   CommandParameterBinding="{Binding CurrentUser.Id}"/>
</wi:Interaction.Behaviors>

ViewModel:

public ICommand SaveMessageCommand{get;private set;}

public MainViewModel(IDataService dataService, INavigationService navigationService)
{
     //Some initialization goes here
     SaveMessageCommand = new DelegateCommand<int>(OnSaveMessage);
}
private void OnSaveMessage(int userId)
{
     if (_navigationService != null)
     {
        // TODO : change 0 to real current user id
        _navigationService.NavigateTo(new Uri(string.Format(ViewModelLocator.CreateNewMessageUrl, userId),
                    UriKind.Relative));
     }
}

用户类别:

public class User : BaseModel
{
    private int _id;
    public int Id
    {
        get { return _id; }
        set
        {
            if (value == _id)
            {
                return;
            }
            _id = value;
            RaisePropertyChanged("Id");
        }
    }
 }

当我运行应用程序时,我有数据错误

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'Microsoft.Practices.Prism.Commands.DelegateCommand`1[System.Int32]' (type 'Microsoft.Practices.Prism.Commands.DelegateCommand`1[System.Int32]'); BindingExpression: Path='SaveMessageCommand' DataItem='WindowsPhone_Application.ViewModel.MainViewModel' (HashCode=75877085); target element is 'Microsoft.Practices.Prism.Interactivity.ApplicationBarButtonCommand' (Name='null'); target property is 'CommandBinding' (type 'System.Windows.Data.Binding').. System.InvalidOperationException: Can't convert type Microsoft.Practices.Prism.Commands.DelegateCommand`1[System.Int32] to type System.Windows.Data.Binding.

有什么想法可以引起这个问题吗?

感谢

使用ApplicationBarButtonCommand和DelegateCommand时发生绑定错误

修复了这个问题,问题出在System.Windows.Interactive上,只需将引用放在棱镜中包含的另一个版本上