代码之家  ›  专栏  ›  技术社区  ›  Pranay Deep

C相当于XAML的commandParameter=“binding.”

  •  1
  • Pranay Deep  · 技术社区  · 6 年前

    我正在用C语言编写一些用户界面代码。它有 列表视图 具有 ItemSource 作为 List<MyClass> . 当我单击一个单元格时,我想传递 MyClass 到绑定的命令。

    当我在XAML中开发它时,遇到了commandParameter=“binding.”。这样我就可以发送整个物体了。

     <Label.GestureRecognizers>
         <TapGestureRecognizer Command="{Binding BindingContext.MyId, Source={x:Reference MyList}}"
        CommandParameter="{Binding .}" />
        </Label.GestureRecognizers>
    

    我想知道C等价于 "{Binding .}" .

    3 回复  |  直到 6 年前
        1
  •  1
  •   Pranay Deep    6 年前

    在花了几乎几个小时的时间来应用各种排列和组合之后,以下是对我有用的“诀窍”:

    Label lbl = new Label();
    lbl.SetBinding(Label.TextProperty, modelProperty.Name, BindingMode.TwoWay);
    TapGestureRecognizer tgr= new TapGestureRecognizer
    {
      Command = BindingContext.CellClickedCommand,
    };
    tgr.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
    lbl.GestureRecognizers.Add(tgr);
    

    我认为微软必须为Xamarin表单提供更好的文档,因为并非所有开发人员都一直在使用Xaml。而且,大多数文档都不使用MVVM。

        2
  •  0
  •   Brandon Minnick    6 年前

    下面是一个如何绑定 CommandParameterProperty A的 TapGestureRecognizer 到标签的 Text 使用C的属性#

    public MainPage()
    {
        var labelTappedGestureRecognizer = new TapGestureRecognizer
        {
            Command = new Command<string>(async labelText => await DisplayAlert("Label Tapped", labelText, "OK"))
        };
    
        var myLabel = new Label
        {
            Text = "This is my label",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center
        };
    
        myLabel.GestureRecognizers.Add(labelTappedGestureRecognizer);
        labelTappedGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding(nameof(Label.Text), source: myLabel));
    
        Content = myLabel;
    }
    
        3
  •  0
  •   Alex.Wei    6 年前

    "{Binding .}" 简单等于 new Binding(".") . 但是 CommandParameter="{Binding .}" 等于'tapgesturerecognizer.setbinding(tapgesturerecognizer.commandParameterProperty,new binding(“.”)。