移出 ContentPage 并再次移回后出现 Xamarin 标签文本

Xamarin Label Text Appears after moving out of the ContentPage and Moving back to it again

我有一个来自 ContentView 的字符串

public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(nameof(LabelText), typeof(string), typeof(CustomToolBar), string.Empty);

这是它的名字。

<Label Text="{Binding LabelText}" TextColor="#FFFFFF" 
                       FontAttributes="Bold" HorizontalOptions="CenterAndExpand"  
                       FontSize="16" Margin="10,13,0,0" x:Name="LocationName"
                       LineBreakMode="TailTruncation">
                </Label>

内容页initiated/used的内容如下

 <local:CustomToolBar CustomImageSource="baseline_arrow_back_black_24.png" 
                                     NavigationBarColor="#FF8800" BorderColor="#FF8800"
                                     CustomBackButtonCommand="{Binding OnBackButtonClicked}" Margin="10,0,0,0" 
                                     LabelText="{Binding SearchedLocationName}"/>

问题是 LabelText 是不可见的,直到我移出该页面并再次移回,这时 Label 才出现。

下面是我如何绑定 SearchedLocationName

public string _selectedLocation;

        public string SearchedLocationName
        {
            get
            {
                return _selectedLocation;
            }
            set
            {
                if (_selectedLocation != value)
                {
                    _selectedLocation = value;
                    OnPropertyChanged("SearchedLocationName");
                }
            }
        }


public  void DoSetups(string MainText)
        {
            SearchedLocationName = MainText;
        }

您可以使用下面的代码。在 propertyChanged 中设置 LabelText 的值,而不是在 CustomToolBar 中进行绑定。

自定义工具栏

Xaml:

 <StackLayout>
        <Label  TextColor="Black" 
                   FontAttributes="Bold" HorizontalOptions="CenterAndExpand"  
                   FontSize="16" Margin="10,13,0,0" x:Name="LocationName"
                   LineBreakMode="TailTruncation">
        </Label>
    </StackLayout>

后面的代码:

 public partial class CustomToolBar : ContentView
{
    public static readonly BindableProperty LabelTextProperty = BindableProperty.Create(
        nameof(LabelText),
        typeof(string),
        typeof(CustomToolBar),
        string.Empty,
        BindingMode.TwoWay, 
        null,
        propertyChanged);

    private static void propertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var cView = (CustomToolBar)bindable;
        cView.LocationName.Text = (string)newValue;
    }


    public string LabelText
    {
        get => (string)GetValue(LabelTextProperty);
        set => SetValue(LabelTextProperty, value);
    }


    public CustomToolBar()
    {
        InitializeComponent();
     
    }
}

用法:

   <local:CustomToolBar LabelText="{Binding SearchedLocationName}"/>

后面的代码:

 public string _selectedLocation;

    public string SearchedLocationName
    {
        get
        {
            return _selectedLocation;
        }
        set
        {
            if (_selectedLocation != value)
            {
                _selectedLocation = value;
                OnPropertyChanged("SearchedLocationName");
            }
        }
    }

    public Page1()
    {
        InitializeComponent();
        SearchedLocationName = "Helo";

        this.BindingContext = this;
    }