ContentPage 级别的可绑定属性 [英] Bindable property at ContentPage level
问题描述
我有一个 ContentPage,我在其中创建了一个可绑定的属性,以便我可以将一些信息从 ViewModel 传递到实现 ContentPage 的类.
为了更清楚,类背后的代码定义了新的可绑定属性(ConnectionSettingsEnabled):
公共部分类 ConnectionSettingsPage : ContentPage{公共静态只读 BindableProperty ConnectionSettingsEnabledProperty = BindableProperty.Create(nameof(ConnectionSettingsEnabled), typeof(bool), typeof(ConnectionSettingsPage), true, propertyChanged: HandleConnectionSettingsEnabledChanged);public bool ConnectionSettingsEnabled{获取{返回(布尔)GetValue(ConnectionSettingsEnabledProperty);}set { SetValue(ConnectionSettingsEnabledProperty, value);}}(...多一点代码...)}
XAML 有以下代码:
一切都很好,工作正常,但我刚刚升级到 macOS 的 VS8.2,现在新的编辑器(macOS 的 VS 终于用与 Windows 的 VS 相同的引擎替换了他们可怕的 XAML 编辑器)抱怨事实上,在 ContentPage 类型中找不到 属性 ConnectionSettingsEnabled(这实际上是有道理的,一旦你考虑过).
奇怪的是,一切都可以编译并完美运行,但我无法逃避这样的感觉,即这不是实现我想要的东西的正确方法.
任何人都有一个示例,说明为单个 ContentPage 实现可绑定属性并在其 XAML 中引用它的正确方法是什么?
原因:
ConnectionSettingsEnabled
是 ConnectionSettingsPage
的属性,不是 ContentPage
的属性.
你可以在其他页面访问,比如你把它放在TabbedPage中.
<local:ConnectionSettingsPage ConnectionSettingsEnabled="{Binding IsConnectionInfoEditable}"/></TabbedPage>
解决方案:
<块引用>首先,创建 ContentPage
的子类,名为 ConnectionSettingsPage
公共类 ConnectionSettingsPage : ContentPage{公共静态只读 BindableProperty ConnectionSettingsEnabledProperty = BindableProperty.Create(nameof(ConnectionSettingsEnabled), typeof(bool), typeof(ConnectionSettingsPage), true, propertyChanged: HandleConnectionSettingsEnabledChanged);私有静态无效 HandleConnectionSettingsEnabledChanged(BindableObject 可绑定,对象 oldValue,对象 newValue){//...}public bool ConnectionSettingsEnabled{获取{返回(布尔)GetValue(ConnectionSettingsEnabledProperty);}set { SetValue(ConnectionSettingsEnabledProperty, value);}}//...}
在你的主页
<块引用>MainPage.xaml.cs
公共部分类 MainPage : ConnectionSettingsPage
<块引用>
在 MainPage.xaml 中
它可能会因为IDE问题出现一些错误.您仍然可以构建它.
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?
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 .
这篇关于ContentPage 级别的可绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!