Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: namespace error #54

Closed
ethpch opened this issue Sep 21, 2024 · 6 comments · Fixed by #61 or #65
Closed

[Bug]: namespace error #54

ethpch opened this issue Sep 21, 2024 · 6 comments · Fixed by #61 or #65
Labels
bug Something isn't working

Comments

@ethpch
Copy link

ethpch commented Sep 21, 2024

Describe the bug 🐞

Build Avalonia project always report CS0246 error. It seems like namespace is not resolved for custom classes. In generated files there are always no using statements for Avalonia namespace and Project.ViewModels namespace.

Step to reproduce

  1. Create a new Avalonia C# Project, enable ReactiveUI and add this package to the project by nuget
  2. Open MainView.axaml.cs
  3. Add [IViewFor(nameof(MainViewModel))] for MainView
  4. Build, and see error

Reproduction repository

No response

Expected behavior

No build errors

Screenshots 🖼️

image

IDE

Visual Studio 2022

Operating system

Windows 11 Pro

Version

23H2

Device

Desktop

ReactiveUI Version

18.3.1, by avalonia

Additional information ℹ️

Changing attribute to full declaration like [IViewFor($"ViewModels.{nameof(MainViewModel)}")] can avoid such issue, but StyledProperty and AvaloniaPropertyChangedEventArgs belongs to Avalonia namespace. Please fix this namespace error for Avalonia, and add more information at readme.

image

Maybe Generic Attribute could help, or scan the full assembly to find the correct class and its namespace. I'm not familar with SourceGenerators.

@ethpch ethpch added the bug Something isn't working label Sep 21, 2024
@ChrisPulman
Copy link
Member

We will release this update soon, if you wanted to try it you could download the related nuget package from the CI build https://github.com/reactiveui/ReactiveUI.SourceGenerators/actions/runs/11043024970/artifacts/1979778639

Thank you

@ethpch
Copy link
Author

ethpch commented Sep 26, 2024

Thanks for your quick response!

I have tried the latest version and the generic attribute works as expected. But the generated code still lacks namespace Avalonia so project cannot get built, which StyledProperty and AvaloniaPropertyChangedEventArgs belongs to.

I have uploaded minimal project. This project is created step by step, a minimal Avalonia C# Project with [IViewFor<ViewModels.MainViewModel>] attribute to MainView class.

And the Generic Attribute tracking issue shows this feature is still in progress, and it won't be backported to .NET Framework. Please evaluate its compatibility.

@ethpch
Copy link
Author

ethpch commented Sep 26, 2024

I have checked default templates for Maui, WinForm and Wpf, please check this project.

As a result, Maui and WinForm works well, but there are some issues with Avalonia and Wpf.

Avalonia is namespace error, and it cannot get built.

Wpf can get built, but running breaks at MainWindow.InitializeComponent(). The error message is ArgumentException: “IViewFor``1”类型必须派生自 DependencyObject。

Check the generated code, the ViewModelProperty is defined as public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(nameof(ViewModel), typeof(WpfApp1.MainViewModel), typeof(IViewFor<WpfApp1.MainViewModel>), new PropertyMetadata(null));.

Parameter 3 of DependencyProperty.Register is ownerType. It should be typeof(MainWindow) here, or typeof(SomeControl), not typeof(IViewFor<WpfApp1.MainViewModel>).

image

There are some troubles for default templates for WinUI3(#4693, #4734) and UWP(strange nullable settings), I cannot test these frameworks.

Should I open another issue for this?

I hope the information can help this project better.

@ChrisPulman
Copy link
Member

Thank you very much for this feedback, I will reopen this and re visit the solution.

@ChrisPulman ChrisPulman reopened this Sep 26, 2024
ChrisPulman added a commit that referenced this issue Sep 27, 2024
ChrisPulman added a commit that referenced this issue Sep 27, 2024
@ChrisPulman
Copy link
Member

Hi, I believe that #65 has now resolved this issue, thank you very much for the pointers to the issues.
We will try to release this in the next few days.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
2 participants