Issue
Here is the error message I am seeing in the Application output:
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);
}
The warning message appears but the code seems to work. Does anyone have an idea what might be causing the warning message?
Solution
How to avoid the "Warning"
Change the constructor of HomeTabPage
to
public HomeTabPage()
{
// Set the BindingContext before the page is created!
BindingContext = vm = new HomeTabViewModel();
InitializeComponent();
}
Why the "Warning" occurs?
On the code you posted, when you create HomeTabPage
like
public HomeTabPage()
{
InitializeComponent();
BindingContext = vm = new HomeTabViewModel();
}
what happens is that HomeTabPage
is being initialized before you set the BindingContext
for that Page
... but the initializer does find a BindingContext
for its base Page
HeadingScrollableView
, namely the one you set as
public HeadingScrollableView()
{
InitializeComponent();
BindingContext = this;
}
and thus, since the BindingContext
is set to this
what the initializer does is to look for the path GearTapBtnCmd
in the source which is HomeTabPage
or any of the objects it inherits from and it does not find it: then the "Warning" is shown.
Later on the BindingContext
of HomeTabPage
is set, and everything works as expected.
When you do as i suggest (set the BindingContext
before the Page
is initialized) GearTapBtnCmd
is found immediately, and no warning is shown.
Why the "Warning" occurs only on Android?
Sadly i have not an answer for that question, but it is noteworthy to see that the warning comes only when debugging, not if you rebuild the project (!). That means that the debugger for Android, which should be fundamentally different from that of iOS, is behind the warning... To find out exactly why this happens only in Android you could ask directly in GitHub to the Xamarin.Forms developer team... (and let us know if you find out).
But as for your question
Does anyone have an idea what might be causing the warning message?
i think i might already have given you an answer :D
Answered By - Mönchskopf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.