这是我在应用程序输出中看到的错误消息:
Binding: 'GearTapBtnCmd' property not found on 'Memorise.HomeTabPage',
target property: 'Memorise.HomeTabPage.RightIconTapCommand'
首页TabPage.xaml <?xml version="1.0" encoding="UTF-8"?>
<t:HeadingScrollableView
PageTitle="Home"
PageTitleVisible="True"
RightIconSource="{DynamicResource GearIcon}"
RightIconTapCommand="{Binding GearTapBtnCmd, Mode=OneWay}"
首页TabPage.xaml.cs public partial class HomeTabPage : HeadingScrollableView
{
public HomeTabViewModel vm;
public HomeTabPage()
{
InitializeComponent();
BindingContext = vm = new HomeTabViewModel();
}
首页TabViewModel 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
...但初始化程序确实找到了 BindingContext
用于其底座 Page
HeadingScrollableView
,即您设置的那个public HeadingScrollableView()
{
InitializeComponent();
BindingContext = this;
}
因此,由于 BindingContext
设置为 this
初始化程序所做的是寻找路径 GearTapBtnCmd
在来源是 HomeTabPage
或它继承的任何对象但它没有找到它:然后显示“警告”。稍后在
BindingContext
的 HomeTabPage
已设置,一切都按预期进行。当您按照我的建议进行操作时(在初始化
BindingContext
之前设置 Page
)GearTapBtnCmd
立即被发现,并且不显示任何警告。为什么“警告”只出现在 Android 上?
遗憾的是,我没有这个问题的答案,但值得注意的是,警告仅在调试时出现,而不是在您重建项目 (!) 时出现。这意味着 的调试器安卓 ,这应该与 的根本不同iOS , 是在警告后面...要确切地找出这种情况仅发生在 中的原因安卓您可以直接在中询问GitHub 到 Xamarin.Forms 开发团队......(如果你发现了,请告诉我们)。
但至于你的问题
Does anyone have an idea what might be causing the warning message?
我想我可能已经给了你答案:D
关于xamarin - XF Binding .. 未找到属性,但仅适用于我的 Android 应用程序,而不适用于 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64014888/