dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.2k stars 1.75k forks source link

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

Open Quasimodo6 opened 1 year ago

Quasimodo6 commented 1 year ago

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:

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
ghost commented 1 year ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

Zhanglirong-Winnie commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Can repro on android and windows platform with sample project. TestXamlBindings-master.zip Screenshot 2023-06-21 141429