c# – 在代码后面设置Xamarin.Forms绑定CommandParameter

我正试图在代码中设置TapGestureRecognizer的绑定,我无法找到正确的方法来做到这一点.工作的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的绑定.在xaml中,这只是{Binding}而没有其他参数.这是如何在代码中完成的?传入新的Binding()或this.BindingContext似乎不起作用.

最佳答案 CommandProperty绑定与您正在执行的操作相同.

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

要解决这个问题,你需要像Adam指出的那样指定Source属性.

请注意,但是如果您尝试分配的BindingContext是Null,我怀疑它在您的情况下,这仍然会引发异常.

下面示例中的Grid将BindingContext设置为视图模型(objGrid.BindingContext = objMyView2).

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

如果运行下面的演示,您将在“输出”窗口中看到一条调试消息,指示视图模型中的属性值.

        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);

辅助班: –

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));
        }
    }

}
点赞