【问题标题】:Bind an object in xaml在 xaml 中绑定对象
【发布时间】:2018-03-20 16:17:01
【问题描述】:

我正在尝试使用它的 x:Name 将整个对象绑定到另一个对象中的属性。例如

<local:customImage Tag="{Binding x:Name}"/>

我该如何实现这一目标?

更新:我尝试实施这些建议,但没有成功。我说的xaml如下:

<local:StarBehavior x:Name="starOne" GroupName="myStar"/>
    <local:StarImage BindingContext="{x:Reference starOne}" starTag="{Binding Name}" x:Name="starBlankOne" Source="rating-stars/star_outline.png">
        <local:StarImage.GestureRecognizers>
            <TapGestureRecognizer Tapped="Star_Tapped"/>
        </local:StarImage.GestureRecognizers>
    </local:StarImage>
</Grid>

也许我应该尝试将 starBehaviour 移到 c# 中,因为它不是视觉元素?

P.S 我要做的只是将 startTag 设置为一个对象,所以如果我可以在不绑定的情况下执行此操作,我不介意是否有解决方案。

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    要绑定对象,您只需添加对象的名称。 您的财产必须是公开的,即

    public ObjectType Name {get;set;}
    
    <local:customImage Tag="{Binding Name}"/>
    

    或者如果你想绑定你当前的绑定上下文,你可以像这样使用它

    <local:customImage Tag="{Binding .}"/>
    

    您的属性“标签”也必须是可绑定属性。

    【讨论】:

    • 我想将对象绑定到一个属性上。所以在代码中,starTag 将等于在 xaml 中设置的对象
    • 您是否将 BindingContext 设置为您的 local:StarBehavior ?还是你想通过 BindingContext 传递 local:StarBehavior 控制?
    • 我想通过上下文传递 StarBehavior 以便 starTag = StarBehavior
    【解决方案2】:

    x:名称引用正在使用的组件的名称。 绑定与组件名称无关,应该是这样的

    例如:

    <local: customImage x: Name = "NameOfComponent" Tag = "{Binding Name}" />
    

    【讨论】:

      【解决方案3】:

      您的视图必须具有包含(公共)属性“名称”的对象的绑定上下文

      <local:customImage Tag="{Binding Name}"/>
      

      编辑:尝试像这样声明您的属性,它与您所做的很接近,它只是类型更安全(使用 nameof(...) 运算符),并且可能涵盖一些您没有正确编写的警告。

       public static readonly BindableProperty starTagProperty = BindableProperty.Create(propertyName: nameof(starTag),
                                                                                                      returnType: typeof(StarBehaviour),
                                                                                                      defaultValue: null,
                                                                                                      declaringType: typeof(TheClassYouDeclareThisIn),
                                                                                                      defaultBindingMode: BindingMode.TwoWay,
                                                                                                      propertyChanged: OnStarTagChanged);
      
              public StarBehaviour starTag
              {
                  get { return (StarBehaviour)GetValue(starTagProperty); }
                  set { SetValue(starTagProperty, value); }
              }
      
      private static void OnStarTagChanged(BindableObject bindable, object oldValue, object newValue)
              {
                  if (bindable is TheClassYouDeclareThisIn starTagDeclarer && newValue is StarBehaviour newStarTag)
                  {
      //Does this get called if u change up the starTag property? test it
                  }
              }
      

      【讨论】:

      • 它没有用,但我修改了原始帖子以包含更多信息
      • 好的,现在我明白了,“starTag”可能不是“BindableProperty”,因此您无法将任何内容绑定到它。阅读与 xamarin 的基本数据绑定,您似乎错过了一些东西。
      • 我相信是的。由于它不是可绑定的属性,我曾经遇到过错误,但我使用了以下代码: using System;使用 System.Collections.Generic;使用 System.Text;使用 Xamarin.Forms;命名空间 SalApp { public class StarImage : Image { public static readonly BindableProperty startTagProperty = BindableProperty.Create("starTag", typeof(StarBehavior), typeof(StarImage), null);公共 StarBehavior starTag { 获取;放; } } }
      • 是的,您的属性 startTag 似乎缺少可绑定属性正常运行所需的 SetValue() 和 GetValue() 方法。检查我的编辑
      猜你喜欢
      • 1970-01-01
      • 2015-09-11
      • 2012-02-06
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多