我正在更改infoLabel2.Text=newValue的值.ToString();在后端C#,但显示不变



我有这个XAML

<t:InfoBulletLabel Text="To provide and maintain the Servicexxx" />

这个C#类:

public partial class InfoBulletLabel : Grid
{
public InfoBulletLabel()
{
Padding = new Thickness(0, 5);
SetDynamicResource(Label.MarginProperty, "InfoBulletMargin");
var grid = new Grid();
this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(14, GridUnitType.Star) });
this.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
var infoLabel1 = new InfoLabel() {
Text = "•"
};
var infoLabel2 = new InfoLabel();

Children.Add(infoLabel1, 0, 0);
Children.Add(infoLabel2, 1, 0);
}
private static void TextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var infoLabel2 = (InfoBulletLabel)bindable;
infoLabel2.Text = newValue.ToString();
}

public static readonly BindableProperty TextProperty =
BindableProperty.Create(nameof(Text), typeof(string), typeof(InfoBulletLabel), default(string), propertyChanged: TextPropertyChanged);
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
}

我在这里看到了正确的信息:

infoLabel2.Text = newValue.ToString();

但是显示没有改变。

有人知道怎么了吗?

供参考。这是InfoLabel类:

public class InfoLabel :Label
{
public InfoLabel()
{
this.SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
this.SetDynamicResource(Label.FontSizeProperty,   "InfoTextFontSize");
this.SetDynamicResource(Label.TextColorProperty,  "InfoLabelColor");
LineBreakMode = LineBreakMode.WordWrap;
VerticalOptions = LayoutOptions.Start;
HorizontalTextAlignment = TextAlignment.Start;
}
}

TextPropertyChanged方法中,bindable是InfoBulletLabel的对象类型,而不是InfoLabel:的类型

如果您想更改infoLabel2的文本,请将其作为公共属性:

public partial class InfoBulletLabel : Grid
{
public InfoLabel  infoLabel2;
private static void TextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var MyInfoBulletLabel = (InfoBulletLabel)bindable;
MyInfoBulletLabel.infoLabel2.Text = newValue.ToString();
}
}

你使用了相同的名字,这可能会引起一些误解。InfoBulletLabel是具有自定义属性TextGrid,而infoLabel2是具有Text属性的Label的类型。

您在网格中添加的标签需要是全局变量。

public partial class InfoBulletLabel : Grid
{
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(InfoBulletLabel), "Default", propertyChanged: TextPropertyChanged);
public static InfoLabel infoLabel1 = new InfoLabel();
public static InfoLabel infoLabel2 = new InfoLabel();
public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public InfoBulletLabel()
{
Padding = new Thickness(0, 5);
SetDynamicResource(MarginProperty, "InfoBulletMargin");
ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(14, GridUnitType.Star) });
RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
infoLabel1.Text = "•";
infoLabel2.Text = Text;
Children.Add(infoLabel1, 0, 0);
Children.Add(infoLabel2, 1, 0);
}
private static void TextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
infoLabel2.Text = newValue.ToString();
}

最新更新