English 中文(简体)
壳牌导航重复使用前页。 NET MAUI
原标题:Shell navigation reuses previous page in .NET MAUI

我拥有一个利用数据库和单壳航标的蚊帐。

其中一个页“斜体”显示数据库的数据,每当页显示时装上。

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子。

现在, 本条在改用<条码>新目录后,再改用上页的内容,然后改用<条码>。

例如,

  1. 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.

我如何确定这一点?

Code snippets,复制本。

问题回答

在将数据重新装上编码之前,你可以尝试澄清数据:

 Clients.Clear();

请参看以下法典:

public partial class ClientsViewModel : ObservableObject
{      

      public async Task LoadClientsAsync() 
        {
            await ExecuteAsync(async () =>
            {
                var clients = await _context.GetAllAsync<Client>();
                Console.WriteLine("length = " + clients.LongCount());

                if (clients is not null && clients.Any())//if we have at least one client
                {
                    Clients ??= new ObservableCollection<Client>(); //if null, initialize
                   
                   //clear data before adding data again. 
                    Clients.Clear();


                    foreach (var client in clients)//insert each client into a obervable collection Clients 
                    {
                        Clients.Add(client);
                    }
                }
            }, "Fetching clients...");
        }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签