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

Cannot create an instance of "ReactiveUserControl`1" #1853

Closed
M0n7y5 opened this issue Dec 6, 2018 · 16 comments
Closed

Cannot create an instance of "ReactiveUserControl`1" #1853

M0n7y5 opened this issue Dec 6, 2018 · 16 comments

Comments

@M0n7y5
Copy link

M0n7y5 commented Dec 6, 2018

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
XAML Designer not working
obrazek

But compilation work.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem
Simply create Reactive User Control

What is the expected behavior?

Working XAML Designer

What is the motivation / use case for changing the behavior?

...

Which versions of ReactiveUI, and which platform / OS are affected by this issue? Did this work in previous versions of ReativeUI? Please also test with the latest stable and development snapshot

ReactiveUI 9.4.1
Visual Studio 2017 CE 15.9.3
Net Framework 4.7.2

Other information (e.g. stacktraces, related issues, suggestions how to fix)
Control XAML

<reactiveui:ReactiveUserControl x:Class="WavinModbusTester.Views.TestControl" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"          
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:reactiveui="http://reactiveui.net"
        xmlns:viewmodel="clr-namespace:WavinModbusTester.ViewModels" 
        x:TypeArguments="viewmodel:TestControlViewModel"
        mc:Ignorable="d" 
        d:DesignHeight="450" d:DesignWidth="800">
    <Grid>

    </Grid>
</reactiveui:ReactiveUserControl>

Control CS

using ReactiveUI;
using WavinModbusTester.ViewModels;

namespace WavinModbusTester.Views
{
    /// <summary>
    /// Interaction logic for TestControl.xaml
    /// </summary>
    public partial class TestControl : ReactiveUserControl<TestControlViewModel>
    {
        public TestControl()
        {
            InitializeComponent();
        }
    }
}

Control ViewModel

using ReactiveUI;

namespace WavinModbusTester.ViewModels
{
    public class TestControlViewModel : ReactiveObject
    {
    }
}

XAML Designer StackTrace

   at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.InstanceBuilderOperations.InstantiateType(Type type, Boolean supportInternal)
   at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(ILocalInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.UserControlInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.LocalInstanceManager.CreateInstance(IInstanceBuilder builder, ViewNode viewNode)
@jorisvergeer
Copy link
Contributor

I have the same issue.
I little workaround is to make the views in code. But it is subpar to using XAML.

@glennawatson
Copy link
Contributor

I don't think is one we will necessarily be able to fix easily. The xaml editor is notorious for breaking easily. Hopefully with wpf being made open source now there will be some love to the xaml editor.

@jorisvergeer
Copy link
Contributor

jorisvergeer commented Dec 10, 2018

For what I saw happening was that x:TypeArguments="viewmodel:TestControlViewModel" is being ignored by the code generator. Resulting in a class in the ******.g.cs with a definition like public partial class TestControl : ReactiveUserControl without the generic type arguments. Which results in not-compilable code.

I dont't know if this is a cause or a symptom. But i have no experience in the xaml code generation corner of c# development, nor had the time to look into it.

@M0n7y5
Copy link
Author

M0n7y5 commented Dec 10, 2018

So ... That means ReactiveUserControl is useless ? Or how can i use ReactiveUserControl with working XAML Designer? Is there any workaround?

@jasonwurzel
Copy link
Contributor

I'm using ReactiveUserControl extensively and my small workaround is the following pattern:
(in xaml.cs)

public class MyViewBase : ReactiveUserControl<MyViewModel>{ }

public class MyView : MyViewBase
{
// Actual Implementation
}

(in xaml)

<MyViewBase x:Class="MyView">
...
</MyViewBase>

@jorisvergeer
Copy link
Contributor

jorisvergeer commented Dec 10, 2018

Ok,
I did some googling, and found some useful stackoverflow (where else) threads

  1. https://stackoverflow.com/questions/7573712/how-to-specify-generic-type-argument-in-xaml
    So it appears that using generic types in XAML is only supported in XAML 2009.

So can we use XAML 2009?
2. https://stackoverflow.com/questions/31767198/xaml-2009-and-net-4-6-windows-10
Apparently not in UWP projects, limited in WPF projects, and fully in Xamarin.Forms projects.

I guess, we are all trying to use it in an UWP project where unfortunately generics in xaml are not supported.

Stack Overflow
I have a BaseView for my MVP - PRISM WPF application. Now for some reason we thought to make the _presenter as a Templated field in the BaseView.

earlier i had the view xaml representation as

<

Stack Overflow
WPF never ended up supporting XAML 2009, which is a shame because it added some useful extensions, such as generic type parameters and complex x:Key values.

UWP seems to have built upon WPF. Does ...

@jorisvergeer
Copy link
Contributor

So i guess that the workaround of @jasonwurzel is the best we can get.

@M0n7y5
Copy link
Author

M0n7y5 commented Dec 10, 2018

@jorisvergeer
Well ... i am using WPF right now ...

@jasonwurzel Noice... I'll try it 👍

@M0n7y5
Copy link
Author

M0n7y5 commented Dec 10, 2018

@jasonwurzel Well ... Can you please more describe your workaround?
TestControl is a partial class ...

@M0n7y5
Copy link
Author

M0n7y5 commented Dec 10, 2018

Ok ... It's working now.
Here is code:
XAML View

<views:MyViewBase
        xmlns:views="clr-namespace:WavinModbusTester.Views" 
        x:Class="MyView" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"          
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:reactiveui="http://reactiveui.net"
        xmlns:viewmodel="clr-namespace:WavinModbusTester.ViewModels" 
        mc:Ignorable="d" 
        d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Label Content="Hello World" FontSize="30"/> 
    </Grid>
</views:MyViewBase>

View CS

using ReactiveUI;
using WavinModbusTester.ViewModels;

namespace WavinModbusTester.Views
{
    public class MyViewBase : ReactiveUserControl<TestControlViewModel> { }

    public class MyView : MyViewBase
    {
        // Actual Implementation
    }
}

Viewmodel is unchanged.
Screen
obrazek

@jorisvergeer
Copy link
Contributor

@glennawatson

It might be useful to add an entry in the docs for this class.

@jorisvergeer
Copy link
Contributor

The code example in the comments in ReactiveUI/src/ReactiveUI/Platforms/windows-common/ReactiveUserControl.cs should maybe be updated so that it reflects this XAML behaviour.

@glennawatson
Copy link
Contributor

We welcome PR on the website project.

@jorisvergeer
Copy link
Contributor

I might write some later this week

@glennawatson
Copy link
Contributor

dotnet/wpf#58

Potentially related to this issue.

@glennawatson
Copy link
Contributor

Going to close this one, since there are workarounds presented, documentation has been changed, and more of a WPF visual studio bug.

@lock lock bot added the outdated label Jun 24, 2019
@lock lock bot locked and limited conversation to collaborators Jun 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants