如果我继承了从MvxViewModel派生的基础ViewModel,则不会调用mvvmcross Init方法

时间:2013-11-10 16:51:36

标签: mvvmcross

首先我使用mvvmcross version 3.0.13。当从继承自MvxViewModel的基类继承类时,不会从IoC调用Init方法。目前丑陋的解决方法是在构造函数中调用Init。这可能是一个错误还是有其他模式可供使用?

在这两个类(该基础的基础和子级)中,它不会被调用。例如:

public class BaseViewModel : MvxViewModel
{
    protected CDataImportService DataImportService { get; private set; }
    protected CSettingService SettingService { get; private set; }
    protected CDataService DataService { get; private set; }
    protected CDocumentService DocumentService { get; private set; }

    public BaseViewModel(IDataService objDataService, IDataImportService objDataImportService, IDocumentService objDocumentService, ISettingService objSettingService)
    {
        DataImportService = (CDataImportService)objDataImportService;
        SettingService = (CSettingService)objSettingService;
        DataService = (CDataService)objDataService;
        DocumentService = (CDocumentService) objDocumentService;
    }

    public void Init()
    {
        Mvx.Trace("Init called in {0}", GetType().Name);
    }
}

public class DocumentsViewModel : BaseViewModel
{
    public MenuViewModel(IDataService objDataService, IDataImportService objDataImportService, IDocumentService objDocumentService, ISettingService objSettingService)
        : base(objDataService, objDataImportService, objDocumentService, objSettingService)
    {
    }
}

1 个答案:

答案 0 :(得分:4)

它自己的IOC不会调用Constructor-Init-Reload-Start序列。

IOC是一般的C#服务,只调用构造函数部分。

如果您想要调用整个序列,则可以通过IMvxViewModelLoader对象访问它 - 例如Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(MvxViewModelRequest<MyViewModel>.GetDefaultRequest(), null);

默认情况下,这将使用默认ViewModel定位器来创建视图模型实例 - https://github.com/MvvmCross/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs


如果有帮助,请获取更多信息: