自定义控件的可绑定 属性 未更新 - Xamarin Forms

Bindable property of custom control not updating - Xamarin Forms

我创建了一个自定义控件,其中包含一个将其文本绑定到可绑定的标签 属性。

XAML自定义控件:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestProject.CustomControls.MessageBar"
             HorizontalOptions="Fill"
             VerticalOptions="End"
             x:Name="this">
    <Frame BackgroundColor="{StaticResource MessageBarColor}"
           CornerRadius="0"
           Padding="0">
        <Label Text="{Binding MessageText, Source={x:Reference this}, Mode=OneWay}"
               LineBreakMode="TailTruncation"
               MaxLines="3"
               FontSize="13"
               TextColor="White"
               HorizontalOptions="Start"
               VerticalOptions="Center"
               Margin="20,16"></Label>
    </Frame>
</ContentView>

自定义控件的隐藏代码:

using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestProject.CustomControls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MessageBar : ContentView
    {
        public static readonly BindableProperty MessageTextProperty = BindableProperty.Create(
            propertyName: nameof(MessageText),
            returnType: typeof(string),
            declaringType: typeof(MessageBar),
            defaultValue: default(string));

        public string MessageText
        {
            get { return (string)GetValue(MessageTextProperty); }
            set { SetValue(MessageTextProperty, value); }
        }

        public MessageBar()
        {
            InitializeComponent();
        }
    }
}

现在我的问题是: 如果我在页面中使用控件并在 XAML 中设置 MessageText 属性,一切正常。显示文本。但是当我将 MessageText 属性 绑定到 ViewModel 时,没有显示任何文本。 MessageText 属性 根本没有设置。

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"
             xmlns:customControls="clr-namespace:TestProject.CustomControls"
             x:Class="TestProject.MVVM.Pages.ExamplePage">
    <StackLayout Orientation="Vertical">

        <!-- Works fine -->
        <customControls:MessageBar MessageText="This is a message!"></customControls:MessageBar>

        <!-- It does not work -->
        <customControls:MessageBar MessageText="{Binding Message, Mode=OneWay}"></customControls:MessageBar>
    </StackLayout>
</ContentPage>

页面的视图模型:

using Prism.Mvvm;

namespace TestProject.MVVM.ViewModels
{
    public class ExampleViewModel : BindableBase
    {
        private string _message;

        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public ExampleViewModel()
        {
            Message = "Message from viewModel!";
        }
    }
}

我做错了什么?

P.S.:

我已经找到了一些关于这个主题的帖子,但它们没有包含任何可行的解决方案。许多人建议为自定义控件命名并将 属性 与 x:Reference 绑定。如您所见,我这样做了,但它仍然不起作用。

您的自定义控件没有任何问题,我使用的是一个简单的模型,它在我这边运行良好。检查 ExamplePage.cs 中的 BindingContextmessage

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        ExampleViewModel vm = new ExampleViewModel();
        vm.Message = "test";
        BindingContext = vm;
    }
}

public class ExampleViewModel : INotifyPropertyChanged
{
    private string _message;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Message
    {
        set
        {
            if (_message != value)
            {
                _message = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Message"));
                }
            }
        }
        get
        {
            return _message;
        }
    }

    public ExampleViewModel()
    {
        Message = "Message from viewModel!";
    }
}

我上传了一个示例项目here

Hello, Have you register ViewModel with the view in App.cs file Like this

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
     containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
}