Reputation: 5900
I have a ContentPage where I've created a bindable property, so that I can pass some info from the ViewModel to the class that implements the ContentPage.
To make it clearer, the code behind class has new bindable property (ConnectionSettingsEnabled) defined:
public partial class ConnectionSettingsPage : ContentPage
{
public static readonly BindableProperty ConnectionSettingsEnabledProperty = BindableProperty.Create(nameof(ConnectionSettingsEnabled), typeof(bool), typeof(ConnectionSettingsPage), true, propertyChanged: HandleConnectionSettingsEnabledChanged);
public bool ConnectionSettingsEnabled
{
get { return (bool)GetValue(ConnectionSettingsEnabledProperty); }
set { SetValue(ConnectionSettingsEnabledProperty, value); }
}
(... a bit more code...)
}
And the XAML has this code:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Pages.Settings.ConnectionSettingsPage"
Title="{me:TranslateExtension Text=Server}"
ConnectionSettingsEnabled="{Binding IsConnectionInfoEditable}">
All was well and working, but I've just upgraded to VS8.2 for macOS, and now the new editor (VS for macOS has finally replaced their horrible XAML editor with the same engine as VS for Windows) complains about the fact that the property ConnectionSettingsEnabled was not found in type ContentPage (which actually makes sense, once you think about it).
The weird thing is that everything compiles and works perfectly, but I can't escape the feeling that this is not the proper way to implement what I wanted.
Anyone has an example of what would be the proper way to implement a bindable property for a single ContentPage and reference it in its XAML?
Upvotes: 0
Views: 820
Reputation: 18861
Cause :
ConnectionSettingsEnabled
is a property of ConnectionSettingsPage
,.which is not the property of ContentPage
.
You can access in other page,for example you put it in TabbedPage.
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage"
x:Class="TabbedPageWithNavigationPage.MainPage">
<local:ConnectionSettingsPage ConnectionSettingsEnabled="{Binding IsConnectionInfoEditable}" />
</TabbedPage>
Solution:
firstly,create s subclass of
ContentPage
calledConnectionSettingsPage
public class ConnectionSettingsPage : ContentPage
{
public static readonly BindableProperty ConnectionSettingsEnabledProperty = BindableProperty.Create(nameof(ConnectionSettingsEnabled), typeof(bool), typeof(ConnectionSettingsPage), true, propertyChanged: HandleConnectionSettingsEnabledChanged);
private static void HandleConnectionSettingsEnabledChanged(BindableObject bindable, object oldValue, object newValue)
{
//...
}
public bool ConnectionSettingsEnabled
{
get { return (bool)GetValue(ConnectionSettingsEnabledProperty); }
set { SetValue(ConnectionSettingsEnabledProperty, value); }
}
//...
}
And in your MainPage
MainPage.xaml.cs
public partial class MainPage : ConnectionSettingsPage
in MainPage.xaml
<local:ConnectionSettingsPage
xmlns:local="clr-namespace:App11" xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="xxx.MainPage"
ConnectionSettingsEnabled="{Binding xxx}">
It maybe will appear some errors like because of IDE issue.You can still build it .
Upvotes: 1