如果您的视图模型已经实现了
INotifyPropertyChanged
- 您可以尝试使用 null/空参数引发 PropertyChangedEvent - 这应该强制更新所有绑定属性 - 更多详细信息请参见此处。
public void RaiseAllProperties()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}
对我有用的 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 ...
}
}