我拥有一个利用数据库和单壳航标的蚊帐。
其中一个页“斜体”显示数据库的数据,每当页显示时装上。
I had an issue where when I move around pages on TabBar, the data retrieved from database kept stacking up on previous results on the "Clients" page every time I visit there.
我之所以能够解决这一问题,是因为我补充说,我发现 。
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
ShellContent _previousShellContent;
protected override void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);
if (CurrentItem?.CurrentItem?.CurrentItem is not null &&
_previousShellContent is not null)
{
var property = typeof(ShellContent)
.GetProperty("ContentCache", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
property.SetValue(_previousShellContent, null);
}
_previousShellContent = CurrentItem?.CurrentItem?.CurrentItem;
}
}
然后,我又增加了一个分页“新罪犯”的分页,该分页是“罪犯”一页的,与“新罪犯”一页的对应。
ClientsPage.xaml.cs
:
private async void AddClient_Clicked(object sender, EventArgs e){
await Shell.Current.GoToAsync("NewClientPage");
}
App.xaml.cs
:
Routing.RegisterRoute("NewClientPage", typeof(NewClientPage));
接着又回到“Clients”网页上GoToAsync(
)(由于不像我所期望的那样工作,因此没有使用 built子。
现在, 本条在改用<条码>新目录代码>后,再改用上页的内容,然后改用<条码>。
例如,
- MainPage --> 2. ClientsPage --> 3. NewClientPage --> 4. ClientsPage --> 5. MainPage, --> 6. ClientsPage
<代码>MainPage和ClientsPage
均在Taba。
在4位客户从新客户返回后,他们看好。 然而,如果我去其他网页,回过来,新装数据则在上页内容下显示为重复。
这是装上该页的代码。 参看。
ClientsViewModel.cs
:
public partial class ClientsViewModel : ObservableObject
{
private readonly DatabaseContext _context;
public ClientsViewModel(DatabaseContext context)
{
_context = context;
}
public async Task LoadClientsAsync()
{
var clients = await _context.GetAllAsync<Client>();
if (clients is not null && clients.Any())//if we have at least one client
{
Clients ??= new ObservableCollection<Client>(); //if null, initialize
foreach (var client in clients)//insert each client into a obervable collection Clients
{
Clients.Add(client);
}
}
}
}
这一点在Appearing(Appearing)中得到了呼吁。
ClientsPage.xaml
:
public partial class ClientsPage : ContentPage
{
private readonly ClientsViewModel _viewModel;
public ClientsPage(ClientsViewModel viewModel)
{
InitializeComponent();
BindingContext = viewModel;
_viewModel = viewModel;
}
protected async override void OnAppearing()
{
base.OnAppearing();
await _viewModel.LoadClientAsync();
}
}
When I debug, OnNavigated
seems to be running, but there is still previous data left after using GoToAsync.
When I m on the NewClientPage
, CurrentItem?.CurrentItem?.CurrentItem.Title
is Client. I m guessing there is something wrong when _previousShellContent == CurrentItem?.CurrentItem?.CurrentItem
like Clients(on ClientsPage) == Clients(actually on NewClientPage)
, but I am not sure. I m testing on Android.
我如何确定这一点?