unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.74k stars 706 forks source link

Codegeneration is failed when using ResourceDictionary where elements requires code behind #12495

Open FrozDark opened 1 year ago

FrozDark commented 1 year ago

Current behavior

Xaml parser fails with error System.Exception: Unable to find x:Class on the top level element

<ResourceDictionary
    x:Class="UnoAppIssues.Resources.ResourceDictionaryWithCodeBehind"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <MenuFlyout x:Key="someFlyout">
        <MenuFlyoutItem Click="MenuFlyoutItem_Click" Text="Some text" />
    </MenuFlyout>

</ResourceDictionary>

Code behind

public sealed partial class ResourceDictionaryWithCodeBehind : ResourceDictionary
    {
        public ResourceDictionaryWithCodeBehind()
        {
            this.InitializeComponent();
        }

        private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
        {

        }
    }

Expected behavior

Xaml parse and code generation successfully

How to reproduce it (as minimally and precisely as possible)

Repro: XamlParseUnoAppIssues.zip

Workaround

None

Works on UWP/WinUI

Yes

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

Uno 4.10.0-dev.111

Affected platforms

WebAssembly, Android, iOS, macOS (AppKit), Mac Catalyst, Skia (WPF), Skia (GTK on Linux/macOS/Windows), Skia (Linux Framebuffer), Skia (Tizen)

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

No response

jeromelaban commented 1 year ago

Thanks for the report. This is looks like a duplicate of https://github.com/unoplatform/uno/issues/10109#issuecomment-1360318219, and it's unlikely that we'll be adding support for this particular feature. It's probably best to use command binding instead.

FrozDark commented 1 year ago

Thanks for the report. This is looks like a duplicate of #10109 (comment), and it's unlikely that we'll be adding support for this particular feature. It's probably best to use command binding instead.

Why not? WinUI 3 is working there

MartinZikmund commented 11 months ago

@jeromelaban Indeed as @FrozDark mentions, this actually does work in UWP/WinUI. I saw the linked issue and the reason why the events were not fired while you tested it is that resource dictionaries with code-behind need to be declared differently in XAML:

This will load the resource dictionary without its code-behind (e.g. no events will work}:

<ResourceDictionary Source="ms-appx:///MyDictionary.xaml" />

This loads the resource dictionary with its code-behind (events should work):

<myRes:MyDictionary />