如何强制View更新Xamarin Forms中的绑定属性?

问题描述 投票:0回答:2
c# xaml xamarin data-binding xamarin.forms
2个回答
7
投票

如果您的视图模型已经实现了

INotifyPropertyChanged
- 您可以尝试使用 null/空参数引发 PropertyChangedEvent - 这应该强制更新所有绑定属性 - 更多详细信息请参见此处

public void RaiseAllProperties()
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}

0
投票

对我有用的 CustomView 方法是:

using Xamarin.Forms;

namespace Xam.CustomViews.ContentsViews
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class FeatherButton : ContentView
    {
        // ... Existing code ...

        public FeatherButton()
        {
            InitializeComponent();
            PropertyChanged += OnPropertyChanged;
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == StyleProperty.PropertyName)
            {
                UpdateVisualProperties();
            }
        }

        private void UpdateVisualProperties()
        {
            OnPropertyChanged(nameof(TextColor));
            OnPropertyChanged(nameof(BackgroundColor));
            OnPropertyChanged(nameof(BorderColor));
            OnPropertyChanged(nameof(BorderWidth));
        }

        // ... Existing code ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.