enisn / UraniumUI

Uranium is a Free & Open-Source UI Kit for MAUI.
Apache License 2.0
1.17k stars 142 forks source link

UraniumContentPage throws Null reference on InitializeComponent method #583

Open Az6bcn opened 8 months ago

Az6bcn commented 8 months ago

I have my Page inheriting from UraniumContentPage and I have done same in the xlml to use the bottom sheet in the attachment but it keeps throwing null reference exception (Object reference not set to an instance of an object) when InitializeComponent method is called.

public partial class MyPage : UraniumContentPage
{
    private readonly MyViewModel _viewModel;
    private readonly MediaElement _myMediaElement;

    public MyPage(MyViewModel viewModel)
    {
        InitializeComponent();

        _viewModel = viewModel;

        _myMediaElement = new MediaElement();

        BindingContext = _viewModel;
    }
<uranium:UraniumContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:DataType="viewModel:MyViewModel"
             xmlns:viewModel="clr-namespace:xxxxxViewModels"
             xmlns:material="http://schemas.enisn-projects.io/dotnet/maui/uraniumui/material"
             xmlns:uranium="clr-namespace:UraniumUI.Pages;assembly=UraniumUI"
             x:Class="xxxx.Views.MyPage">

    <uranium:UraniumContentPage.Attachments>
         <material:BottomSheetView IsPresented="True">
            <material:BottomSheetView.Header>
                <Grid RowDefinitions="*, auto" Grid.Row="2" Padding="10" BackgroundColor="{StaticResource Gray600}">
                <Grid ColumnDefinitions="auto,*">
                <Grid ColumnDefinitions="50, *">
                    <Image HeightRequest="60" Aspect="Fill" WidthRequest="50" Source="audio_icon.png"></Image>
                    <StackLayout Grid.Column="1" VerticalOptions="Center" Margin="10, 0">
                        <Label Text="xx"></Label>
                        <Label Text="xx" FontSize="11" TextColor="{StaticResource Gray500}"></Label>
                    </StackLayout>
                </Grid>

                </Grid>

                </Grid>
            </material:BottomSheetView.Header>
            <TableView Intent="Menu" Margin="0,0,0,40">
                <TableRoot>
                    <TableSection Title="Song Queue">
                        <TextCell Text="1. Introduction to .NET MAUI"
                    Detail="Learn about .NET MAUI and what it provides." />
                    </TableSection>
                </TableRoot>
            </TableView>
        </material:BottomSheetView>
    </uranium:UraniumContentPage.Attachments>
</uranium:UraniumContentPage>
enisn commented 8 months ago

Hi, can you share the exception with stacktrace?

Az6bcn commented 8 months ago

This is the stacktrace,

"   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo)\n   at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)\n   at Microsoft.Maui.Controls.Xaml.ValueNode.Accept(IXamlNodeVisitor visitor, INode parentNode)\n   at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)\n   at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)\n   at Microsoft.Maui.Controls.Xaml.XamlLoader.Visit(RootNode rootnode, HydrationContext visitorContext, Boolean useDesignProperties)\n   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties)\n   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Boolean useDesignProperties)\n   at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType)\n   at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[MyPage](MyPage view, Type callingType)\n   at MyApp.Views.MyPage.InitializeComponent() in /Users/Documents/Projects/Maui/MyApp/MyApp/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator/Views_MyPage.xaml.sg.cs:line 22\n   at MyApp.Views.MyPage..ctor(MyViewModel viewModel) in /Users/Documents/Projects/Maui/MyApp/MyApp/Views/MyPage.xaml.cs:line 15\n   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags , Binder , Object[] , CultureInfo )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite , RuntimeResolverContext )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite , RuntimeResolverContext )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite , RuntimeResolverContext )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite , RuntimeResolverContext )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite , ServiceProviderEngineScope )\n   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type )\n   at System.Collections.Concurrent.ConcurrentDictionary`2[[System.Type, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Func`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope, Microsoft.Extensions.DependencyInjection, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetOrAdd(Type , Func`2 )\n   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type , ServiceProviderEngineScope )\n   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type )\n   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)\n   at Microsoft.Maui.Controls.ShellContent.<>c__DisplayClass19_0.<Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent>b__0()\n   at Microsoft.Maui.Controls.ElementTemplate.CreateContent()\n   at Microsoft.Maui.Controls.Internals.DataTemplateExtensions.CreateContent(DataTemplate self, Object item, BindableObject container)\n   at Microsoft.Maui.Controls.ShellContent.Microsoft.Maui.Controls.IShellContentController.GetOrCreateContent()\n   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.LoadRenderers()\n   at Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRootRenderer.ViewDidLoad()\n   at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr )\n   at UIKit.UIApplication.Main(String[] , Type , Type )\n   at MyApp.Program.Main(String[] args) in /Users/Documents/Projects/Maui/MyApp/MyApp/Platforms/iOS/Program.cs:line 13"

Both my Page and viewModel are registered

   builder.Services.AddSingleton<MyViewModel>();
   builder.Services.AddSingleton<MyPage>();

And this is the source generated code from the line its failing on when InitializeComponent() is called:

 private void InitializeComponent()
{
    global::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof(MyPage));
}

And am on .NET 7

enisn commented 8 months ago

I couldn't reproduce the same problem currently.

Are you using UranuimUI v2.5.6 on .NET 7?

DevFromDownUnder commented 8 months ago

This could be a duplicate of #540

I have not tested though