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.78k stars 2.23k forks source link

MergeResourceInclude doesn't include namespaces definition #10640

Open SKProCH opened 1 year ago

SKProCH commented 1 year ago

Describe the bug When i use MergeResourceInclude i get System.ArgumentException: Unable to resolve namespace for type ... when trying to start the app.

To Reproduce Steps to reproduce the behavior:

  1. Have a Styles with AttachedProperties used like this: xmlns:dialogHostAvalonia="clr-namespace:DialogHostAvalonia" CornerRadius="{Binding Path=(dialogHostAvalonia:DialogHostStyle.CornerRadius), RelativeSource=...}"
    namespace DialogHostAvalonia;
    public class DialogHostStyle {
        public static readonly AttachedProperty<CornerRadius> CornerRadiusProperty =
            AvaloniaProperty.RegisterAttached<DialogHostStyle, DialogHost, CornerRadius>("CornerRadius");
        public static CornerRadius GetCornerRadius(DialogHost element)
        {
            return element.GetValue(CornerRadiusProperty);
        }
        public static void SetCornerRadius(DialogHost element, CornerRadius value)
        {
            element.SetValue(CornerRadiusProperty, value);
        }
    }

    Everything works fine.

  2. Migrate styles to ControlTheme. Add resource dictionary with ControlTheme like this:
    <Styles xmlns="https://github.com/avaloniaui"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Styles.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <MergeResourceInclude Source="avares://DialogHost.Avalonia/DialogHost.axaml" />
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Styles.Resources>
    </Styles>
  3. Add this Styles to demo app styles.
  4. Start app, use control with this ControlTheme, get exception.
  5. If we add xmlns:dialogHostAvalonia="clr-namespace:DialogHostAvalonia" to namespaces definition in styles (step 2) everything starts working fine.

How to fix Exception shouldn't be raised. Namespaces should be copied automatically by the MergeResourceInclude.

Desktop (please complete the following information):

Additional context

Exception info ``` System.ArgumentException: Unable to resolve namespace for type dialogHostAvalonia:DialogHostStyle at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.XamlTypeResolver.Resolve(String qualifiedTypeName) in /_/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs:line 153 at Avalonia.Markup.Xaml.Extensions.ResolveType(IServiceProvider ctx, String namespacePrefix, String type) in /_/src/Markup/Avalonia.Markup.Xaml/Extensions.cs:line 37 at Avalonia.Markup.Parsers.ExpressionParser.ParseAttachedProperty(AttachedPropertyNameNode node) in /_/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs:line 132 at Avalonia.Markup.Parsers.ExpressionParser.Parse(CharacterReader& r) in /_/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs:line 56 at Avalonia.Markup.Parsers.ExpressionObserverBuilder.Parse(String expression, Boolean enableValidation, Func`3 typeResolver, INameScope nameScope) in /_/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionObserverBuilder.cs:line 22 at Avalonia.Data.Binding.CreateExpressionObserver(AvaloniaObject target, AvaloniaProperty targetProperty, Object anchor, Boolean enableDataValidation) in /_/src/Markup/Avalonia.Markup/Data/Binding.cs:line 69 at Avalonia.Data.BindingBase.Initiate(AvaloniaObject target, AvaloniaProperty targetProperty, Object anchor, Boolean enableDataValidation) in /_/src/Markup/Avalonia.Markup/Data/BindingBase.cs:line 93 at Avalonia.AvaloniaObjectExtensions.Bind(AvaloniaObject target, AvaloniaProperty property, IBinding binding, Object anchor) in /_/src/Avalonia.Base/AvaloniaObjectExtensions.cs:line 204 at DialogHostAvalonia.DialogHostStyles.XamlClosure_6.XamlClosure_9.Build(IServiceProvider ) in C:\Users\SKProCH\RiderProjects\DialogHost.Avalonia\DialogHost.Avalonia\DialogHostStyles.axaml:line 61 at Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers.<>c__DisplayClass1_0`1.b__0(IServiceProvider sp) in /_/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs:line 37 at Avalonia.Markup.Xaml.Templates.TemplateContent.Load(Object templateContent) in /_/src/Markup/Avalonia.Markup.Xaml/Templates/TemplateContent.cs:line 13 at Avalonia.Markup.Xaml.Templates.ControlTemplate.Build(TemplatedControl control) in /_/src/Markup/Avalonia.Markup.Xaml/Templates/ControlTemplate.cs:line 16 at Avalonia.Controls.Primitives.TemplatedControl.ApplyTemplate() in /_/src/Avalonia.Controls/Primitives/TemplatedControl.cs:line 289 at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Base/Layout/Layoutable.cs:line 526 at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Base/Layout/Layoutable.cs:line 361 at Avalonia.Controls.Primitives.OverlayLayer.MeasureOverride(Size availableSize) in /_/src/Avalonia.Controls/Primitives/OverlayLayer.cs:line 28 at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize) in /_/src/Avalonia.Base/Layout/Layoutable.cs:line 529 at Avalonia.Layout.Layoutable.Measure(Size availableSize) in /_/src/Avalonia.Base/Layout/Layoutable.cs:line 361 at Avalonia.Layout.LayoutManager.Measure(Layoutable control) in /_/src/Avalonia.Base/Layout/LayoutManager.cs:line 294 at Avalonia.Layout.LayoutManager.ExecuteMeasurePass() in /_/src/Avalonia.Base/Layout/LayoutManager.cs:line 254 at Avalonia.Layout.LayoutManager.InnerLayoutPass() in /_/src/Avalonia.Base/Layout/LayoutManager.cs:line 236 at Avalonia.Layout.LayoutManager.ExecuteLayoutPass() in /_/src/Avalonia.Base/Layout/LayoutManager.cs:line 148 at Avalonia.Layout.LayoutManager.ExecuteQueuedLayoutPass() in /_/src/Avalonia.Base/Layout/LayoutManager.cs:line 108 at Avalonia.Threading.JobRunner.Job.Avalonia.Threading.JobRunner.IJob.Run() in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 193 at Avalonia.Threading.JobRunner.RunJobs(Nullable`1 priority) in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 38 at Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 257 at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg) at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 204 at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 61 at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 120 at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 212 at DialogHostDemo.Program.Main(String[] args) in C:\Users\SKProCH\RiderProjects\DialogHost.Avalonia\DialogHost.Demo\Program.cs:line 13 ```
maxkatz6 commented 1 year ago

Another workaround would be to use compiled bindings.