maui icon indicating copy to clipboard operation
maui copied to clipboard

Dependency Injection: Attempted to access a missing method

Open AstrNexus opened this issue 3 years ago • 0 comments

Description

Page need need no parameter Constructor to work normally.

Steps to Reproduce

MauiProgram.cs

builder.Services.AddSingleton<MyService>();

AppShell.xaml

<Shell
    x:Class="MauiLab.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiLab"
    Shell.FlyoutBehavior="Disabled">
    <TabBar>
        <Tab Title="Hello">
            <ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
        </Tab>
        <Tab Title="World">
            <ShellContent ContentTemplate="{DataTemplate local:NewPage}" />
        </Tab>
    </TabBar>
</Shell>

MyService.cs

 public class MyService  {  }

NewPage.cs

public partial class NewPage : ContentPage
{
    public NewPage (MyService service) { InitializeComponent(); }
}

Launch App and switch to World Tab

UnhandledException in App.g.i.cs MauiLab.WinUI.App.InitializeComponent() sender object {MauiLab.WinUI.App}

e {"Attempted to access a missing method."} | System.Exception {System.MissingMethodException}
"Cannot dynamically create an instance of type 'MauiLab.NewPage'. Reason: No parameterless constructor defined."

My debugger can't catch exception if app's first page's constructor need service injection.

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

Windows 11 22622.436 with Visual Studio 17.3/17.4 Preview 1

Did you find any workaround?

Register page manually just like preview 13 announcing blog post add builder.Services.AddTransient<NewPage>();

Relevant log output

No response

AstrNexus avatar Aug 09 '22 21:08 AstrNexus