Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MvxException:无法从定位器初始化x类型的ViewModel_C#_Sqlite_Xamarin_Xamarin.forms_Mvvmcross - Fatal编程技术网

C# MvxException:无法从定位器初始化x类型的ViewModel

C# MvxException:无法从定位器初始化x类型的ViewModel,c#,sqlite,xamarin,xamarin.forms,mvvmcross,C#,Sqlite,Xamarin,Xamarin.forms,Mvvmcross,拜访Xamarin和MvvmCross专家。我已尝试调试此错误数小时MvvmCross.Exceptions.MvxException:未能从定位器MvxDefaultViewModelLocator构建和初始化TestApp.Core.ViewModels.IngredientsViewModel类型的ViewModel。我发现了导致此错误的代码片段,它与使用EFCore sqlite从数据库中提取数据有关。 这是我第一次在手机应用中使用它,所以也许你会发现一些我看不到的东西。以下是我认为足够

拜访Xamarin和MvvmCross专家。我已尝试调试此错误数小时
MvvmCross.Exceptions.MvxException:未能从定位器MvxDefaultViewModelLocator构建和初始化TestApp.Core.ViewModels.IngredientsViewModel类型的ViewModel
。我发现了导致此错误的代码片段,它与使用EFCore sqlite从数据库中提取数据有关。 这是我第一次在手机应用中使用它,所以也许你会发现一些我看不到的东西。以下是我认为足够的所有信息,如果需要更多,请告诉我!我希望这个解决方案能帮助其他人。 请注意,当我注释掉
GetComponents
时,我没有发现上面的错误。

核心/共享项目中的我的应用程序文件
公共类AppCore:MvxApplication
{
公共覆盖无效初始化()
{
Mvx.IoCProvider.RegisterType();
Mvx.IoCProvider.RegisterType();
RegisterAppStart();
}
}
我的数据库上下文
公共类TestContext:DbContext
{
publictestcontext():base()
{
}
私有常量字符串_databaseName=“Test.db”;
公共数据库集{get;set;}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
字符串数据库路径;
交换机(设备运行时平台)
{
case Device.iOS:
SQLitePCL.batters_V2.Init();
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),“.”、“Library”、_databaseName);
打破
case设备。Android:
databasePath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),\u databaseName);
打破
违约:
抛出新的NotImplementedException(“不支持平台”);
}
optionsBuilder.UseSqlite($“Filename={databasePath}”);
}
}
存储库
公共接口假定,其中tenty:class
{
IEnumerable GetAll();
}
公共类存储库:IRepository,其中tenty:class
{
受保护的只读TestContext上下文;
公共存储库(TestContext上下文)
{
上下文=上下文;
}
公共IEnumerable GetAll()
{
返回Context.Set().ToList();//抛出此行错误后
}
}
公共接口IINGREDInterpository:IRepository{}
公共类IngredEnterpository:存储库,iIngredEnterpository
{
公共IngredEnterpository(TestContext TestContext):基(TestContext)
{ }
}
我的视图模型
公共类IngredientsViewModel:BaseViewModel
{
私有IIngredientRepository IngredientRepository{get;set;}
公共IngredientsViewModel(IINGREDINTERPOLITORY INGREDINTERPOLITORY)
{
IngredEnterpository=IngredEnterpository;
GetComponents();//当注释掉时,视图加载良好
}
私人有限公司()
{
var Components=IngredEnterpository.GetAll();
成分=新的MvxObservableCollection(成分);
}
私人MvxObservableCollection\u配料;
公共MvxObservableCollection成分
{
获取=>\u成分;
集合{SetProperty(ref _,value);}
}
}

在TestContext.cs中,尝试在构造函数中添加以下行:

Database.EnsureCreated(); //returns boolean which could be used for some check if needed
另外,我的建议是覆盖onmodel创建,如:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        //creating tables according to UML
        modelBuilder.Entity<Ingredient>();

        //add one for each table
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
//根据UML创建表
modelBuilder.Entity();
//为每个表添加一个
}

如果这没有帮助,请尝试检查InnerException或逐步调试。

应该有一个内部异常,该异常包含一条嵌入在该MvxException中的更详细的错误消息。你能把它贴出来吗?我猜您的配料表尚未创建,这就是为什么在查询该表中的所有项目时遇到错误的原因。@pnavk您是指抛出错误后输出窗口中的信息吗?我看到很多.db文件找不到。我认为这指向了下面的答案。谢谢!好极了我的视图现在加载没有问题!我在Android Device Monitor中意识到,我的模拟设备即使在放入
Database.encurecreated()
Database.Migrate()
之后,仍然有一个空的数据文件。你知道为什么吗?
public interface IRepository<TEntity> where TEntity : class
{
        IEnumerable<TEntity> GetAll();
}

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly TestContext Context;
    public Repository(TestContext context)
    {
        Context = context;
    }

    public IEnumerable<TEntity> GetAll()
    {
        return Context.Set<TEntity>().ToList(); //after this line error is thrown
    }
}

public interface IIngredientRepository : IRepository<Ingredient> {}
public class IngredientRepository : Repository<Ingredient>, IIngredientRepository
{
    public IngredientRepository(TestContext testContext) : base(testContext)
    { }
}
public class IngredientsViewModel : BaseViewModel
{
    private IIngredientRepository IngredientRepository { get; set; }

    public IngredientsViewModel(IIngredientRepository ingredientRepository)
    {
        IngredientRepository = ingredientRepository;
        GetIngredients(); //when commented out, the view loads fine
    }

    private void GetIngredients()
    {
        var ingredients = IngredientRepository.GetAll();
        Ingredients = new MvxObservableCollection<Ingredient>(ingredients);
    }

    private MvxObservableCollection<Ingredient> _ingredients;
    public MvxObservableCollection<Ingredient> Ingredients
    {
        get => _ingredients;
        set { SetProperty(ref _ingredients, value); }
    }
}
Database.EnsureCreated(); //returns boolean which could be used for some check if needed
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        base.OnModelCreating(modelBuilder);

        //creating tables according to UML
        modelBuilder.Entity<Ingredient>();

        //add one for each table
}