找不到XF绑定..属性,仅适用于我的Android应用程序,而不适用于iOS

问题描述

这是我在应用程序输出中看到的错误消息:

Binding: 'GearTapBtnCmd' property not found on 'Memorise.HoMetabPage',target property: 'Memorise.HoMetabPage.RightIconTapCommand'

HoMetabPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<t:headingScrollableView
    PageTitle="Home"
    PageTitleVisible="True"
    RightIconSource="{DynamicResource GearIcon}"
    RightIconTapCommand="{Binding GearTapBtnCmd,Mode=OneWay}"

HoMetabPage.xaml.cs

public partial class HoMetabPage : headingScrollableView
{
    public HoMetabviewmodel vm;

    public HoMetabPage()
    {
        InitializeComponent();
        BindingContext = vm = new HoMetabviewmodel();
    }

HoMetabviewmodel

public partial class HoMetabviewmodel : Baseviewmodel
{
    public IAsyncCommand GearTapBtnCmd { get; private set; }
    
    public HoMetabviewmodel()
    {
        GearTapBtnCmd = new MvvmHelpers.Commands.AsyncCommand(OpenPrefAsync);
    }

HeadinsScrollableView.xaml

<?xml version="1.0" encoding="UTF-8"?>
<t:headingViewBase 
    Shell.NavBarIsVisible="false"
    x:Class="Memorise.Templates.headingScrollableView"
    x:Name="ContentPage"
    xmlns:ffimageloadingsvg="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
    xmlns:t="clr-namespace:Memorise.Templates"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns="http://xamarin.com/schemas/2014/forms"  >
    <t:headingViewBase.Content>
       <Grid 
          <Grid.GestureRecognizers>
             <TapGestureRecognizer Command="{Binding Source={x:Reference ContentPage},Path=RightIconTapCommand}"/>
          </Grid.GestureRecognizers>
       </Grid>
    </t:headingViewBase.Content>
</t:headingViewBase>

headingScrollableView

[contentproperty(nameof(InnerContent))]
public partial class headingScrollableView : headingViewBase
{
    public headingScrollableView()
    {
        InitializeComponent();
        BindingContext = this;
    }

    public static readonly BindableProperty RightIconTapCommandProperty =    
             BindableProperty.Create(nameof(RightIconTapCommand),typeof(AsyncCommand),typeof(headingScrollableView),default(MvvmHelpers.Commands.Command));

    public AsyncCommand RightIconTapCommand
    {
        get => (AsyncCommand)GetValue(RightIconTapCommandProperty);
        private set => SetValue(RightIconTapCommandProperty,value);
    }

出现警告消息,但代码似乎起作用。是否有人知道导致警告消息的原因?

解决方法

如何避免“警告”

HomeTabPage的构造函数更改为

public HomeTabPage()
{
    // Set the BindingContext before the page is created!
    BindingContext = vm = new HomeTabViewModel(); 
    InitializeComponent();
}

为什么会发生“警告”?

在您发布的代码上,创建HomeTabPage之类的

public HomeTabPage()
{
    InitializeComponent();
    BindingContext = vm = new HomeTabViewModel(); 
}

发生的情况是,在为HomeTabPage设置BindingContext之前,Page正在被初始化...但是初始化程序确实为其基数{{ 1}} BindingContext,即您设置为的那个

Page

,因此,由于HeadingScrollableView设置为public HeadingScrollableView() { InitializeComponent(); BindingContext = this; } ,因此初始化程序要做的是在 source path BindingContext / em>是this或它继承的任何对象,但找不到它:然后显示“警告”。

GearTapBtnCmd的{​​{1}}上已设置完毕,一切正常。

按照我的建议进行操作(在HomeTabPage初始化之前设置BindingContext)会立即发现HomeTabPage,并且不会显示警告。

为什么“警告”仅在Android上发生?

可悲的是我没有这个问题的答案,但是值得注意的是,警告仅在调试时出现,而不是在重建项目(!)时才出现。这就意味着 Android 的调试器(应该与 iOS 的根本不同)在警告的后面……要确切地了解为什么仅在 > Android ,您可以直接在 GitHub 中向 Xamarin.Forms 开发人员小组询问...(如果发现,请告知我们)。


但是关于你的问题

有人知道导致警告消息的原因是什么吗?

我想我可能已经给你答案了:D