自定义ViewCell的Xamarin命令



我想创建一个Humburger菜单。我创建了一个ExtendedViewCell,它覆盖了SelectedColor,我还向其添加了CommanderProperty。我做对了吗?然后,我为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初始化时会导致错误。

我的页面:

<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>

错误:没有为"命令"找到的属性,可绑定的属性或事件,或在价值和属性之间的类型不匹配类型。

我如何解决此问题或如何不同?

这是一个解决方案:https://anthonysimmon.com/eventtocommand-in-xamarin-forms-apps/或者,您可以看到Xamarin.forum https://forums.xamarin.com/discussion/60937/how-to-to-bind-a-command-to-listview-itemtapped

最新更新