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



我想在内容页面中强制更新数据绑定属性。在这种情况下,是ContentPageTitle参数。

<ContentPage x:Class="Containers.Views.ContainerPage" 
xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="{Binding SomeStringProperty}"
Appearing="ContentPage_Appearing">

我得到的最接近的是这个,但它不起作用。

private void ContentPage_Appearing(object sender, EventArgs e)
{
this.BindingContext = null;
this.BindingContext = myClassInstance;
}

我不想实现onPropertyChange事件。我只想"刷新"视图的有界数据。

如果您的视图模型已经实现INotifyPropertyChanged-您可以尝试使用null/empty参数引发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 ...
}
}

最新更新