【问题标题】:Xamarin Command for Custom ViewCell自定义 ViewCell 的 Xamarin 命令
【发布时间】:2018-08-04 06:48:29
【问题描述】:

我想创建一个汉堡菜单。我创建了一个 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>

错误:找不到“命令”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配。

我该如何解决这个问题或如何以不同的方式解决?

【问题讨论】:

    标签: listview xamarin xamarin.forms


    【解决方案1】:
    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2021-04-05
    • 2016-09-01
    • 2016-08-24
    • 2020-10-04
    • 1970-01-01
    相关资源
    最近更新 更多