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

时间:2016-02-06 05:05:48

标签: c# xamarin xamarin.forms code-behind

我正在尝试在代码中对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}而没有其他参数。这是如何在代码中完成的?传入new Binding()this.BindingContext似乎无效。

2 个答案:

答案 0 :(得分:2)

CommandProperty绑定与您正在进行的绑定相同。

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

要解决此问题,您需要指定Adam指出的Source属性。

请注意,如果您尝试分配BindingContext Null,我怀疑它是您的情况,那么仍然抛出异常。

以下示例中的GridBindingContext设置为视图模型(objGrid.BindingContext = objMyView2)。

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

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

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

}

答案 1 :(得分:0)

如果你的{Binding}内部没有任何内容,它就会绑定到绑定上下文并传递它。因此,您将它绑定到页面的默认绑定上下文。

创建新的Binding时设置源。

var binding = new Xamarin.Forms.Binding() { Source = this.BindingContext };