-
Notifications
You must be signed in to change notification settings - Fork 1.9k
MAUI - XAML - Object reference not set to an instance of an object - reproducable, not same as #8249 #13856
Description
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
- Create a new .NET Maui 7.0 Project in Visual Studio 2022
- Add a public enum to the mainpage.xaml.cs:
public enum UserSetting { BrowserInvisible, GlobalWaitForElementsInBrowserInSek, TBD, }
- 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; } }
- Initialize your Dictionary in the mainpage constructor:
public MainPage() { UserSettings = new Dictionary<UserSetting, object> { { UserSetting.TBD, "pups" } }; InitializeComponent(); BindingContext = this; }
- 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">
- 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" />
- 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