当尝试执行ICommand时,由于参数为null,它总是抛出异常。
当我手动打印使用Debug.WriteLine传递给ICommand的变量时,它可以正常工作。
这是命令本身
private CatalogBase<T> _catalogBase;
public DeleteCommand()
{
_catalogBase = new CatalogBase<T>();
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_catalogBase.Delete((int)parameter);
}
public event EventHandler CanExecuteChanged;
带参数和命令绑定的按钮:
<AppBarButton Icon="Delete" Label="Delete" CommandParameter="{Binding CustomerId}" Command="{Binding DeleteCommand}"/>
这是上面引用的 CustomerId :
public int CustomerId
{
get { return _customer.CustomerId; }
}
同样,手动打印 _customer.CustomerId 会返回正确的,而不是null。
任何人都知道是什么原因引起的?提前谢谢。
下面的完整xaml和viewmodel:
<Page
x:Class="Hovmand.View.Domain.CustomersPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:page="using:Hovmand.ViewModel.Page"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
<page:CustomerViewModel/>
</Page.DataContext>
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Add" Label="Create" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Delete" Label="Delete" DataContext="" CommandParameter="{Binding CustomerDomainObject.CustomerId}" Command="{Binding DeleteCommand}"/>
</CommandBar>
</Page.BottomAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding Customers}" SelectedItem="{Binding CustomerDomainObject, Mode=TwoWay}">
<ListView.HeaderTemplate>
<DataTemplate>
<Grid Padding="0,12,0,12" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Padding="20,0,0,0" Text="Name" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="1" Text="Phone" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="2" Text="Mail" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="3" Text="Company" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="4" Text="Adress" Style="{ThemeResource CaptionTextBlockStyle}"/>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" VerticalAlignment="Center" Text="1" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="2"/>
<TextBlock Grid.Column="2" VerticalAlignment="Center" Text="3"/>
<TextBlock Grid.Column="3" VerticalAlignment="Center" Text="4"/>
<TextBlock Grid.Column="4" VerticalAlignment="Center" Text="5"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
视图模型
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Hovmand.Annotations;
using Hovmand.Model.Database;
using Hovmand.ViewModel.Base;
using Hovmand.ViewModel.Commands;
namespace Hovmand.ViewModel.Page
{
public class CustomerViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
private ICommand _createCommand = new CreateCommand<Customer>();
private ICommand _deleteCommand = new DeleteCommand<Customer>();
private ICommand _updateCommand = new UpdateCommand<Customer>();
private HovmanddbContext dbContext = HovmanddbContext.Instance;
private Customer _customer = new Customer();
public CustomerViewModel()
{
}
public Customer CustomerDomainObject
{
get { return _customer; }
set
{
_customer = value;
OnPropertyChanged();
Debug.WriteLine(_customer.CustomerId);
}
}
public int CustomerId
{
get { return _customer.CustomerId; }
}
public int Cvr
{
get { return CustomerDomainObject.Cvr; }
set
{
CustomerDomainObject.Cvr = value;
OnPropertyChanged();
}
}
public string CompanyName
{
get { return CustomerDomainObject.CompanyName; }
set
{
CustomerDomainObject.CompanyName = value;
OnPropertyChanged();
}
}
public string Address
{
get { return CustomerDomainObject.Address; }
set
{
CustomerDomainObject.Address = value;
OnPropertyChanged();
}
}
public int Phone
{
get { return CustomerDomainObject.Phone; }
set
{
CustomerDomainObject.Phone = value;
OnPropertyChanged();
}
}
public string Mail
{
get { return CustomerDomainObject.Mail; }
set
{
CustomerDomainObject.Mail = value;
OnPropertyChanged();
}
}
public int FkContactId
{
get { return CustomerDomainObject.FkContactId; }
set
{
CustomerDomainObject.FkContactId = value;
OnPropertyChanged();
}
}
public int FkLocationId
{
get { return CustomerDomainObject.FkLocationId; }
set
{
CustomerDomainObject.FkLocationId = value;
OnPropertyChanged();
}
}
public List<Customer> Customers
{
get { return dbContext.Customers.ToList(); }
}
public ICommand CreateCommand
{
get { return _createCommand; }
}
public ICommand DeleteCommand
{
get { return _deleteCommand; }
}
public ICommand UpdateCommand
{
get { return _updateCommand; }
}
[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}