Home > Back-end >  MAUI: Unable to add an event handler via XAML if the event is internal
MAUI: Unable to add an event handler via XAML if the event is internal

Time:01-12

I found an unexpected XAML parser behavior. Here is a page from a MAUI project:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiApp1"
             x:Class="MauiApp1.MyPage">
  <local:MyView MyEvent="MyView_MyEvent" />
</ContentPage>

local:MyView is:

namespace MauiApp1;

class MyView: View
{
  public event EventHandler MyEvent;
}

Pay attention to the event's public modifier. This is a fully working project until I change the modifier to internal. The page and MyView are entities of the same assembly, so the modifier change should have no affect on the app. However, it causes the LoadFromXaml method called from MyPage.InitializeComponent to throw a XamlParseException. With the internal modifier, I can still successfully add the event handler via C#, but not via XAML.

I decided to find out if this behavior is inherent in XAML in principle. In fact, this proved not to be the case. For example, WPF doesn't mind adding handlers to internal events via XAML.

Therefore, I would like to know: is this a bug from the MAUI developers or am I missing something?

CodePudding user response:

When changing the modifier to Private, it'll also throw a XamlParseException.And this is a known issue Private EventHandlers throw an exception on XAML-defined custom control in Github, you can follow up it.

Furthermore, I notice you have reported a new issue on Github: https://github.com/dotnet/maui/issues/12566 and you can follow up it.

  • Related