0

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?

5
  • Stephen Quan - I just tried what you suggested, and I'm still getting the same result. It's still an error of some kind. Commented Feb 2 at 3:50
  • What is the actual exception causing it to break? Commented Feb 2 at 4:00
  • @Jason - I don't know. All I know is it jumps right to the App.g.i.cs file and at the line 'if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();' the section 'global::System.Diagnostics.Debugger.Break();' is highlighted yellow. Commented Feb 2 at 4:29
  • When it breaks look at the value of “e” for the exception details Commented Feb 2 at 4:54
  • Thank you. The exception details are: "Unable to resolve service for type 'LockAndKeyMaui.ViewModels.MainViewModel' while attempting to activate 'LockAndKeyMaui.Views.MainPage'." Commented Feb 2 at 5:32

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

When working with compiled bindings, the general rule I follow is try to set BindingContext and x:DataType at the same time. Because you set x:DataType in XAML and BindingContext in the code behind, there's a chance that when the InitializeComponent() is being run, it may be momentarily unhappy.

Generally, I found it better for me to bind to the ContentPage from XAML, e.g.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:LockAndKeyMaui"
             x:Name="this"
             BindinContext={Reference this}"
             x:DataType="local:MainPage"
             x:Class="LockAndKeyMaui.Views.MainPage"
             x:DataType="viewmodel:MainViewModel">
    <!-- ... -->
    <Label Text="{Binding VM.Ownr}" />

And, with this small change, I capture the MainViewModel as a property on the page, e.g.

public partial class MainPage : ContentPage
{
    public MainViewModel VM { get; }
    public MainPage(MainViewModel vm)
    {
        this.VM = vm;
        InitializeComponent();
    }
}

1 Comment

Stephen Quan - Thanks for the answer, but I'm still getting that same error. I have the MainPage files located in a folder called Views, and in the AppShell.xaml file I have the line xmlns:local="clr-namespace:LockAndKeyMaui.Views". Maybe that has something to do with it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.