【问题标题】:DataTemplate with data binding from two sources具有来自两个源的数据绑定的 DataTemplate
【发布时间】:2020-06-20 17:46:18
【问题描述】:

我有一个带有图像的数据模板,该图像的 Source 属性绑定到默认源(可观察的集合,它工作正常)。问题是我需要将其 IsVisible 属性绑定到其他源(在我的代码中声明的对象)但是在运行应用程序时我在控制台上得到了这个:

Binding: 'ScrollEvent' property not found on 'Xamarin.Forms.Binding', target property: 'FFImageLoading.Forms.CachedImage.IsVisible'

我的代码的相关部分:

MyPage.xaml

<DataTemplate x:Key="MapMsgSend">

 ...

   <ffimageloading:CachedImage
        Source="{Binding imageSource}" 
        IsVisible="{Binding Source={Binding MyPage}, Path=ScrollEvent.Visibility}">
   </ffimageloading:CachedImage>

 ...

</DataTemplate>

MyPage.xaml.cs(相关部分)


namespace Project.XAML
{
    public partial class MyPage : ContentPage
    {
       public MyPage(){
         this.BindingContext=this;
       }
       public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };
    } 
}

编辑

public class IsScrolling : INotifyPropertyChanged
{
      private bool _ShowImage { get; set; }
      public bool ShowImage
      {
          get { return _ShowImage; }
          set
          {
              _ShowImage = value;
              NotifyPropertyChange("ShowImage");
          }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      void NotifyPropertyChange(string PropName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(PropName));
      }
}

//default value
public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };

【问题讨论】:

    标签: c# xamarin xamarin.forms data-binding xamarin-binding


    【解决方案1】:

    语法应该是这样的

    <ContentPage ... x:Name="page">
    ...
    
    <ffimageloading:CachedImage
        Source="{Binding imageSource}" 
        IsVisible="{Binding Source={x:Reference page},Path=ScrollEvent.Visibility}"
    

    您只能绑定到公共属性

    这是公开的,但不是属性

    public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };
    

    它需要 get 和/或 set 成为属性,就像这样

    public IsScrolling ScrollEvent { get; set; } = new IsScrolling() { ShowImage = true };
    

    【讨论】:

    • 我毫不怀疑你知道如何做到这一点,你已经帮助了我很多次。但我完全按照你说的做。我现在才把它搬回来。它崩溃异常:Xamarin.Forms.Xaml.XamlParseException: Position 282:57. Can not find the object referenced by `Page` at Xamarin.Forms.Xaml.ReferenceExtension.ProvideValue (System.IServiceProvider serviceProvider) [0x000bf] in D:\a\1\s\Xamarin.Forms.Xaml\MarkupExtensions\ReferenceExtension.cs:42 at MasterDetailPageNavigation.XAML.Page+&lt;InitializeComponent&gt;_anonXamlCDataTemplate_8.LoadDataTemplate () [0x00a2c] in
    • 我会接受你的回答,因为这可能是正确的做法,但如果你能帮助我更多地找出它崩溃的原因,我会非常高兴。
    • 错误信息非常清楚 - “位置 282:57。找不到 Page 引用的对象”。您是否使用与您的名字一致的命名和大小写?您可能想使用不同的名称,因为“页面”可能与某些内置类型冲突。
    • 其实名字是ChatPage,我改名是为了发布问题并保留它不会引起混淆。当然,它存在......这是我想要进行绑定的同一页面。
    • 您在x:Name 中使用的名称是否与您在x:Reference 中使用的名称匹配?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    相关资源
    最近更新 更多