我有 IListViewModel 来处理 Id 和 Name 的列表。 我还有另外 4 个 ViewModel 命名 AViewModel 、 BViewModel 、 CViewModel 、 DViewModel 和我为 Xamarin 项目使用了 Prism 框架
每次我导航到另一个页面时,我都会在列表中添加新项目。
我期望在页面中导航后我将在列表中有 4 个项目,但不幸的是,
下面的片段是我在其中一个 ViewModel 上的示例代码
public class CPageViewModel : BaseViewModel
{
private readonly INavigationService _navigationService;
private readonly IListTestViewModel _listTestViewModel;
public ICommand NavigateToPreviousCommand => new DelegateCommand(async () => await NavigateToPrevious());
public ICommand NavigateToRootCommand => new DelegateCommand(async () => await NavigateToRoot());
public CPageViewModel(INavigationService navigationService, IListTestViewModel listTestViewModel) : base(navigationService)
{
this._navigationService = navigationService;
this._listTestViewModel = listTestViewModel;
listTestViewModel.TestListCollection.Add(new MyTestModel { Id = 4, Name = "Josh" });
}
private Task NavigateToPrevious()
{
return _navigationService.NavigateAsync("BPage");
}
private Task NavigateToRoot()
{
return _navigationService.GoBackToRootAsync();
}
}
我在列表中得到 1 条记录。下面是我遇到的问题的示例项目文件
最佳答案
大胆猜测:IListTestViewModel
已注册为 transient 服务。
注册为单例,你应该没问题:
containerRegistry.RegisterSingleton<IListTestViewModel, ListTestViewModelImplementation>();
关于c# - 导航时刷新 ViewModel 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57405132/