locked
How to use a control from a dll / assembly in XAML?

    Question

  • [http://stackoverflow.com/questions/14728946/how-to-use-a-control-from-a-dll-assembly-in-xaml]

    I have a CustomControl derived class in a dll (MyNamespace.UI). How can I use this control in my main application? I've tried adding a reference to the project and then using a custom XAML namespace to point to my namespace, but it can't find it:

    <Page ...
        xmlns:ui="using:MyNamespace.UI"
        ...>
    <Canvas>
        <ui:MyControl />
    </Canvas>
    </Page>

    I get an error:

    The name "MyControl" does not exist in the namespace "using:MyNamespace.UI".

    Searching around, I managed to find the following quote:

    The name of the assembly that defines the backing types for a XAML namespace is not specified in the mapping. The logic for which assemblies are available is controlled at the app-definition level and is part of basic app deployment and security principles. Declare any assembly that you want included as a code-definition source for XAML as a dependent assembly in project settings.

    http://msdn.microsoft.com/en-gb/library/windows/apps/jj150588.aspx

    Can someone point me in the direction of the "dependent assembly" setting in the project settings?


    Edit After making sure the root namespace project setting matched the namespace of my control (MyNamespace.UI) I could get the project to compile. But I can't get it to run because I get loads of crashes with the following information:

    WinRT information: Cannot create instance of type 'MyNamespace.UI.MyControl' 

    No information on *why* it couldn't make the control though... I stepped through the constructor of `MyControl` and the exception is thrown in `InitializeComponent()`. This is the XAML of my UserControl (`MyControl`):


    <UserControl
        x:Class="MyNamespace.UI.MyControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        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">
    
        <Grid>
    
        </Grid>
    </UserControl>


    Edit 2 I deleted the Grid control, built and ran, and it worked, so then I put the Grid control back, and it still worked. Very weird.




    Wednesday, February 6, 2013 1:03 PM

Answers

  • You need to make sure the `MyNamespace.UI` assembly has a `RootNamespace` property that matches `MyNamespace.UI`. This property can't be changed in the project settings pages, but you can do it via notepad or equivalent. My `RootNamespace` was set to `UI`, and when I changed it, it started working.
    • Marked as answer by Jesse Jiang Thursday, February 7, 2013 3:16 AM
    Wednesday, February 6, 2013 2:32 PM

All replies

  • You need to make sure the `MyNamespace.UI` assembly has a `RootNamespace` property that matches `MyNamespace.UI`. This property can't be changed in the project settings pages, but you can do it via notepad or equivalent. My `RootNamespace` was set to `UI`, and when I changed it, it started working.
    • Marked as answer by Jesse Jiang Thursday, February 7, 2013 3:16 AM
    Wednesday, February 6, 2013 2:32 PM
  • Thanks for sharing the solution.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 7, 2013 3:16 AM