最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - I'm having a problem setting up compiled binding in my maui content page - Stack Overflow

programmeradmin0浏览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=";
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
             xmlns:x=";
             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?

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/dotnet/2021/maui"
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
             xmlns:x="http://schemas.microsoft/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?

Share Improve this question asked Feb 2 at 3:30 Bob GattoBob Gatto 1158 bronze badges 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. – Bob Gatto Commented Feb 2 at 3:50
  • What is the actual exception causing it to break? – Jason 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. – Bob Gatto Commented Feb 2 at 4:29
  • When it breaks look at the value of “e” for the exception details – Jason 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'." – Bob Gatto Commented Feb 2 at 5:32
Add a comment  | 

2 Answers 2

Reset to default 1

When I setup the compiled data binding as per This article, I fot 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.

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/dotnet/2021/maui"
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
             xmlns:x="http://schemas.microsoft/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();
    }
}
发布评论

评论列表(0)

  1. 暂无评论