Is there anything semantically different in, or inherently wrong with, instantiating a viewmodel explicitly from a page's constructor, opposed to declaring it from within XAML using a property element?
NotePage.xaml
<ContentPage.BindingContext>
<viewModels:NoteViewModel/>
</ContentPage.BindingContext>
NotePage.cs
public NotePage()
{
BindingContext = new ViewModels.NoteViewModel();
InitializeComponent();
}
From XAML
NotePage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:Notes.ViewModels"
x:Class="Notes.Views.NotePage"
Title="Note">
<ContentPage.BindingContext>
<viewModels:NoteViewModel />
</ContentPage.BindingContext>
...
</ContentPage>
NotePage.cs
public partial class NotePage : ContentPage
{
public NotePage()
{
// BindingContext = new ViewModels.NoteViewModel();
InitializeComponent();
}
}
From constructor
NotePage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Notes.Views.NotePage"
Title="Note">
<!--xmlns:viewModels="clr-namespace:Notes.ViewModels"-->
<!--<ContentPage.BindingContext>
<viewModels:NoteViewModel />
</ContentPage.BindingContext>-->
...
</ContentPage>
NotePage.cs
public partial class NotePage : ContentPage
{
public NotePage()
{
BindingContext = new ViewModels.NoteViewModel();
InitializeComponent();
}
}