Andy Sparrow
Andy Sparrow

Reputation: 17

Can't use control from other project in xaml WPF

I want use a control from another class

xmlns:control="clr-namespace:Devices;assembly=Devices"

 <Grid>
    <control:D_main VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"/>
</Grid>

And this control

<UserControl x:Class="Devices.D_main"
    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"
    xmlns:local="clr-namespace:Devices"
    mc:Ignorable="d"
   Height="704.667" Width="1173.833">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="611*"/>
        <ColumnDefinition Width="563*"/>
    </Grid.ColumnDefinitions>
    <local:DHControl x:Name="HControl" Width="Auto" Height="Auto" Grid.ColumnSpan="2"/>
</Grid>

The class is public. All other controls I can use in this way, but with this I see an error.

Exception: Cannot create an instance of "D_main".

StackTrace:

at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.InstanceBuilderOperations.InstantiateType(Type type, Boolean supportInternal) at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(ILocalInstanceBuilderContext context, ViewNode viewNode) at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode) at Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode) at Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.UserControlInstanceBuilder.Instantiate(ILocalInstanceBuilderContext context, ViewNode viewNode) at Microsoft.VisualStudio.DesignTools.Designer.InstanceBuilders.LocalInstanceManager.CreateInstance(IInstanceBuilder builder, ViewNode viewNode)

When I hover over the control before build I see a message:

Index was outside the bounds of the array.

Upvotes: 0

Views: 224

Answers (1)

Mahsa Alamdari
Mahsa Alamdari

Reputation: 62

Your question and the code you attached is a bit unclear. I can give you an example. If you have a class (UserControl) like this:

<UserControl x:Class="myProject.newUC"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="200" d:DesignWidth="200">
    <Grid Background="Red">
            
    </Grid>
</UserControl>

You can use this class in a window like this:

<Window x:Class="myProject.MainWindow"        
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-myProject"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <local:newUC></local:newUC>
        </Grid>
    </Window>

Upvotes: 1

Related Questions