【问题标题】:Binding error using ApplicationBarButtonCommand and DelegateCommand使用 ApplicationBarButtonCommand 和 DelegateCommand 的绑定错误
【发布时间】:2012-02-10 02:02:27
【问题描述】:

我有一个简单的 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>

视图模型:

public ICommand SaveMessageCommand { get;私人套装; }

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.

有什么想法会导致问题吗?

谢谢

【问题讨论】:

    标签: c# windows-phone-7 xaml mvvm prism


    【解决方案1】:

    修复了问题,问题出在 System.Windows.Interactivity 上,只是引用另一个版本,它包含在 prism 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2012-03-28
      • 2016-09-22
      相关资源
      最近更新 更多