Skip to content

MAUI - XAML - Object reference not set to an instance of an object - reproducable, not same as #8249 #13856

@Quasimodo6

Description

@Quasimodo6

Description

If you have a Dictionary with an self created enum as key, and try to bind this to an entry element in xaml, you can not start this app, nor does the intellisense nor the compiler give you any meaningful errormessage, you only get a "Object reference not set to an instance of an object" in the error list.

If you change from Dictionary<yourEnum, yourObject> to Dictionary<string, yourObject> it runs like a charme.

Steps to Reproduce

  1. Create a new .NET Maui 7.0 Project in Visual Studio 2022
  2. Add a public enum to the mainpage.xaml.cs:
    public enum UserSetting
    {
        BrowserInvisible,
        GlobalWaitForElementsInBrowserInSek,
        TBD,
    
    }
  3. Add a public Dictionary<yourEnum, typeYouLike> to the mainpage.xaml.cs
    private Dictionary<UserSetting, object> _userSettings;
    
    public Dictionary<UserSetting, object> UserSettings
    {
        get { return _userSettings; }
        set
        {
            _userSettings = value;
        }
    }
  4. Initialize your Dictionary in the mainpage constructor:
    public MainPage()
    {
        UserSettings = new Dictionary<UserSetting, object>
        {
            { UserSetting.TBD, "pups" }
        };
    
        InitializeComponent();
        
        BindingContext = this;
        
    }
  5. Add the DataType to the contentpage of the mainpage.xaml
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestXamlBindings.MainPage"
             xmlns:settings="clr-namespace:TestXamlBindings"
             x:DataType="settings:MainPage">
  6. Add an "entry" element somewhere on the mainpage.xaml with Text bound to the Dictionary:
    <Entry AutomationId="GlobalWaitForElementsInBrowserInSek"
                       Text="{Binding UserSettings[TBD]}"
                       ToolTipProperties.Text="0-20"
                       Placeholder="10"
                       Keyboard="Numeric" />
  7. BUG:
    you can not start this app, nor does the intellisense nor the compiler give you any meaningful errormessage, you only get a "Object reference not set to an instance of an object" in the error list.

**8. Why is this a bug:

  • If you change from Dictionary<yourEnum, yourObject> to Dictionary<string, yourObject> it runs like a charme**
  • If you get rid of the "DataType" in the .xaml, the intellisense won't find the Binding, nor will the program find it at runtime

Link to public reproduction project repository

https://github.com/Quasimodo6/TestXamlBindings

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows

Affected platform versions

net7.0-windows10.0.19041.0

Did you find any workaround?

nope, i have wasted days and days on this $!"§$!** xaml bindings and red hours over hours in stackoverflow and other sources, without any success. finally i decided to start a clean new project and check, if it is a problem in front of the monitor or behind ;)
turns out: it simply doesn't work as expected.

Relevant log output

Schweregrad	Code	Beschreibung	Projekt	Datei	Zeile	Unterdrückungszustand
Fehler		Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.	TestXamlBindings	C:\Users\*****\*****\Visual Studio Projects\TestXamlBindings\MainPage.xaml	1

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlXAML, CSS, Triggers, Behaviorsplatform/androidplatform/windowss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions