1

首先我使用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)
    {
    }
}
4

1 回答 1

4

IOC 本身不会调用 Constructor-Init-Reload-Start 序列。

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

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

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


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

于 2013-11-11T19:24:38.930 回答