WTS Cannot declare ViewModelLocator in XAMLWhat is a ViewModelLocator and what are its pros/cons compared to DataTemplates?Cannot create an instance of “ViewModelLocator”Cannot find resource named 'ViewModelLocator' exceptioncannot create instance of viewmodellocator in c# mvvm lightCannot make my ViewModelLocator workApp.xaml can not found namespace IocContainter MVVMXamarin MVVM Light ViewModelLocator setting BindingContext in xamlViewModelLocator in prism mvvmXAML Command Inside a StylePrism Auto ViewModelLocator with Subdirectories in UWP
Do we have to introduce the character's name before using their names in a dialogue tag?
How can I show that the speed of light in vacuum is the same in all reference frames?
Quickest way to move a line in a text file before another line in a text file?
When will the last unambiguous evidence of mankind disappear?
Found old paper shares of Motorola Inc that has since been broken up
Killing a star safely
Has Iron Man made any suit for underwater combat?
Book in which the "mountain" in the distance was a hole in the flat world
Is it OK to accept a job opportunity while planning on not taking it?
Does observational data need further proof?
Could Europeans in Europe demand protection under UN Declaration on the Rights of Indigenous Peoples?
How to check if all my faces on mesh are all quads using a script
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
Substitute dried pig's blood for fresh
What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?
creating recursive, self-similar patterns with Adobe Illustrator
Why is DC so, so, so Democratic?
I have a domain, static IP and many devices I'd like to access outside my house. How to route them?
How old is the Italian word "malandrino"?
Is there a smart way to pick up Page Design other than Template to Design mapping?
Count the identical pairs in two lists
Why do we need an estimator to be consistent?
Did Don Young threaten John Boehner with a 10 inch blade to the throat?
Are there foods that astronauts are explicitly never allowed to eat?
WTS Cannot declare ViewModelLocator in XAML
What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?Cannot create an instance of “ViewModelLocator”Cannot find resource named 'ViewModelLocator' exceptioncannot create instance of viewmodellocator in c# mvvm lightCannot make my ViewModelLocator workApp.xaml can not found namespace IocContainter MVVMXamarin MVVM Light ViewModelLocator setting BindingContext in xamlViewModelLocator in prism mvvmXAML Command Inside a StylePrism Auto ViewModelLocator with Subdirectories in UWP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I've created my MVVM Light app with Windows Template Studio and now i'm trying to use databindings in my XAML. But this does not seem possible to me with the generated code because it does not declare the Viewmodel in the XAML. I changed the code to do this but then i stumbeled across this error:
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
I've tried making the constructor public but that does not solve the issue. Any idea's on how i can get databindings to work?
This is all the code:
App.XAML
<Application
x:Class="PatientApp.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:PatientApp.UWP.ViewModels">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
<ResourceDictionary Source="/Styles/_Colors.xaml"/>
<ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
<ResourceDictionary Source="/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="/Styles/TextBlock.xaml"/>
<ResourceDictionary Source="/Styles/Page.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
ViewModelLocator:
[Windows.UI.Xaml.Data.Bindable]
public class ViewModelLocator
private static ViewModelLocator _current;
public static ViewModelLocator Current => _current ?? (_current = new ViewModelLocator());
private ViewModelLocator()
SimpleIoc.Default.Register(() => new NavigationServiceEx());
SimpleIoc.Default.Register<ShellViewModel>();
Register<PatientsViewModel, AllPatientsPage>();
Register<DiseasesViewModel, AllDiseasesPage>();
public DiseasesViewModel DiseasesViewModel => SimpleIoc.Default.GetInstance<DiseasesViewModel>();
public PatientsViewModel PatientsViewModel => SimpleIoc.Default.GetInstance<PatientsViewModel>();
public ShellViewModel ShellViewModel => SimpleIoc.Default.GetInstance<ShellViewModel>();
public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();
public void Register<VM, V>()
where VM : class
SimpleIoc.Default.Register<VM>();
NavigationService.Configure(typeof(VM).FullName, typeof(V));
PatientPage:
<Page
x:Class="PatientApp.UWP.Views.AllPatientsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="StaticResource PageStyle"
mc:Ignorable="d"
xmlns:datamodel="using:PatientApp.Model"
DataContext="Binding PatientViewModelInstance, Source=StaticResource Locator">
<Grid
Background="ThemeResource SystemControlPageBackgroundChromeLowBrush">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right"
Grid.Row="0"
OverflowButtonVisibility="Collapsed">
<AppBarButton Icon="Sort" Label="Sort" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Add" Label="Add" />
<AppBarButton Icon="Zoom" Label="Search" />
</CommandBar>
<GridView x:Name="Patients"
Grid.Row="1"
ItemsSource="Binding Patients">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Margin="14,0,0,0" Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate x:DataType="datamodel:Patient">
<TextBlock Text="x:Bind Name"
FontWeight="Medium"
TextWrapping="NoWrap"
HorizontalAlignment="Left" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
mvvm uwp
add a comment |
I've created my MVVM Light app with Windows Template Studio and now i'm trying to use databindings in my XAML. But this does not seem possible to me with the generated code because it does not declare the Viewmodel in the XAML. I changed the code to do this but then i stumbeled across this error:
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
I've tried making the constructor public but that does not solve the issue. Any idea's on how i can get databindings to work?
This is all the code:
App.XAML
<Application
x:Class="PatientApp.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:PatientApp.UWP.ViewModels">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
<ResourceDictionary Source="/Styles/_Colors.xaml"/>
<ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
<ResourceDictionary Source="/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="/Styles/TextBlock.xaml"/>
<ResourceDictionary Source="/Styles/Page.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
ViewModelLocator:
[Windows.UI.Xaml.Data.Bindable]
public class ViewModelLocator
private static ViewModelLocator _current;
public static ViewModelLocator Current => _current ?? (_current = new ViewModelLocator());
private ViewModelLocator()
SimpleIoc.Default.Register(() => new NavigationServiceEx());
SimpleIoc.Default.Register<ShellViewModel>();
Register<PatientsViewModel, AllPatientsPage>();
Register<DiseasesViewModel, AllDiseasesPage>();
public DiseasesViewModel DiseasesViewModel => SimpleIoc.Default.GetInstance<DiseasesViewModel>();
public PatientsViewModel PatientsViewModel => SimpleIoc.Default.GetInstance<PatientsViewModel>();
public ShellViewModel ShellViewModel => SimpleIoc.Default.GetInstance<ShellViewModel>();
public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();
public void Register<VM, V>()
where VM : class
SimpleIoc.Default.Register<VM>();
NavigationService.Configure(typeof(VM).FullName, typeof(V));
PatientPage:
<Page
x:Class="PatientApp.UWP.Views.AllPatientsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="StaticResource PageStyle"
mc:Ignorable="d"
xmlns:datamodel="using:PatientApp.Model"
DataContext="Binding PatientViewModelInstance, Source=StaticResource Locator">
<Grid
Background="ThemeResource SystemControlPageBackgroundChromeLowBrush">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right"
Grid.Row="0"
OverflowButtonVisibility="Collapsed">
<AppBarButton Icon="Sort" Label="Sort" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Add" Label="Add" />
<AppBarButton Icon="Zoom" Label="Search" />
</CommandBar>
<GridView x:Name="Patients"
Grid.Row="1"
ItemsSource="Binding Patients">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Margin="14,0,0,0" Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate x:DataType="datamodel:Patient">
<TextBlock Text="x:Bind Name"
FontWeight="Medium"
TextWrapping="NoWrap"
HorizontalAlignment="Left" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
mvvm uwp
add a comment |
I've created my MVVM Light app with Windows Template Studio and now i'm trying to use databindings in my XAML. But this does not seem possible to me with the generated code because it does not declare the Viewmodel in the XAML. I changed the code to do this but then i stumbeled across this error:
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
I've tried making the constructor public but that does not solve the issue. Any idea's on how i can get databindings to work?
This is all the code:
App.XAML
<Application
x:Class="PatientApp.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:PatientApp.UWP.ViewModels">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
<ResourceDictionary Source="/Styles/_Colors.xaml"/>
<ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
<ResourceDictionary Source="/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="/Styles/TextBlock.xaml"/>
<ResourceDictionary Source="/Styles/Page.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
ViewModelLocator:
[Windows.UI.Xaml.Data.Bindable]
public class ViewModelLocator
private static ViewModelLocator _current;
public static ViewModelLocator Current => _current ?? (_current = new ViewModelLocator());
private ViewModelLocator()
SimpleIoc.Default.Register(() => new NavigationServiceEx());
SimpleIoc.Default.Register<ShellViewModel>();
Register<PatientsViewModel, AllPatientsPage>();
Register<DiseasesViewModel, AllDiseasesPage>();
public DiseasesViewModel DiseasesViewModel => SimpleIoc.Default.GetInstance<DiseasesViewModel>();
public PatientsViewModel PatientsViewModel => SimpleIoc.Default.GetInstance<PatientsViewModel>();
public ShellViewModel ShellViewModel => SimpleIoc.Default.GetInstance<ShellViewModel>();
public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();
public void Register<VM, V>()
where VM : class
SimpleIoc.Default.Register<VM>();
NavigationService.Configure(typeof(VM).FullName, typeof(V));
PatientPage:
<Page
x:Class="PatientApp.UWP.Views.AllPatientsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="StaticResource PageStyle"
mc:Ignorable="d"
xmlns:datamodel="using:PatientApp.Model"
DataContext="Binding PatientViewModelInstance, Source=StaticResource Locator">
<Grid
Background="ThemeResource SystemControlPageBackgroundChromeLowBrush">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right"
Grid.Row="0"
OverflowButtonVisibility="Collapsed">
<AppBarButton Icon="Sort" Label="Sort" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Add" Label="Add" />
<AppBarButton Icon="Zoom" Label="Search" />
</CommandBar>
<GridView x:Name="Patients"
Grid.Row="1"
ItemsSource="Binding Patients">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Margin="14,0,0,0" Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate x:DataType="datamodel:Patient">
<TextBlock Text="x:Bind Name"
FontWeight="Medium"
TextWrapping="NoWrap"
HorizontalAlignment="Left" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
mvvm uwp
I've created my MVVM Light app with Windows Template Studio and now i'm trying to use databindings in my XAML. But this does not seem possible to me with the generated code because it does not declare the Viewmodel in the XAML. I changed the code to do this but then i stumbeled across this error:
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
I've tried making the constructor public but that does not solve the issue. Any idea's on how i can get databindings to work?
This is all the code:
App.XAML
<Application
x:Class="PatientApp.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:PatientApp.UWP.ViewModels">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
<ResourceDictionary Source="/Styles/_Colors.xaml"/>
<ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
<ResourceDictionary Source="/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="/Styles/TextBlock.xaml"/>
<ResourceDictionary Source="/Styles/Page.xaml"/>
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
ViewModelLocator:
[Windows.UI.Xaml.Data.Bindable]
public class ViewModelLocator
private static ViewModelLocator _current;
public static ViewModelLocator Current => _current ?? (_current = new ViewModelLocator());
private ViewModelLocator()
SimpleIoc.Default.Register(() => new NavigationServiceEx());
SimpleIoc.Default.Register<ShellViewModel>();
Register<PatientsViewModel, AllPatientsPage>();
Register<DiseasesViewModel, AllDiseasesPage>();
public DiseasesViewModel DiseasesViewModel => SimpleIoc.Default.GetInstance<DiseasesViewModel>();
public PatientsViewModel PatientsViewModel => SimpleIoc.Default.GetInstance<PatientsViewModel>();
public ShellViewModel ShellViewModel => SimpleIoc.Default.GetInstance<ShellViewModel>();
public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();
public void Register<VM, V>()
where VM : class
SimpleIoc.Default.Register<VM>();
NavigationService.Configure(typeof(VM).FullName, typeof(V));
PatientPage:
<Page
x:Class="PatientApp.UWP.Views.AllPatientsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="StaticResource PageStyle"
mc:Ignorable="d"
xmlns:datamodel="using:PatientApp.Model"
DataContext="Binding PatientViewModelInstance, Source=StaticResource Locator">
<Grid
Background="ThemeResource SystemControlPageBackgroundChromeLowBrush">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right"
Grid.Row="0"
OverflowButtonVisibility="Collapsed">
<AppBarButton Icon="Sort" Label="Sort" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Add" Label="Add" />
<AppBarButton Icon="Zoom" Label="Search" />
</CommandBar>
<GridView x:Name="Patients"
Grid.Row="1"
ItemsSource="Binding Patients">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Margin="14,0,0,0" Orientation="Vertical" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate x:DataType="datamodel:Patient">
<TextBlock Text="x:Bind Name"
FontWeight="Medium"
TextWrapping="NoWrap"
HorizontalAlignment="Left" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
mvvm uwp
mvvm uwp
asked Mar 26 at 13:01
Jari FlederickJari Flederick
17613 bronze badges
17613 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
The reason is ViewModelLocator construct method is private in your scenario. So you could not instantiate it in the xaml. you could change it to public. Even that you can't use it. Because the ViewModelLocator instance in xaml is created by a constructor rather than a singleton method. In other words <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" /> not equal to ViewModelLocator.Current. Unfortunately, ActivationService used ViewModelLocator.Current to get NavigationService. So the app throw exception when start.
public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
It is hard to approach with MVVMLight. In general, the page DataContext was defended in code behind. And with x:bind to binding data source.
private SettingsViewModel ViewModel
get return ViewModelLocator.Current.SettingsViewModel;
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357849%2fwts-cannot-declare-viewmodellocator-in-xaml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
The reason is ViewModelLocator construct method is private in your scenario. So you could not instantiate it in the xaml. you could change it to public. Even that you can't use it. Because the ViewModelLocator instance in xaml is created by a constructor rather than a singleton method. In other words <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" /> not equal to ViewModelLocator.Current. Unfortunately, ActivationService used ViewModelLocator.Current to get NavigationService. So the app throw exception when start.
public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
It is hard to approach with MVVMLight. In general, the page DataContext was defended in code behind. And with x:bind to binding data source.
private SettingsViewModel ViewModel
get return ViewModelLocator.Current.SettingsViewModel;
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
add a comment |
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
The reason is ViewModelLocator construct method is private in your scenario. So you could not instantiate it in the xaml. you could change it to public. Even that you can't use it. Because the ViewModelLocator instance in xaml is created by a constructor rather than a singleton method. In other words <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" /> not equal to ViewModelLocator.Current. Unfortunately, ActivationService used ViewModelLocator.Current to get NavigationService. So the app throw exception when start.
public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
It is hard to approach with MVVMLight. In general, the page DataContext was defended in code behind. And with x:bind to binding data source.
private SettingsViewModel ViewModel
get return ViewModelLocator.Current.SettingsViewModel;
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
add a comment |
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
The reason is ViewModelLocator construct method is private in your scenario. So you could not instantiate it in the xaml. you could change it to public. Even that you can't use it. Because the ViewModelLocator instance in xaml is created by a constructor rather than a singleton method. In other words <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" /> not equal to ViewModelLocator.Current. Unfortunately, ActivationService used ViewModelLocator.Current to get NavigationService. So the app throw exception when start.
public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
It is hard to approach with MVVMLight. In general, the page DataContext was defended in code behind. And with x:bind to binding data source.
private SettingsViewModel ViewModel
get return ViewModelLocator.Current.SettingsViewModel;
XAML ViewModelLocator type cannot be constructed. In order to be constructed in XAML, a type cannot be abstract, interface, nested, generic or a struct, and must have a public default constructor
The reason is ViewModelLocator construct method is private in your scenario. So you could not instantiate it in the xaml. you could change it to public. Even that you can't use it. Because the ViewModelLocator instance in xaml is created by a constructor rather than a singleton method. In other words <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" /> not equal to ViewModelLocator.Current. Unfortunately, ActivationService used ViewModelLocator.Current to get NavigationService. So the app throw exception when start.
public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
It is hard to approach with MVVMLight. In general, the page DataContext was defended in code behind. And with x:bind to binding data source.
private SettingsViewModel ViewModel
get return ViewModelLocator.Current.SettingsViewModel;
answered Mar 27 at 7:16
Nico Zhu - MSFTNico Zhu - MSFT
12.7k1 gold badge6 silver badges24 bronze badges
12.7k1 gold badge6 silver badges24 bronze badges
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
add a comment |
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
Indeed! Using x:Bind ViewModel.Patients Seems to work like i want it to.
– Jari Flederick
Mar 27 at 8:26
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55357849%2fwts-cannot-declare-viewmodellocator-in-xaml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown