【问题标题】:ViewModel List was refresh when navigatingViewModel 列表在导航时被刷新
【发布时间】:2019-12-15 17:23:01
【问题描述】:

我有处理 Id 和名称列表的 IListViewModel。 我还有 4 个其他 ViewModel 名为 AViewModel 、 BViewModel 、 CViewModel 、 DViewModel 和我在 Xamarin 项目中使用了 Prism 框架

每次我导航到另一个页面时,我都会在列表中添加新项目。

我所期望的是,在我导航到页面后,列表中将有 4 个项目,但不幸的是,

以下 sn-ps 是我在其中一个 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 条记录。下面是我遇到的问题的示例项目文件

Click this link to Download my Sample Project

【问题讨论】:

    标签: c# mvvm xamarin.forms cross-platform prism


    【解决方案1】:

    猜测:IListTestViewModel 已注册为临时服务。

    注册为单例,应该没问题:

    containerRegistry.RegisterSingleton<IListTestViewModel, ListTestViewModelImplementation>();
    

    【讨论】:

    • 感谢您的快速响应,它节省了我很多时间!干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2023-03-11
    • 2018-02-25
    • 2015-08-21
    • 2020-04-09
    • 2013-05-27
    相关资源
    最近更新 更多