繁体   English   中英

Xamarin.Forms - 绑定命令

[英]Xamarin.Forms - Binding a command

我正在使用 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代码),网格绑定视图模型,您的扩展器绑定根(您的页面)的命令:

<?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, iOS=1,  UWP=0}" OutlineColor="White" HasShadow="{OnPlatform Android=true, iOS=false, UWP=true}">
                        <Grid Padding="{OnPlatform Android=2, iOS=2,  UWP=0}" Margin="{OnPlatform Android=1, iOS=1,  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM