Cannot Reference ViewModel with EFCore in Contructor from UWP XAML
See original GitHub issueCreate the EFGetStarted.UWP app with all prerequisites in accordance with UWP - New Database
Add a class MainPageViewModel.cs
namespace EFGetStarted.UWP
{
public class MainPageViewModel
{
public MainPageViewModel()
{
using (var db = new BloggingContext())
{
}
}
}
}
Add the following using directive to MainPage.xaml
xmlns:ViewModels="using:EFGetStarted.UWP"
and the following
<Page.DataContext>
<ViewModels:MainPageViewModel x:Name="MainPageViewModel" />
</Page.DataContext>
In VS2015 Update 3there is a build error
Could not load file or assembly ‘Microsoft.EntityFrameworkCore, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60’ or one of its dependencies. The system cannot find the file specified. EFGetStarted.UWP
project.json
{
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
},
``` "frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}
App.config
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.IO.FileSystem.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Overlapped" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="4.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="4.1.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Comment out the using statement and the code builds. Or comment out
<ViewModels:MainPageViewModel x:Name="MainPageViewModel" />
and the code builds.
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (8 by maintainers)
Top Results From Across the Web
Unable to reference a design-time ViewModel in Xaml
In short, I've got a design-time model that inherits from my real view model and sets some properties in the constructor.
Read more >uwp with c# and mvvm inversion of control (dependency ...
7.Resolve the MainViewModel , and inject the dependencies by using ActivatorUtilities function to inject parameters into the constructor.
Read more >Building Modern Desktop Apps with .NET and C - WinUI 3.0
WinUI 3.0 is an evolution of the UWP XAML framework. Then there are also cross-platform frameworks like .NET MAUI and Uno Platform.
Read more >Error in designer when used in constructor · Issue #89
This works at runtime, this is an issue of during design time in the UWP-XAML designer. This occurs when the constructor of the...
Read more >Learn How to Use Dependency Injection in .NET MAUI
This blog explains the dependency injection technique and how to easily use it in your .NET MAUI applications with code examples.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to add up to that. I get an error only on x86 configuration, which is required for Design Data to work.
However I’ve just found a fancy solution. You just need to put your context initialization in a different function. For instance:
And this is strange, but it works.
@Andrew-By 's workaround is good for me too.