微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin.Forms - 绑定命令

如何解决Xamarin.Forms - 绑定命令

我正在使用 ToolKits Expander 并且我正在尝试绑定一个命令,这是我目前得到的:

public partial class AssignTaskPage : ContentPage
    {

        

        public AssignTaskPage()
        {
            InitializeComponent();

            GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());

        }

        public ICommand GetMathSubCatgories { get; private set; }
        void MathSubCatgoriesCommand()
        {
            Console.Write("Here");
        }

    }

在我看来

<xct:Expander Command="{Binding GetMathSubCatgories}">
                            <xct:Expander.Header>
                                <Frame Padding="10" Margin="10" HasShadow="False" BorderColor="LightGray" VerticalOptions="CenterandExpand">
                                    <StackLayout Orientation="Horizontal">
                                        <Image Source="{Binding icon}" WidthRequest="25" HeightRequest="25"></Image>
                                        <Label Text="{Binding name}" TextColor="{Binding textColor}" FontSize="Large" FontAttributes="Bold" HeightRequest="35" VerticalOptions="CenterandExpand"></Label>
                                    </StackLayout>
                                </Frame>
                            </xct:Expander.Header>
                            <Grid Padding="10">
                                <Grid.ColumnDeFinitions>
                                    <ColumnDeFinition Width="Auto" />
                                </Grid.ColumnDeFinitions>
                                <ListView x:Name="SubCategories" ItemsSource="{Binding subCategories}" ItemSelected="SubCategories_ItemSelected">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <StackLayout>
                                                    <Label Text="{Binding name}" TextColor="#02cc9d" FontAttributes="Bold" HeightRequest="35" VerticalOptions="CenterandExpand"></Label>
                                                </StackLayout>
                                            </ViewCell>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </Grid>
                        </xct:Expander>

这根本行不通,(我在 Console.Write("Here"); 上设置了一个断点;并且它没有击中它)

所以我做了一些挖掘并找到了这个教程:

https://www.syncfusion.com/kb/12154/how-to-bind-command-to-expander-in-itemtemplate-of-xamarin-forms-listview-sflistview

这里是 git 中的示例。

https://github.com/SyncfusionExamples/command-to-expander-in-itemtemplate-listview-xamarin

我明白我必须在这里做什么,我面临的问题是当这个命令被调用时,我想获得一个值并在我的 AssignTaskPage 中使用它,但是教程所说的有一个 viewmodel在一个单独的文件中。那么我应该在我的 AssignTaskPage 中设置一个 MessagingCenter 并在 viewmodel 中调用它以获取我想要的值并将其传递给 AssignTaskPage 吗?

解决方法

因为您的命令未在您绑定到的 ViewModel 中定义。您可以绑定在您的 AssignTaskPage 中定义的命令,然后为扩展器的父元素绑定视图模型。

例如:

public partial class AssignTaskPage : ContentPage
{

    public AssignTaskPage()
    {
        InitializeComponent();

        GetMathSubCatgories = new Command(() => MathSubCatgoriesCommand());
        BindingContext = this;
    }

    public ICommand GetMathSubCatgories { get; private set; }
    void MathSubCatgoriesCommand(object obj)
    {
          DisplayAlert("Alert!","" + (obj as Contact).ContactName + "expanded","Ok");
    }

}

xaml(这里使用上面示例的xaml代码),grid绑定viewmodel,你的expander绑定root(你的页面)的命令:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:ExpanderXamarin"
         x:Class="ExpanderXamarin.ExpandableListView"
         x:Name="root"
         xmlns:sflistview="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
         xmlns:expander="clr-namespace:Syncfusion.XForms.Expander;assembly=Syncfusion.Expander.XForms">

<ContentPage.Content>
    <Grid x:Name="mainGrid" BackgroundColor="#F0F0F0" Padding="4">
        <Grid.BindingContext>
            <local:ViewModel />
        </Grid.BindingContext>
        <sflistview:SfListView x:Name="listView" AutoFitMode="DynamicHeight" ItemsSource="{Binding ContactsInfo}">
            <sflistview:SfListView.ItemTemplate>
                <DataTemplate>
                    <Frame x:Name="frame" CornerRadius="2" Padding="{OnPlatform Android=1,iOS=1,UWP=0}" Margin="{OnPlatform Android=1,UWP=0}" OutlineColor="White" HasShadow="{OnPlatform Android=true,iOS=false,UWP=true}">
                        <Grid Padding="{OnPlatform Android=2,iOS=2,UWP=0}" BackgroundColor="White" >
                            <expander:SfExpander x:Name="expander"   HeaderIconPosition="None">
                                <expander:SfExpander.Behaviors>
                                    <local:EventToCommandBehavior Command="{Binding Path=BindingContext.GetMathSubCatgories,Source={x:Reference root}}" EventName="Expanding" CommandParameter="{Binding .}"/>
                                </expander:SfExpander.Behaviors>
                                <expander:SfExpander.Header>
                                   ...
                                </expander:SfExpander.Header>
                                <expander:SfExpander.Content>
                                   ..
                                </expander:SfExpander.Content>
                            </expander:SfExpander>
                        </Grid>
                    </Frame>
                </DataTemplate>
            </sflistview:SfListView.ItemTemplate>
        </sflistview:SfListView>
    </Grid>
  </ContentPage.Content>
</ContentPage>

如果你想得到参数,你可以像上面那样绑定 CommandParameter

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。