我想创建一个humburger菜单。我创建了一个ExtendedViewCell,它覆盖了SelectedColor,我还添加了CommandProperty。我做对了吗? 然后我为我的MenuItemCellData菜单单元创建了一个数据模型。
ExtendedViewCell类:
public class ExtendedViewCell : ViewCell
{
#region BindavleProperties
public static BindableProperty SelectedColorProperty = BindableProperty.Create(
"SelectedColor",
typeof(Color),
typeof(ExtendedViewCell),
Color.Accent);
public static BindableProperty CommandProperty = BindableProperty.Create(
"Command",
typeof(ICommand),
typeof(ExtendedViewCell));
#endregion
#region Properies
public Color SelectedColor
{
get => (Color)GetValue(SelectedColorProperty);
set => SetValue(SelectedColorProperty, value);
}
public ICommand Command
{
get => (ICommand) GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
#endregion
public ExtendedViewCell()
{
if (Command != null)
{
Tapped += (sender, args) =>
{
if (Command.CanExecute(null))
Command.Execute(null);
};
}
}
}
首先,我在Xaml资源中创建一个单元格数组,然后将它们分配给我的ListView.ItemSource。但是在MenuItemCellData中初始化时,Command属性会导致错误。
我的页面:
<pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MDOSchedule.UI.Pages.MasterDetailPage"
xmlns:pages="clr-namespace:MDOSchedule.UI.Pages;assembly=MDOSchedule"
xmlns:extensions="clr-namespace:MDOSchedule.Extensions;assembly=MDOSchedule"
xmlns:uimodels="clr-namespace:MDOSchedule.UI.Models;assembly=MDOSchedule">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Menu Data -->
<x:Array x:Key="NavigationItems" Type="{x:Type uimodels:MenuItemCellData}">
<uimodels:MenuItemCellData Text="{extensions:Translate AllJobsPageTitle}"
ImageFileName="ic_assignment.png"
Command="{Binding GoToAllJobsCommand}"/>
<uimodels:MenuItemCellData Text="{extensions:Translate MyJobsPageTitle}"
ImageFileName="ic_assignment.png"
Command="{Binding GoToMyJobsCommand}"/>
<uimodels:MenuItemCellData Text="{extensions:Translate MyTasksPageTitle}"
ImageFileName="ic_assignment.png"
Command="{Binding GoToMyTasksCommand}"/>
<uimodels:MenuItemCellData Text="{extensions:Translate LogoutButton}"
ImageFileName="ic_assignment.png"
Command="{Binding GoToLoginCommand}"/>
</x:Array>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<!-- App Info -->
<StackLayout BackgroundColor="{StaticResource PrimaryColor}">
<Grid Margin="{StaticResource SmallMargin}">
<!-- App Name -->
<Label Grid.Row="0" Text="{extensions:Translate AppName}"
TextColor="White" FontSize="Large"/>
</Grid>
</StackLayout>
<ListView Grid.Row="1"
ItemsSource="{StaticResource NavigationItems}"
ItemTemplate="{StaticResource DataTemplate.MenuItems}"/>
</Grid>
</ContentPage.Content>
</pages:BasePage>
MenuItemCellData:
public class MenuItemCellData : Bindable
{
#region Properties
public string Text
{
get => Get(String.Empty);
set => Set(value);
}
public string ImageFileName
{
get => Get(String.Empty);
set => Set(value);
}
public ICommand Command
{
get => Get<ICommand>();
set => Set(value);
}
#endregion
}
我的模板
<!-- Menu Item Commands-->
<DataTemplate x:Key="DataTemplate.MenuItems">
<controls:ExtendedViewCell SelectedColor="{StaticResource PrimaryColorLight}"
Command="{Binding Command}">
<Grid>
<StackLayout Orientation="Horizontal"
Margin="{StaticResource SmallMargin}">
<Image Aspect="AspectFit" Source="{Binding ImageFileName}"
WidthRequest="16" HeightRequest="16"/>
<Label Text="{Binding Text}"/>
</StackLayout>
</Grid>
</controls:ExtendedViewCell>
</DataTemplate>
错误:找不到&#39; Command&#39;的属性,可绑定属性或事件,或者值和属性之间的不匹配类型。
我如何解决此问题或如何以不同方式解决此问题?
答案 0 :(得分:-1)