AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.95k stars 2.25k forks source link

Unable to create a TemplatedControl in a library project #8138

Closed sam-wheat closed 2 years ago

sam-wheat commented 2 years ago

Expected behavior - Create an application project and a library project. Create a TemplatedControl in the lib project. Reference the lib from the app and use the app.

I am unable to guess how the above process works in Avalonia. Another user in one of the links below requested a tutorial also. I am unable to locate the tutorial.

https://github.com/AvaloniaUI/Avalonia/issues/2516 https://github.com/AvaloniaUI/Avalonia/issues/6475 https://github.com/AvaloniaUI/Avalonia/issues/6924 https://github.com/AvaloniaUI/Avalonia/issues/4456

Attached project is pretty much out-of-the-box and results in this error msg:

Avalonia.Markup.Xaml.XamlLoadException HResult=0x80131500 Message=No precompiled XAML found for make sure to specify x:Class and include your XAML file as AvaloniaResource Source=Avalonia.Markup.Xaml

Adding this:

<ItemGroup>
    <Folder Include="Models\" />
    <AvaloniaResource Include="Assets\**" />
    <Compile Update="**\*.axaml.cs">
        <DependentUpon>%(Filename)</DependentUpon>
    </Compile>
    <AvaloniaResource Include="**\*.axa

AvaloniaApp2.zip ml">

Designer
    </AvaloniaResource>
</ItemGroup>

Results in this:

Error AVLN:0002 Duplicate x:Class directive, AvaloniaLibrary2.App is already used in /App.axaml AvaloniaLibrary2

maxkatz6 commented 2 years ago

Hi! I see couple of problems with your project:

  1. You don't need <AvaloniaResource Include="**\*.axaml" /> because Avalonia already adds "axaml" files to the resources. You basically make it duplicate; exactly what error says to you "Duplicate x:Class directive".
  2. You TemplatedControl1 has InitializeComponent method. That's wrong, because for templated controls "xaml" markup is detached from the codebehind. You could use our templates for VS or Rider/dotnet, which are defined properly. Because of InitializeComponent app tried to get non-existed xaml file (not a style, but base markup for the control) and said "No precompiled XAML found".
  3. Your AvaloniaLibrary2 project has yet another App.axaml and MainWindow.axaml. It won't cause any problems in compilation and running the project, namespace is different, but you probably don't need it there. Note that StyleInclude should be in your running App.xaml.cs.
leaderanalytics commented 2 years ago

Thank you @maxkatz6. I implemented your recommended changes and the errors I was encountering are gone. The new error (which is what I started with) is:

HResult=0x80131500 Message=No precompiled XAML found for controls:TemplatedControl1.axaml (baseUri: avares://AvaloniaApp2/App.axaml), make sure to specify x:Class and include your XAML file as AvaloniaResource Source=Avalonia.Markup.Xaml

Modified app attached.

AvaloniaApp2.zip

maxkatz6 commented 2 years ago

Wrong StyleInclude syntax, the error tells you what can't be found and where it was defined (App.axaml).

<StyleInclude Source="avares://AvaloniaLibrary2/TemplatedControl1.axaml" />

image

sam-wheat commented 2 years ago

Of course! What was I thinking! Thanks for the assist.