locked
Cannot create instance for usercontrol RRS feed

  • Question

  • Hi All,

    In my silverlight application I tried to add one user control (Button) and access it from other pages.

    Steps:

    #1 Created one folder as "CommonControls"
    #2 Added one silverlight usercontrol.

    <UserControl x:Class="Testapplication.CommonControls.ButtonPrint"
                 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"
                 d:DesignHeight="300"
                 d:DesignWidth="400"
                 mc:Ignorable="d">
        <Grid x:Name="LayoutRoot" Background="White">
    <Button x:Name="btnPrint"
                    Width="45"
                    Click="btnPrint_Click"
                    Content="Print"
                    FontWeight="Bold" Height="35" />

      </Grid></UserControl>

    #3 In the target page I have given like this:

    <navigation:Page x:Class="TestApplication.Page1"
    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:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    xmlns:local="TestApplication.CommonControls"
    Title="Page"
    d:DesignHeight="480"
    d:DesignWidth="640"
    mc:Ignorable="d">
     <Grid x:Name="LayoutRoot" VerticalAlignment="Top">
    <local:ButtonPrint/>
        </Grid>
    </navigation:Page>

    I didn't get any buid errors.But it is displaying like "Cannot create instance for ButtonPrint "

    How to resolve this issue.Can you please help me?

    Thanks,
    DIP
    Monday, November 19, 2012 9:59 AM

Answers

  • Hi Dhanalakshmi,

    It seems that the namespace is not correct. Please follow steps below for a simple test.

    After create a new silverlight application, add an item use "Silverlight User Control" template( named ButtonPrint1), in the ButtonPrint1.xmal, add code as below.

    <Grid x:Name="LayoutRoot" Background="White">
            <Button x:Name="btnPrint"
                     Width="45"             
                     Content="Print"
                     FontWeight="Bold" Height="35" />
    
        </Grid>

    Then, build the solution, you will find the control that you just create in the toolbox, drag the control to MainPage.xaml, it will add the namespace for you.

    <UserControl xmlns:my="clr-namespace:TestButtonPrint"  x:Class="TestButtonPrint.MainPage"
        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 x:Name="LayoutRoot" Background="White">
            <my:ButtonPrint1 />
        </Grid>
    </UserControl>

    Hope this can help you.

    Best Regards.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Tuesday, November 20, 2012 9:16 AM
    Moderator

All replies

  • Hi Dhanalakshmi,

    It seems that the namespace is not correct. Please follow steps below for a simple test.

    After create a new silverlight application, add an item use "Silverlight User Control" template( named ButtonPrint1), in the ButtonPrint1.xmal, add code as below.

    <Grid x:Name="LayoutRoot" Background="White">
            <Button x:Name="btnPrint"
                     Width="45"             
                     Content="Print"
                     FontWeight="Bold" Height="35" />
    
        </Grid>

    Then, build the solution, you will find the control that you just create in the toolbox, drag the control to MainPage.xaml, it will add the namespace for you.

    <UserControl xmlns:my="clr-namespace:TestButtonPrint"  x:Class="TestButtonPrint.MainPage"
        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 x:Name="LayoutRoot" Background="White">
            <my:ButtonPrint1 />
        </Grid>
    </UserControl>

    Hope this can help you.

    Best Regards.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework

    Tuesday, November 20, 2012 9:16 AM
    Moderator
  • Replace  xmlns:local="TestApplication.CommonControls"

    By          xmlns:local="clr-namespace:TestApplication.CommonControls;assembly=TestApplication"


    Dhananjay Tech Lead

    Tuesday, November 27, 2012 6:41 PM