拜访Xamarin和MvvmCross专家。我已经尝试调试MvvmCross.Exceptions.MvxException: Failed to construct and initialize ViewModel for type TestApp.Core.ViewModels.IngredientsViewModel from locator MvxDefaultViewModelLocator
几个小时的错误了。我发现了实际上导致此错误的那段代码,这与使用EFCore sqlite从数据库中提取数据有关。
这是我第一次将其用于移动应用程序,因此也许您可能会发现我看不到的东西。以下是我认为足够的所有信息,如果需要更多信息,请告诉我!我希望该解决方案可以帮助其他人。
请注意,当我将GetIngredients
注释掉时,我没有得到上面的错误。
public class AppCore : MvxApplication
{
public override void Initialize()
{
Mvx.IoCProvider.RegisterType<TestContext>();
Mvx.IoCProvider.RegisterType<IIngredientRepository, IngredientRepository>();
RegisterAppStart<IngredientsViewModel>();
}
}
public class TestContext : DbContext
{
public TestContext() : base()
{
}
private const string _databaseName = "Test.db";
public DbSet<Ingredient> Ingredients { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string databasePath;
switch (Device.RuntimePlatform)
{
case Device.iOS:
SQLitePCL.Batteries_V2.Init();
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", _databaseName); ;
break;
case Device.Android:
databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), _databaseName);
break;
default:
throw new NotImplementedException("Platform not supported");
}
optionsBuilder.UseSqlite($"Filename={databasePath}");
}
}
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); }
}
}
答案 0 :(得分:1)
在您的TestContext.cs中,尝试在构造函数中添加以下行:
Database.EnsureCreated(); //returns boolean which could be used for some check if needed
而且,我的建议是像这样重写OnModelCreating:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//creating tables according to UML
modelBuilder.Entity<Ingredient>();
//add one for each table
}
如果这样做没有帮助,请尝试检查InnerException或逐步调试。