[英]Xamarin.Forms - Data binding not working on updating UI
道歉,我已經看到了很多有關此問題的問題,但是在我的方案中我沒有成功實現其他解決方案。 我仍然對MVVM有所了解,在某些情況下,我可以進行數據綁定,但在其他情況下(如現在),我無法理解。
我找到的每個用於數據綁定的解決方案,人們總是建議確保OnPropertyChanged()被調用-這始終是我所研究的其他解決方案的答案,但就我而言,我已經有了這個解決方案,因此我非常困惑。 我知道綁定最初是在打開頁面時起作用的,而ICommand始終可以正常工作-它只是在更新UI的日期。
我正在做的是,允許用戶切換,這會將用戶導航到“操作系統設置”頁面(iOS)。 從那里他們可以根據需要進行一些更改,但是當它們返回我的應用程序時,我會仔細檢查它們是否按預期進行了更改,否則,如果他們沒有將切換開關恢復到我從檢查中得到的任何結果。 進入業務邏輯,這項工作很有魅力。 我的布爾值始終是我期望的值,但是UI從未反映出這一點。
SettingsPage.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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:behaviour="clr-namespace:OCS.Behaviours"
x:Class="OCS.Views.SettingsPage"
Title="Settings">
<ContentPage.Content>
<StackLayout>
<Switch VerticalOptions="Center" IsToggled="{Binding IsPushNotificationsAllowed}">
<Switch.Behaviors>
<behaviour:EventToCommandBehaviour EventName="Toggled" Command="{Binding PushNotificationsCommand}" />
</Switch.Behaviors>
</Switch>
</StackLayout>
</ContentPage.Content>
</ContentPage>
SettingsPage.xaml.cs
namespace OCS.Views {
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SettingsPage : ContentPage {
private SettingsViewModel settingsViewModel;
public SettingsPage() {
InitializeComponent();
settingsViewModel = new SettingsViewModel();
BindingContext = settingsViewModel;
}
protected override void OnAppearing() {
base.OnAppearing();
MessagingCenter.Unsubscribe<App>(this, "UpdateUI");
MessagingCenter.Subscribe<App>(this, "UpdateUI", (sender) => settingsViewModel.UpdateUI());
}
}
}
SettingsViewModel.cs:
namespace OCS.ViewModel {
public class SettingsViewModel : BaseViewModel {
private Boolean isPushNotificationsAllowed;
public Boolean IsPushNotificationsAllowed { get=> isPushNotificationsAllowed; set => SetValue(ref isPushNotificationsAllowed, value); }
public ICommand PushNotificationsCommand => new Command(OnPushNotificationToggled);
public SettingsViewModel() => UpdateUI();
public async void UpdateUI() {
IsPushNotificationsAllowed = await DependencyService.Get<IPermissions>().IsPushNotificationsEnabled();
}
private void OnPushNotificationToggled() {
Preferences.Set("OnSleepBlock", true);
DependencyService.Get<IPermissions>().OpenOSSettingsForPermissionChange();
}
}
}
BaseViewModel.cs
namespace OCS.ViewModel {
public class BaseViewModel {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] String propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected void SetValue<T>(ref T backingField, T value, [CallerMemberName] String propertyName = null) {
if (EqualityComparer<T>.Default.Equals(backingField, value)) {
return;
}
backingField = value;
OnPropertyChanged(propertyName);
}
private Boolean isBusy = false;
public Boolean IsBusy { get => isBusy; set => SetValue(ref isBusy, value); }
}
}
App.xaml.cs
protected override void OnResume() {
MessagingCenter.Send<App>(this, "UpdateUI");
}
正如miechooy在我的原始帖子的評論中指出的那樣,我在BaseViewModel.cs中缺少InotifyPropertyChanged
之前:
public class BaseViewModel { }
后:
public class BaseViewModel : INotifyPropertyChanged { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.