Search code examples
c#wpfxamlnugetwebbrowser-control

C# Custom abstraction layer for WPF UserControl when moved into a NuGet-Package makes it no longer function


At my work we have a custom class public partial class ExtendedWebBrowser : UserControl, IDisposable. This class have the following XAML

<UserControl x:Class="Judex.AmPHI.MobileClient.Components.View.xaml.ExtendedWebBrowser"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:helper="clr-namespace:Judex.AmPHI.MobileClient.Components.View.Helpers"
             xmlns:AmphiSysWebBrowser="clr-namespace:AmphiSys.WebBrowser.Abstractions;assembly=AmphiSys.WebBrowser.Abstractions"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             x:Name="ExtendedWebBrowserControl">
    <AmphiSysWebBrowser:WebBrowser x:FieldModifier="public" x:Name="WebBrowserControl" 
                helper:WebBrowserHelper.Source="{Binding ElementName=ExtendedWebBrowserControl, Path=Source}" 
                helper:WebBrowserHelper.SuppressScriptErrors="{Binding ElementName=ExtendedWebBrowserControl, Path=SuppressScriptErrors}"
                helper:WebBrowserHelper.ShouldAlwaysOpenLinkInCurrentWindow="{Binding ElementName=ExtendedWebBrowserControl, Path=ShouldAlwaysOpenLinkInCurrentWindow}" />
</UserControl>

As we can see in the XAML we make use of a custom abstraction class (AmphiSys.WebBrowser.Abstractions) for the WebBrowser component created by Microsoft. This class looks like:

[ContentProperty(nameof(WebBrowserControl))]
public sealed class WebBrowser : Control, IDisposable
{
    public static Func<IWebBrowser> WebBrowserControlInitializer;

    public static readonly DependencyProperty WebBrowserControlProperty = DependencyProperty.Register(nameof(WebBrowserControl),
                                                                                                      typeof(IWebBrowser),
                                                                                                      typeof(WebBrowser),
                                                                                                      new PropertyMetadata(default(IWebBrowser)));

    public IWebBrowser WebBrowserControl
    {
        get
        {
            return (IWebBrowser)GetValue(WebBrowserControlProperty);
        }
        set
        {
            SetValue(WebBrowserControlProperty, value);
        }
    }

    public static readonly DependencyProperty AddressProperty = DependencyProperty.Register(nameof(Address),
                                                                                            typeof(string),
                                                                                            typeof(WebBrowser),
                                                                                            new PropertyMetadata(default(string), PropertyChangedCallback));

    private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var webBrowser = (WebBrowser)d;
        webBrowser.WebBrowserControl.Address = e.NewValue.ToString();
    }

    public void Dispose()
    {
        WebBrowserControl.Dispose();
    }

    public string Address
    {
        get
        {
            return (string)GetValue(AddressProperty);
        }
        set
        {
            SetValue(AddressProperty, value);
        }
    }

    static WebBrowser()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(WebBrowser), new FrameworkPropertyMetadata(typeof(WebBrowser)));
    }

    public WebBrowser()
    {
        WebBrowserControl = WebBrowserControlInitializer();
    }
}

When this code in bundled within the same project, meaning we have a library project for the 'AmphiSys.WebBrowser.Abstractions' the WebBrowser component works just as it should. But when this project is moved into a NuGet-Package for reusability the WebBrowser now only displays a black screen. No exception is thrown and I haven't been able to find any useful information within any log files. After looking at this for several hours I am now lost as I cannot seem to understand why moving a class library project into a NuGet-Package would make it no longer function with this being the only change.


Solution

  • I realized I had by mistake removed the "AssemblyInfo.cs" file which resulted in the library class 'AmphiSys.WebBrowser.Abstractions' didn't work as expected when moved into a NuGet-package