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

No parameterless constructor in custom theme #18273

Open
Flo0806 opened this issue Feb 20, 2025 · 8 comments
Open

No parameterless constructor in custom theme #18273

Flo0806 opened this issue Feb 20, 2025 · 8 comments

Comments

@Flo0806
Copy link

Flo0806 commented Feb 20, 2025

Describe the bug

Hello,

I created a library with some custom controls and styles. I followed the guides and searched the code. I created an entry point like this:

C# (ControlThemes.axaml.cs)

using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using System;

namespace SD.Avalonia.UI.Controls
{
    public class ControlThemes : Styles
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ControlThemes"/> class.
        /// </summary>
        /// <param name="sp">The parent's service provider.</param>
        public ControlThemes(IServiceProvider? sp = null)
        {
            AvaloniaXamlLoader.Load(sp, this);
        }
    }
}

ControlThemes.axaml

<Styles x:Class="SD.Avalonia.UI.Controls.ControlThemes"
		xmlns="/~https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
	<FluentTheme />

	<Styles.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceInclude Source="/Controls/Themes/Styles.axaml" />
				<ResourceInclude Source="/Controls/Themes/Generic.axaml" />
			</ResourceDictionary.MergedDictionaries>
		</ResourceDictionary>
	</Styles.Resources>

</Styles>

In the end all works as well if I start the application. But the designer show an error:
Cannot dynamically create an instance of type 'SD.Avalonia.UI.Controls.ControlThemes'. Reason: No parameterless constructor defined.

I wanna understand what to do... I checked other libraries and I haven't found any differences.

Greetings, Flo

To Reproduce

You can use this package, create a second Project (Avalon MVVM as example), add the library project to it and after a build open the AvaloniaThemeTemplate.axaml to see the error.

Link to the Repo: Here

Expected behavior

No errors :-)

Avalonia version

11.1.0 - 11.2.4

OS

Windows

Additional context

No response

@Flo0806 Flo0806 added the bug label Feb 20, 2025
@maxkatz6
Copy link
Member

maxkatz6 commented Feb 21, 2025

It looks fine. How is this file referenced? Repro project doesn't show that.

"Cannot dynamically create an instance" doesn't seem to be an Avalonia exception.

@Flo0806
Copy link
Author

Flo0806 commented Feb 21, 2025

It looks fine. How is this file referenced? Repro project doesn't show that.

It is referenced as a Avalonia XAML. In the App self its referenced like this:

<Application xmlns="/~https://github.com/avaloniaui"
             xmlns:control="using:SD.Avalonia.UI.Controls"
             ...
             />
    <Application.Styles>
        <FluentTheme/>
        <control:ControlThemes/> <!-- HERE -->
    </Application.Styles>

The Thing is that the designer throws this error. The app starts and runs without any errors after the build.

Greetings, Flo

@maxkatz6
Copy link
Member

maxkatz6 commented Feb 21, 2025

@Flo0806 do you have a complete repro? As you mentioned above, normally this code just works. I don't see anything unusual.

@Flo0806
Copy link
Author

Flo0806 commented Feb 21, 2025

@maxkatz6 Yes, here: REPO.

Load the project, build it und then open the AvaloniaThemeTemplate.axaml in VS Designer. If you run the app you will see a pink button - The proof that the lib will work correctly and all Resources are found successfully.

By the way: Is it normal that MergeResourceInclude throws a InnerException like "Unable to resolve XAML resource xxxx"? In build/run mode all is fine, no errors. Only in the Designer, again.

@Flo0806
Copy link
Author

Flo0806 commented Feb 27, 2025

@maxkatz6 Is the Repo ok? Can you reproduce the error?

@maxkatz6
Copy link
Member

@Flo0806 designer works here.
Image

@maxkatz6
Copy link
Member

Or are you trying to preview AvaloniaThemeTemplate? What's the goal? Avalonia technically could create an empty IServiceProvider to preview files like this, but that's a rare use case, with easy workaround (second ctor without IServiceProvider), and more importantly - there is nothing to preview.

@maxkatz6
Copy link
Member

By the way: Is it normal that MergeResourceInclude throws a InnerException like "Unable to resolve XAML resource xxxx"? In build/run mode all is fine, no errors. Only in the Designer, again.

Not really normal. You can use full avares path instead, should make designer happier.

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

2 participants