我试图在代码中的TapGestureRecognizer上设置绑定(bind),但我想不出正确的方法。正常工作的xaml看起来像这样...

<Grid>
    <Grid.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LaunchLocationDetailsCommand}"
                              CommandParameter="{Binding}" />
    </Grid.GestureRecognizers>
</Grid>
在C#中,看起来像这样...
var gridTap = new TapGestureRecognizer();

gridTap.SetBinding(TapGestureRecognizer.CommandProperty,
                   new Binding("LaunchLocationDetailsCommand"));

gridTap.SetBinding(TapGestureRecognizer.CommandParameterProperty,
                   new Binding(/* here's where I'm confused */));

var grid = new Grid();
grid.GestureRecognizers.Add(gridTap);
我对CommandParameterProperty的绑定(bind)感到困惑。在xaml中,这只是{Binding},没有其他参数。如何在代码中完成?传递new Binding()this.BindingContext似乎无效。

最佳答案

CommandProperty绑定(bind)与您所做的相同。

由于您没有传递要使用的某些属性的路径,因此CommandParameterProperty不能仅创建一个空的Binding,因为它会引发异常。

为了解决这个问题,您需要指定Adam指出的Source属性。

请注意,但是,如果您尝试分配BindingContext Null,我怀疑是您的情况,这将使仍然抛出异常。

下例中的GridBindingContext设置为 View 模型(objGrid.BindingContext = objMyView2)。

然后,我们为命令参数创建一个新的绑定(bind),Source指向我们的 View 模型(Source = objGrid.BindingContext)。

如果运行下面的演示,则会在Output窗口中看到一条调试消息,指示 View 模型中的属性值。

        MyView2 objMyView2 = new MyView2();
        objMyView2.SomeProperty1 = "value1";
        objMyView2.SomeProperty2 = "value2";

        objMyView2.LaunchLocationDetailsCommand_WithParameters = new Command<object>((o2)=>
        {
            LaunchingCommands.LaunchLocationDetailsCommand_WithParameters(o2);
        });

        Grid objGrid = new Grid();
        objGrid.BindingContext = objMyView2;
        objGrid.HeightRequest = 200;
        objGrid.BackgroundColor = Color.Red;

        TapGestureRecognizer objTapGestureRecognizer = new TapGestureRecognizer();
        objTapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("LaunchLocationDetailsCommand_WithParameters"));

        Binding objBinding1 = new Binding()
        {
            Source = objGrid.BindingContext
        };
        objTapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, objBinding1);
        //
        objGrid.GestureRecognizers.Add(objTapGestureRecognizer);

支持类(class):

MyView2 :-
public class MyView2
    : ViewModelBase
{
    public string SomeProperty1 { get; set; }
    public string SomeProperty2 { get; set; }

    public ICommand LaunchLocationDetailsCommand_WithParameters { get; set; }
}

LaunchingCommands :-
public static class LaunchingCommands
{
    public static void LaunchLocationDetailsCommand_WithParameters(object pobjObject)
    {
        System.Diagnostics.Debug.WriteLine("SomeProperty1 = " + (pobjObject as MyView2).SomeProperty1);
    }
}

ViewModelBase :-
public abstract class ViewModelBase
    : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string pstrPropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(pstrPropertyName));
        }
    }

}

10-08 03:29