![](/img/trans.png)
[英]PopAsync not working when i use it in a TabbedPage ViewModels
[英]Is there a way to update bounded data when I swipe back [PopAsync() ]
為了提供一些上下文,我正在編寫一個 Xamarin.Forms 應用程序並利用 INotifyPropertyChanged 進行數據綁定。 目前,我在按鈕上顯示了一個庫存計數器。 此按鈕上的文本顯示有界的“計數”變量(例如,當前庫存:35)。 當我按下按鈕時,我將一個屏幕推送到導航堆棧上,它允許我編輯這個“計數”變量。 我像這樣使用 class 實現
public class UserInventory : INotifyPropertyChanged
{
private int count = 0;
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public int Count
{
get => Preferences.Get(nameof(Count),0);
set
{
if (count == value || value <1)
return;
Preferences.Set(nameof(Count), value);
//count = value;
//Application.Current.Properties["Count"] = count;
OnPropertyChanged(nameof(Count));
//OnPropertyChanged(nameof(displayName));
}
}
public UserInventory()
{
}
void OnPropertyChanged(string count)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(count));
}
}
我根據 Xamarin 上的教程在<ContentPage.BindingContext> <local:UserInventory /> </ContentPage.BindingContext>
中添加這個 class推送新頁面。 問題是當我在 iOS 上滑動時,上一個帶有“當前庫存:35”按鈕的屏幕沒有更新以反映新的變化。 如果我按下該屏幕,則會反映更改。
當您返回 go (PopAsync()) 時,是否有確保更新有界數據?
嘗試覆蓋頁面的 OnAppearing() 方法並從那里調用 OnPropertyChanged。
假設“UserInventory”綁定的虛擬機.....
public partial class Page1:ContentPage
{
public Page1()
{
InitializeComponent();
VM = (UserInventory)BindingContext;
}
public UserInventory VM { get; }
protected override void OnAppearing()
{
VM.Notify();
base.OnAppearing();
}
}
.
public class UserInventory: INotifyPropertyChanged
{
........
public void Notify()
{
OnPropertyChanged(nameof(Count));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.