gpt4 book ai didi

c# - 在父级中设置绑定(bind)时,Xamarin.Forms 绑定(bind)到自定义控件不起作用

转载 作者:行者123 更新时间:2023-12-04 14:18:48 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Xamarin.Forms 自定义控件,但我遇到了绑定(bind)问题。

这是我最初的自定义控件:

<ContentView 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="CubisMobile.Controls.TestControl"
x:Name="TestControlView">
<Label Text="{Binding TestText}" />

public partial class TestControl : ContentView
{
public static readonly BindableProperty TestTextProperty = BindableProperty.Create(nameof(TestText), typeof(string), typeof(TestControl));
public string TestText
{
get { return (string)GetValue(TestTextProperty); }
set { SetValue(TestTextProperty, value); }
}

public TestControl()
{
InitializeComponent();

BindingContext = this;
}
}

我正尝试以这种方式使用它:

...
<StackLayout>
<controls:TestControl TestText="{Binding Title}" />
<Label Text="{Binding Title}" />
</StackLayout>
...

我添加了第二个标签来测试 Title 属性是否正常工作,确实如此。但文本不会显示在自定义控件上。当我设置一个像 TestText="Testing" 这样的常量值时,它会正常工作。我找到了 this answer在 StackOverflow 上,尝试了以下方法,但它也不起作用(自定义控件 XAML):

<ContentView 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="CubisMobile.Controls.TestControl"
x:Name="TestControlView">
<Label Text="{Binding Source={x:Reference TestControlView}, Path=TestText}" />

我真的不明白为什么这个绑定(bind)不起作用。

最佳答案

你找到的答案是好的,我在我的图书馆做了同样的事情:

<tabs:TabItem x:Class="Sharpnado.Presentation.Forms.CustomViews.Tabs.UnderlinedTabItem"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:tabs="clr-namespace:Sharpnado.Presentation.Forms.CustomViews.Tabs;assembly=Sharpnado.Presentation.Forms"
x:Name="RootLayout">

<ContentView.Content>
<Grid BackgroundColor="Transparent">

<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Label Style="{StaticResource TabTextHeader}"
FontFamily="{Binding Source={x:Reference RootLayout}, Path=FontFamily}"
FontSize="{Binding Source={x:Reference RootLayout}, Path=LabelSize}"
Text="{Binding Source={x:Reference RootLayout}, Path=Label}"
TextColor="{Binding Source={x:Reference RootLayout}, Path=UnselectedLabelColor}">

以及背后的代码:

    public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(
nameof(FontFamily),
typeof(string),
typeof(TabItem),
null,
BindingMode.OneWay);

public string FontFamily
{
get => (string)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}

我在您显示的代码中看到的唯一问题是 BindingContext 的设置:

public TestControl()
{
InitializeComponent();

BindingContext = this; // Remove this line
}

关于c# - 在父级中设置绑定(bind)时,Xamarin.Forms 绑定(bind)到自定义控件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57322010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com