I found an article on Compiled Binding and tried to follow it exactly. My XAML file looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="LockAndKeyMaui.Views.MainPage"
x:DataType="viewmodel:MainViewModel">
<Grid RowDefinitions="85,Auto,Auto,Auto"
ColumnDefinitions="240,5,*"
BackgroundColor="DarkGreen">
<Image Source="password_info.png"
Aspect="Fill" Grid.ColumnSpan="3"/>
<Label Text="Group Select"
TextColor="Yellow" FontSize="Medium"
FontAttributes="Bold" HorizontalTextAlignment="Center"
Margin="0,30,0,0" Grid.Row="1"/>
<BoxView Color="Aquamarine"
WidthRequest="2"
HorizontalOptions="Center"
Grid.Row="1" Grid.Column="1"
Grid.RowSpan="3"/>
<Label Text="Password List"
TextColor="Yellow" FontSize="Medium"
FontAttributes="Bold" Margin="20,30,0,0"
Grid.Row="1" Grid.Column="2"/>
<HorizontalStackLayout
HorizontalOptions="End"
Margin="0,30,20,0" Grid.Row="1"
Grid.Column="2">
<Label Text="Owner:"
TextColor="Yellow" FontSize="Medium"
FontAttributes="Bold" TextDecorations="Underline"/>
<Label Text="{Binding Ownr}"
TextColor="White" FontSize="Small"
VerticalTextAlignment="Center" Margin="15,0,0,0"/>
</HorizontalStackLayout>
</Grid>
</ContentPage>
Its code-behind looks like this:
using LockAndKeyMaui.ViewModels;
namespace LockAndKeyMaui.Views;
public partial class MainPage : ContentPage
{
public MainPage(MainViewModel vm)
{
InitializeComponent();
BindingContext = vm;
}
}
And when I try to run the program, I get this:
#if DEBUG && !DISABLE_XAML_GENERATED_BINDING_DEBUG_OUTPUT DebugSettings.BindingFailed += (sender, args) => { global::System.Diagnostics.Debug.WriteLine(args.Message); }; #endif #if DEBUG && !DISABLE_XAML_GENERATED_RESOURCE_REFERENCE_DEBUG_OUTPUT DebugSettings.XamlResourceReferenceFailed += (sender, args) => { global::System.Diagnostics.Debug.WriteLine(args.Message); }; #endif #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION UnhandledException += (sender, e) => { if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); }; #endif } } }
Which is from the App.g.i.cs file.
So what am I missing? How do I fix this?
When I setup the compiled data binding as per This article, I forgot to register the view model file in the MauiProgram.cs file. So adding the line
builder.Services.AddSingleton<MainViewModel>();
to the MauiProgram.cs file solved the problem.