enisn / TreeView.Maui

A simple treeview control for MAUI
MIT License
38 stars 8 forks source link

NullReferenceException when trying to make custom item template #1

Closed nikolaykusch closed 2 years ago

nikolaykusch commented 2 years ago
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="here_class_name"
             xmlns:t="clr-namespace:TreeView.Maui.Controls;assembly=TreeView.Maui">
    <Grid
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand">
        <t:TreeView ItemsSource="{Binding Path=vmTreeView.Items, Mode=OneWay}">
            <t:TreeView.ItemTemplate>
                <DataTemplate>
                    <HorizontalStackLayout>
                        <Image Source="folder.png"/>
                        <Label Text="{Binding Name}"/>
                        <Image Source="check.png" IsVisible="{Binding IsSelected}"/>
                    </HorizontalStackLayout>
                </DataTemplate>
            </t:TreeView.ItemTemplate>
        </t:TreeView>
    </Grid>
</ContentView>

Here details of error

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=Microsoft.Maui.Controls.Xaml StackTrace: at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, Object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo) at Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.Visit(ElementNode node, INode parentNode) at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Microsoft.Maui.Controls.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Microsoft.Maui.Controls.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode) at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, String xaml, Assembly rootAssembly, Boolean useDesignProperties) at Microsoft.Maui.Controls.Xaml.XamlLoader.Load(Object view, Type callingType) at Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml[TXaml](TXaml view, Type callingType) at TDriveMAUI.View.Controls.Main.LeftPane.TreeView.InitializeComponent() in C:...\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\View_Controls_Main_LeftPane_TreeView.xaml.sg.cs:line 22

enisn commented 2 years ago

I got the problem. ItemsSource is null for the first render. I prevented rendering if ItemSource is null with this pr: https://github.com/enisn/TreeView.Maui/pull/2

enisn commented 2 years ago

I've updated the demo and it's working well now treeview-custom-template

nikolaykusch commented 2 years ago

Yes, it works. Thank you.