4

I have created a class library for Windows Universal Platform (Win 10 UWP).

The library houses some UserControls.

When I add the dll from this library to a Win 10 UWP app, and use the UserControls, it gives a XamlParseException as stated here in another question I posted

But when I reference the whole project, there is no exception and I can use the UserControl. This happens supposedly because there are xbf files which are not added to the Win 10 app project when I just reference the dll file.

In a certain project, I need to add the xbf files manually to the Win 10 app project, I cannot reference the whole project, I can only reference the dll and add the required files.

I tried creating a folder in Visual Studio project and adding the xbf files, and also tried creating folders with different names and copying the xbf files there in the "bin" directory through windows explorer. But no success.

So, how do I manuall add xbf files to a Windows 10 UWP project?

Update 1 :- XAML & Code for reference

public sealed partial class CustomPopupControl : UserControl
{
    internal CustomPopupControl()
    {
        this.InitializeComponent();    //-------CRASHES HERE-------
    }

    internal CustomPopupControl() : base()
    {
        Debug.WriteLine("CustomPopupControl");
        //
        //do some stuff
        //
        //
    }

    private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.Popup_Container.HorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        this.Popup_Container.VerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;
    }

    internal void OpenPopup()
    {
        Debug.WriteLine("OpenPopup");
        Popup_Container.IsOpen = true;

        var currentFrame = Window.Current.Content as Frame;
        var currentPage = currentFrame.Content as Page;
        currentPage.IsHitTestVisible = false;

        Debug.WriteLine("OpenPopup Done");
    }

    private void OnLayoutUpdated(object sender, object e)
    {
        if (Grid_Child.ActualWidth == 0 && Grid_Child.ActualHeight == 0)
        {
            return;
        }

        double ActualHorizontalOffset = Popup_Container.HorizontalOffset;
        double ActualVerticalOffset = Popup_Container.VerticalOffset;

        double NewHorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        double NewVerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;

        if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
        {
            Popup_Container.HorizontalOffset = NewHorizontalOffset;
            Popup_Container.VerticalOffset = NewVerticalOffset;
        }
    }
}

XAML :-

<UserControl
x:Class="MyLibrary.CustomPopupControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyLibrary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Popup Name="Popup_Container" LayoutUpdated="OnLayoutUpdated">
    <Border BorderThickness="1" BorderBrush="{ThemeResource AppBarBorderThemeBrush}">
        <Grid Name="Grid_Child">
            <Grid Name="Grid_Content"  Height="300" Width="400" />
        </Grid>
    </Border>
</Popup>

I use the control directly in another app, as -

CustomPopupControl myctrl = new CustomPopupControl();
myctrl.OpenPopup();
1
  • did you solve you issue?
    – bto.rdz
    Nov 18, 2016 at 1:22

2 Answers 2

5

In addition to Thomas's answer, you need to check the "Generate library layout" option in the Build configuration under the project's Properties page. enter image description here

The files we need to reference:

  • ClassLibrary1(Class Library name) Folder
    • ClassLibrary1.xr.xml
    • CustomPopupControl.xaml
  • ClassLibrary1.dll
  • ClassLibrary1.pri -> Package Resource Index file

Copy these files to anywhere and the UWP project just need to add reference to the ClassLibrary1.dll file in the Visual Studio, all of them will be added automatically.

It just throws a xaml parse exception when I try to use the UserControl on the "InitializeComponent()" method

Perhaps the .pri file is missing when you add the reference.

3
  • Thank You! I would not have been able to solve this by myself! I never found anything close to this solution anywhere on the internet. Any resources you can point me to? Dec 17, 2015 at 7:47
  • Generate library out is greyed in my case. How to solve this ? I don't understand what you're proposing to do.
    – Soleil
    Jun 9, 2018 at 17:25
  • @franklin I have the option "Generate library layout" as disabped. What can I do?
    – Mediarea
    Sep 14, 2018 at 14:02
-2

Try to define your constructor as public and not internal.

Also, your second constructor is calling base but i'm not sure to understand why you have/need it if it does not need any parameters.

Try this code:

public sealed partial class CustomPopupControl : UserControl
{
    public CustomPopupControl()
    {
        this.InitializeComponent();    

        Debug.WriteLine("CustomPopupControl");
    }

    private void OnPopupLoaded(object sender, RoutedEventArgs e)
    {
        this.Popup_Container.HorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        this.Popup_Container.VerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;
    }

    internal void OpenPopup()
    {
        Debug.WriteLine("OpenPopup");
        Popup_Container.IsOpen = true;

        var currentFrame = Window.Current.Content as Frame;
        var currentPage = currentFrame.Content as Page;
        currentPage.IsHitTestVisible = false;

        Debug.WriteLine("OpenPopup Done");
    }

    private void OnLayoutUpdated(object sender, object e)
    {
        if (Grid_Child.ActualWidth == 0 && Grid_Child.ActualHeight == 0)
        {
            return;
        }

        double ActualHorizontalOffset = Popup_Container.HorizontalOffset;
        double ActualVerticalOffset = Popup_Container.VerticalOffset;

        double NewHorizontalOffset = (Window.Current.Bounds.Width - Grid_Child.ActualWidth) / 2;
        double NewVerticalOffset = (Window.Current.Bounds.Height - Grid_Child.ActualHeight) / 2;

        if (ActualHorizontalOffset != NewHorizontalOffset || ActualVerticalOffset != NewVerticalOffset)
        {
            Popup_Container.HorizontalOffset = NewHorizontalOffset;
            Popup_Container.VerticalOffset = NewVerticalOffset;
        }
    }
}

Thanks,

2
  • It just throws a xaml parse exception when I try to use the UserControl on the "InitializeComponent()" method. It also gives a NullReferenceException as the outer exception in the app that uses the library. I have used UserControls through libraries before and there were never any xbf files generated or required, at least in WP8. This has occured for Windows 10 UWP platform. Can I not use user controls directly through code - not adding them in xaml, but creating them dynamically?? If so, why does it work if I reference the project?? Dec 14, 2015 at 8:55
  • The code I posted was simplified. The second constructor did have some parameters. Anyway, please check the answer by @FranklinChen I selected as the solution. Dec 17, 2015 at 7:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.