gpt4 book ai didi

c# - Xamarin 中的 TapGesture From Bound 属性

转载 作者:行者123 更新时间:2023-11-28 21:46:58 25 4
gpt4 key购买 nike

好的,我有一个绑定(bind)到 viewModel 的 ViewCell 类。如果我创建一个标签,我可以这样做:

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

我想做的是将对象的值绑定(bind)到 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 中绑定(bind)我的 TapGestureRecognizer:

tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandProperty, vm => vm.DialPhoneCommand);
tapGesturePhone.SetBinding<ProjectContactsViewModel> (TapGestureRecognizer.CommandParameterProperty,vm => vm.PhoneNo);

从这里开始,我只需为 ViewModel 创建一个 Setter 和 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 绑定(bind)到它。

绑定(bind)方式相同

 taskName.SetBinding(Label.TextProperty, "CompanyName");

您可以绑定(bind)点击手势的命令

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

关于c# - Xamarin 中的 TapGesture From Bound 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29751983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com