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

Source Generator and Reactive Attribute, as alternative to ReactiveWindow and ReactiveUserControl #3449

Open
martinrhan opened this issue Dec 16, 2022 · 0 comments

Comments

@martinrhan
Copy link

martinrhan commented Dec 16, 2022

Is your feature request related to a problem? Please describe.
I am now working with ReactiveUI.Wpf and I found it very tedious to use ReactiveWindow and ReactiveUserControl.

Following text is copied from the documentation:

This class is a UserControl that is also reactive. That is, it implements IViewFor. You can extend this class to get an implementation of IViewFor rather than writing one yourself.

Note that the XAML for your control must specify the same base class, including the generic argument you provide for your view model. To do this, use the TypeArguments attribute as follows:

<rxui:ReactiveUserControl
        x:Class="Foo.Bar.Views.YourView"
        x:TypeArguments="vms:YourViewModel"
        xmlns:rxui="http://reactiveui.net"
        xmlns:vms="clr-namespace:Foo.Bar.ViewModels"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d">
    <!-- view XAML here -->
</rxui:ReactiveUserControl>

Note that UWP and WinUI projects do not support the TypeArguments attribute. The XAML designer window in WPF projects also does not support generic types. To use ReactiveUserControl in XAML documents you need to create a base class where you derive from ReactiveUserControl with the type argument filled in.

 internal class YourViewBase : ReactiveUserControl<YourViewModel> { /* No code needed here */ }

 public partial class YourView : YourViewBase
 {
     /* Your code */
 }

Then you can use this base class as root in your XAML document.

<views:YourViewBase
        x:Class="Foo.Bar.Views.YourView"
        xmlns:rxui="http://reactiveui.net"
        xmlns:vms="clr-namespace:Foo.Bar.ViewModels"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d">
    <!-- view XAML here -->
</views:YourViewBase>

As you can see for WPF I need to add many additional code manaully for each View I creates. It become even more lousy when it comes to UWP and WinUI.

Describe the solution you'd like
Just after a View is created, no need addtional modify to the xaml, simply add a Reactive Attribute.

[Reactive]
 public partial class MyView : UserControl //This should also work for something else such as ContentControl
 {
     /* Your code */
 }

Then the Source Generator should generate the implementation.

Describe alternatives you've considered
An alternative is item templates. It can also automatically write all those things. However, this method lacks flexibility.

Describe suggestions on how to achieve the feature
I don't have much idea about how to do that. As I saw https://github.com/reactiveui/ReactiveUI/blob/main/src/ReactiveUI.Uwp/Common/ReactiveUserControl.cs
much more complecated than what I can handle.

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant