uno.extensions icon indicating copy to clipboard operation
uno.extensions copied to clipboard

[C# Markup] ObservableProperty cannot be used in C# Markup due to compilation error

Open kucint opened this issue 5 months ago • 0 comments

Current behavior

When [ObservableProperty] is used in C# markup, the compiler fails with:

The type 'Uno.ObservableAsMarkupApp.MyItem' cannot be used as type parameter 'T' in the generic type or method 'AppBarButtonMarkup.Label<T>(T, string)'. There is no implicit reference conversion from 'Uno.ObservableAsMarkupApp.MyItem' to 'Microsoft.UI.Xaml.Controls.AppBarButton'.

Expected behavior

Compiler shall generate C# Markup correctly for [ObservableProperty]

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

Define an ObservableProperty in ObservableObject:

public sealed partial class MyItem : ObservableObject
{
    [ObservableProperty]
    private string _label = "";
}

and use it in form:

public MainPage()
{
    this
        .Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
        .Content(new StackPanel()
        .VerticalAlignment(VerticalAlignment.Center)
        .HorizontalAlignment(HorizontalAlignment.Center)
        .Children(
            new TextBlock()
            .Text("Hello Uno Platform!"),
            new ListView()
            .ItemsSource(new ObservableCollection<MyItem>
            {
                new MyItem { Label = "Item 1" },  // works fine
                new MyItem ()
                .Label("Item 2"),                 // produces compilation error
            })
        ));
}

repo: Uno.ObservableAsMarkupApp.zip

Environment

Nuget Package (s):

    <UnoFeatures>
      CSharpMarkup;
      Hosting;
      Logging;
      MVUX;
      Configuration;
      ThemeService;
      Mvvm;          // <--------------------
    </UnoFeatures>
  </PropertyGroup>

Package Version(s):

"Uno.Sdk": "5.3.108"

Affected platform(s):

  • [ ] iOS
  • [ ] macOS (AppKit)
  • [ ] Mac Catalyst
  • [ ] Android
  • [ ] WebAssembly
  • [x] Windows
  • [ ] Skia (WPF)
  • [x] Skia (GTK on Linux/macOS/Windows)
  • [ ] Skia (Linux Framebuffer)
  • [ ] Build tasks

Visual Studio:

  • [ ] 2019 (version: )
  • [x] 2022 (version: )
  • [ ] Visual Studio Code (version: )
  • [ ] Rider Windows (version: )
  • [ ] Rider macOS (version: )

Relevant plugins:

  • [ ] Resharper (version: )

Anything else we need to know?

kucint avatar Sep 16 '24 09:09 kucint