Skip to content

System.NullReferenceException when Using MenuFlyoutItem #699

@Jack251970

Description

@Jack251970

Describe the bug

Source codes:

// Build context menu using FluentAvalonia MenuFlyout to reliably show at pointer
var menu = new MenuFlyout();

void AddMenuItem(string header, string glyph, bool isEnabled, Action action)
{
    var mi = new MenuItem
    {
        Header = header,
        /*IconSource = new FontIconSource
        {
            Glyph = glyph
        },*/
        IsEnabled = isEnabled
    };
    mi.Click += (s, _) => action();
    menu.Items.Add(mi);
}

void AddSeparator()
{
    menu.Items.Add(new Separator());
}

AddMenuItem(Localize.MainWindow_Cut(), "\uE8C6", hasSelection, Cut);

AddMenuItem(Localize.MainWindow_Copy(), "\uE8C8", hasSelection, Copy);

AddMenuItem(Localize.MainWindow_Paste(), "\uE77F", canPaste, Paste);

AddMenuItem(Localize.MainWindow_Delete(), "\uE74D", hasSelection, DeleteSelection);

// Show at mouse position
menu.ShowAt(this, true);

When I hover my mouse on this MenuFlyout, it throws this exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   Source: FluentAvalonia
   TargetAssembly: FluentAvalonia, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null
   TargetModule: FluentAvalonia.dll
   TargetSite: Void OnPointerEntered(Avalonia.Input.PointerEventArgs)
   at FluentAvalonia.UI.Controls.MenuFlyoutItemBase.OnPointerEntered(PointerEventArgs e)
   at Avalonia.Input.InputElement.OnPointerEnteredCore(PointerEventArgs e)
   at Avalonia.Input.InputElement.<>c.<.cctor>b__33_5(InputElement x, PointerEventArgs e)
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value)
   at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)
   at Avalonia.Interactivity.EventRoute.RaiseEvent(Interactive source, RoutedEventArgs e)
   at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)
   at Avalonia.Input.PointerOverPreProcessor.SetPointerOverToElement(IPointer pointer, IInputRoot root, IInputElement element, UInt64 timestamp, Point position, PointerPointProperties properties, KeyModifiers inputModifiers)
   at Avalonia.Input.PointerOverPreProcessor.OnNext(RawInputEventArgs value)
   at Avalonia.Reactive.LightweightObservableBase`1.PublishNext(T value)
   at Avalonia.Controls.TopLevel.<>c.<HandleInput>b__150_0(Object state)
   at Avalonia.Threading.Dispatcher.Send(SendOrPostCallback action, Object arg, Nullable`1 priority)

Note that here MenuFltyout is Avalonia.Controls.MenuFltyout

Screenshots
.

Desktop/Platform (please complete the following information):

  • OS: Windows 11
  • FluentAvalonia Version: 2.4.0
  • Avalonia Version: 11.3.8

Additional context
.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions