来自绑定的 Xamarin 中的 TapGesture 属性
TapGesture in Xamarin From Bound property
好的,我有一个绑定到 viewModel 的 ViewCell class。如果我创建一个标签,我可以这样做:
var taskName = new Label()
{
XAlign = TextAlignment.Start,
YAlign = TextAlignment.Start,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
FontAttributes = FontAttributes.Bold,
LineBreakMode = LineBreakMode.TailTruncation
};
taskName.SetBinding(Label.TextProperty, "CompanyName");
我要做的是将对象的值绑定到 tapGesture。
var tapGesturePhone = new TapGestureRecognizer ();
var phoneIcon = new Image {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Source = "phone.png"
};
phoneIcon.GestureRecognizers.Add (tapGesturePhone);
tapGesturePhone.Tapped += (sender, e) => {
var uri = new Uri("tel:" + );
Device.OpenUri(uri);
};
//uri.setbinding("Uri.text", "PhoneNo")?
关于如何执行此操作的任何想法?解决办法也可以。
我有一个名为 ProjectContacts
的模型,ProjectContactsViewModel
设置为 BindingContext
。
我的 ContentView 看起来像这样:
public ProjectContactsView ()
{
//bind the view model to the context
this.BindingContext = _viewModel;
_list.ItemTemplate = new DataTemplate(typeof(ProjectContactsListCell));
_list.IsGroupingEnabled = true;
_list.GroupDisplayBinding = new Binding("Key");
_list.GroupHeaderTemplate = new DataTemplate(typeof(ProjectContactsListHeaderCell));
_list.HasUnevenRows = true;
}
Solution
感谢@Sten Petrov 我设法解决了这个问题。我最终不得不在 ViewCell
中声明我的 ProjectContactsViewModel 的新实例
private TapGestureRecognizer tapGesturePhone = new TapGestureRecognizer ();
tapGesturePhone.BindingContext = new ProjectContactsViewModel ();
然后我以这种方式在 ViewCell 中绑定我的 TapGestureRecognizer:
tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandProperty, vm => vm.DialPhoneCommand);
tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandParameterProperty,vm => vm.PhoneNo);
从这里我只需要为 ViewModel 创建一个 Setter and Getter
并在构造函数中初始化命令:
public Command DialPhoneCommand {get;set;}
public string PhoneNo { get {return "http://google.com"; } set{ PhoneNo = value;}}
public ProjectContactsViewModel ()
{
//Assign the dialcommand.
DialPhoneCommand = new Command((phoneNo)=>
Device.OpenUri(new Uri(phoneNo.ToString())));
}
您可以在 ProjectContactsViewModel
中创建一个 Command
并将 tapGesture.Command 绑定到它。
绑定方式相同
taskName.SetBinding(Label.TextProperty, "CompanyName");
可以绑定点击手势命令
var tapGesturePhone = new TapGestureRecognizer ();
tapGesturePhone.SetBinding(TapGestureRecognizer.CommandProperty,"DialPhoneCommand");
tapGesturePhone.SetBinding(TapGestureRecognizer.CommandParameterProperty,"PhoneNo");
var phoneIcon = new Image {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Source = "phone.png"
};
phoneIcon.GestureRecognizers.Add (tapGesturePhone);
该命令可以在 ProjectContactsViewModel
中初始化(为简单起见):
... ProjectContactsViewModel {
public string PhoneNo {get;set;}
public Command DialPhoneCommand {get;set;}
...
public ProjectContactsViewModel(){
DialPhoneCommand = new Command((phoneNo)=>
Device.OpenUri(new Uri("tel:" + phoneNo ));
);
}
}
好的,我有一个绑定到 viewModel 的 ViewCell class。如果我创建一个标签,我可以这样做:
var taskName = new Label()
{
XAlign = TextAlignment.Start,
YAlign = TextAlignment.Start,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
FontAttributes = FontAttributes.Bold,
LineBreakMode = LineBreakMode.TailTruncation
};
taskName.SetBinding(Label.TextProperty, "CompanyName");
我要做的是将对象的值绑定到 tapGesture。
var tapGesturePhone = new TapGestureRecognizer ();
var phoneIcon = new Image {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Source = "phone.png"
};
phoneIcon.GestureRecognizers.Add (tapGesturePhone);
tapGesturePhone.Tapped += (sender, e) => {
var uri = new Uri("tel:" + );
Device.OpenUri(uri);
};
//uri.setbinding("Uri.text", "PhoneNo")?
关于如何执行此操作的任何想法?解决办法也可以。
我有一个名为 ProjectContacts
的模型,ProjectContactsViewModel
设置为 BindingContext
。
我的 ContentView 看起来像这样:
public ProjectContactsView ()
{
//bind the view model to the context
this.BindingContext = _viewModel;
_list.ItemTemplate = new DataTemplate(typeof(ProjectContactsListCell));
_list.IsGroupingEnabled = true;
_list.GroupDisplayBinding = new Binding("Key");
_list.GroupHeaderTemplate = new DataTemplate(typeof(ProjectContactsListHeaderCell));
_list.HasUnevenRows = true;
}
Solution
感谢@Sten Petrov 我设法解决了这个问题。我最终不得不在 ViewCell
private TapGestureRecognizer tapGesturePhone = new TapGestureRecognizer ();
tapGesturePhone.BindingContext = new ProjectContactsViewModel ();
然后我以这种方式在 ViewCell 中绑定我的 TapGestureRecognizer:
tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandProperty, vm => vm.DialPhoneCommand);
tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandParameterProperty,vm => vm.PhoneNo);
从这里我只需要为 ViewModel 创建一个 Setter and Getter
并在构造函数中初始化命令:
public Command DialPhoneCommand {get;set;}
public string PhoneNo { get {return "http://google.com"; } set{ PhoneNo = value;}}
public ProjectContactsViewModel ()
{
//Assign the dialcommand.
DialPhoneCommand = new Command((phoneNo)=>
Device.OpenUri(new Uri(phoneNo.ToString())));
}
您可以在 ProjectContactsViewModel
中创建一个 Command
并将 tapGesture.Command 绑定到它。
绑定方式相同
taskName.SetBinding(Label.TextProperty, "CompanyName");
可以绑定点击手势命令
var tapGesturePhone = new TapGestureRecognizer ();
tapGesturePhone.SetBinding(TapGestureRecognizer.CommandProperty,"DialPhoneCommand");
tapGesturePhone.SetBinding(TapGestureRecognizer.CommandParameterProperty,"PhoneNo");
var phoneIcon = new Image {
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Source = "phone.png"
};
phoneIcon.GestureRecognizers.Add (tapGesturePhone);
该命令可以在 ProjectContactsViewModel
中初始化(为简单起见):
... ProjectContactsViewModel {
public string PhoneNo {get;set;}
public Command DialPhoneCommand {get;set;}
...
public ProjectContactsViewModel(){
DialPhoneCommand = new Command((phoneNo)=>
Device.OpenUri(new Uri("tel:" + phoneNo ));
);
}
}