AvaloniaVS
AvaloniaVS copied to clipboard
"Type not found" in XAML Designer when extending Window
I created a custom Window
this way:
namespace MyProject.Desktop.Views
{
public abstract class BaseWindow : Window
{
public BaseWindow()
{
this.InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
protected void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
[...]
}
}
Now I use it in my views:
(MainWindow.xaml)
<base:BaseWindow
xmlns="https://github.com/avaloniaui"
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:base="clr-namespace:MyProject.Desktop.Views"
mc:Ignorable="d"
x:Class="MyProject.Desktop.Views.Main.MainWindow"
Icon="/Assets/icon_square.png"
Title="MyProject">
<StackPanel>[...]</StackPanel>
</base:BaseWindow>
In MainWindow.xaml.cs, the view is declared as
namespace SparkEvo.Desktop.Views.Main
{
public partial class MainWindow : BaseWindow
[...]
At this point, the XAML designer in Visual Studio 2017 gets full of errors...
It does not recognize the tags anymore ("The type XXXX was not found. Verify that you are not missing an assembly reference."). But...
- If I make MainWindow a simple Window, so that it does not extend BaseWindow, the error disappears.
- I can compile even with this error, but the XAML designer is unusable, example there is no autocomplete and working gets really difficult.
Avalonia version: 0.8.0 Target Frameworks: netcoreapp2.2;net461
Transferred the issue to the AvaloniaVS repository
Unfortunately this is a limitation of Visual Studio.
Visual Studio works on the xmlns
of the root element, not the root xmlns
declaration. Because BaseWindow
is not in the https://github.com/avaloniaui
namespace, it gives up and shows the WPF designer :(
There doesn't appear to be anything we can do about this, as the selection of the WPF designer is done in core VS before anything in our extension is even loaded. My only suggestion would be to create an issue using the VS feedback tool. If you do create an issue, please link it here so others can upvote it.
unfortunately same problem with VS 2019, and reported a problem to VS developer community here: https://developercommunity.visualstudio.com/content/problem/616853/problem-with-xaml-designer-using-custom-window-in.html
Apart from the problems mentioned above, designer doesn't show any preview for extended windows. This is an important bug which should be fixed. Please let us know ETA.
@waliarubal what do you mean by "extended windows"?
@grokys By extended window I mean a class extending Window, i.e. a child class of Window class. Though I figured out through Gitter channel that changing extension of design file from XAML to PAML and its corresponding code behind fixes the designer and it starts showing the preview.
There doesn't appear to be anything we can do about this, as the selection of the WPF designer is done in core VS before anything in our extension is even loaded.
@grokys so is the fundamental issue here that VS is loading the WPF designer when you'd just rather it didn't and you could use your own design surface?
I managed to get the designer working by renaming XAML to PAML. Maybe update the addin so that whenever any new control or window is added it gets PAML extension for both code behind and designer file... like Demo.paml and Demo.paml.cs.
@stevenbrix yeah, the problem is this:
Visual Studio works on the xmlns of the root element, not the root xmlns declaration. Because BaseWindow is not in the https://github.com/avaloniaui namespace, it gives up and shows the WPF designer :(
So it's showing the WPF designer instead of ours because base:BaseWindow
is in the clr-namespace:MyProject.Desktop.Views
and not in the avalonia namespace.
IMO it should be looking at the xmlns="https://github.com/avaloniaui"
namespace to decide which designer to show, not the namespace of the root element.
@waliarubal we originally did that, but that caused other problems IIRC (I can't remember the details right now). Also they're XAML files, we shouldn't have to come up with our own extension ;) This is 100% a bug in VS that should be fixed IMO.
#111 contains a workaround for this issue. It doesn't fix the underlying issue but at least allows you to open a XAML file with the Avalonia editor from the "Open With..." menu. Thanks @donandren for coming up with this.
Reopening this, as it's still technically an issue.
Closing in favor of https://github.com/AvaloniaUI/AvaloniaVS/issues/159