在后面的代码中设置Xamarin.Forms绑定CommandParameter [英] Set Xamarin.Forms binding CommandParameter in code behind
问题描述
我正在尝试在代码中的TapGestureRecognizer
上设置绑定,但我想不出正确的方法.运行中的xaml看起来像这样...
I'm trying to set binding on a TapGestureRecognizer
in code and I can't figure out the right way to do it. The working xaml looks something like this...
<Grid>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LaunchLocationDetailsCommand}" CommandParameter="{Binding}" />
</Grid.GestureRecognizers>
</Grid>
在C#中,看起来像这样...
And in C#, it looks something like this...
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
似乎无效.
My confusion comes in on the binding of CommandParameterProperty
. In xaml, this simply {Binding}
with no other parameter. How is this done in code? Passing in new Binding()
or this.BindingContext
don't seem to work.
推荐答案
CommandProperty
绑定与您所做的相同.
The CommandProperty
binding is the same as you was doing.
由于您没有传递要使用的某些属性的路径,因此CommandParameterProperty
不能只是创建一个空的Binding
,因为它会引发异常.
As you are not passing in a path to some property to use, your CommandParameterProperty
can't just create an empty Binding
as it will throw an exception.
要解决此问题,您需要指定Adam指出的Source
属性.
To get around this you need to specify the Source
property as Adam has pointed out.
请注意,但是,如果您要分配的BindingContext
是 Null
(我怀疑是您的情况),则会仍然抛出异常
Note, however if the BindingContext
you are trying to assign is Null
, which I suspect it is in your case, this will still throw an exception.
以下示例中的Grid
将BindingContext
设置为视图模型(objGrid.BindingContext = objMyView2
).
The Grid
in the example below has the BindingContext
set to the view model (objGrid.BindingContext = objMyView2
).
然后,我们为命令参数创建一个新的绑定,Source
指向我们的视图模型(Source = objGrid.BindingContext
).
We are then creating a new binding for our command parameter, with the Source
pointing back to our view model (Source = objGrid.BindingContext
).
如果运行下面的演示,则会在Output
窗口中看到一条调试消息,指示视图模型中的属性值.
If you run the demo below, you will see a debug message in the Output
window indicating a property value from the view model.
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; }
}
启动命令:-
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));
}
}
}
这篇关于在后面的代码中设置Xamarin.Forms绑定CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!