我有一个ContentPage,在这里我创建了一个可绑定的属性,这样我就可以将一些信息从ViewModel传递给实现ContentPage的类。
为了更清楚地说明,类后面的代码定义了新的可绑定属性(ConnectionSettingsEnabled):
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...)
}XAML有以下代码:
<?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}">一切都进行得很好,但我刚刚升级到macOS的VS8.2,现在新的编辑器(VS for macOS终于用VS的相同引擎替换了他们可怕的XAML编辑器)抱怨说,属性ConnectionSettingsEnabled没有在ContentPage类型中找到(考虑一下,这实际上是有意义的)。
奇怪的是,所有的东西都是完美的编译和工作,但我不能不感到这不是实现我想要的东西的正确方式。
任何人都有一个示例,说明为单个ContentPage实现可绑定属性并在其XAML中引用它的正确方法是什么?
发布于 2019-07-26 09:20:46
事业:
ConnectionSettingsEnabled是ConnectionSettingsPage的一个属性,.which不是ContentPage的属性。
您可以访问其他页面,例如将其放在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>解决方案:
首先,创建
ContentPage的子类ConnectionSettingsPage。
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); }
}
//...
}在你的MainPage里
MainPage.xaml.cs
public partial class MainPage : ConnectionSettingsPage在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}">它可能会出现一些错误,因为IDE issue.You仍然可以构建它。
https://stackoverflow.com/questions/57201963
复制相似问题