WPF Custom Control project related issue

Hey all,

Using the Feb CTP, if you generate a Custom Control project, it looks like it's not easy to use it from an application. Or perhaps I did something wrong. I created a simple UserControl derived control (the default) and added an Ellipse into it. Compiled it.

Then I created a WPF application, added a reference to this control library. But now, there's no way I can use it from the XAML for the main window. It won't recognize the control at all - not even the fully qualified name.

I also tried to add it directly to the toolbox and got an error message that said that there were no controls in that assembly that could be added to the toolbox. Anyone knows what's going on Or is this a bug in the CTP




Answer this question

WPF Custom Control project related issue

  • arnie

    Nishant Sivakumar wrote:

    Hmmm, actually that works - it wasn't a compiler error. Just a warning! The warning says that my:UserControl1 was not expected as a child element of Window - that makes sense. But when using custom controls this will always be the acse. So that warning seems a tad too strict to me. The compiler should treat the custom control as the lowest parent in the inheritance chain that's allowed under the Window element - which in this case would be UserControl. Thanks again Thomas, for setting me in the right direction.

    This Avalon stuff is sure different from anything I've done previously - pretty interesting though :-)

    I have a similar post...I can't seem to reference my user control library in my wpf application (I'm using 3.5 frame and VS 2008). I get the "assembly cannot be found" error. I too removed the dash which seems to get rid of that error but then I can't get to the user control. I am using a page, not a window, and am wondering if this is where the problem is...

    Plz help.

    Thank you.

    Jon


  • Pankaj Arora

    Do not remove the dash in "clr-namespace"; it belongs there. Ensure your project file includes the assembly in question. Try instantiating an instance of your control in the constructor, just to see what happens.

  • Anat Oren

    Hmmm, actually that works - it wasn't a compiler error. Just a warning! The warning says that my:UserControl1 was not expected as a child element of Window - that makes sense. But when using custom controls this will always be the acse. So that warning seems a tad too strict to me. The compiler should treat the custom control as the lowest parent in the inheritance chain that's allowed under the Window element - which in this case would be UserControl. Thanks again Thomas, for setting me in the right direction.

    This Avalon stuff is sure different from anything I've done previously - pretty interesting though :-)



  • Marcio DTP

    Okay - it was the dash that caused the trouble.

    This works :

    xmlns:local="clrnamespace:CustomControlLibrary1;assembly:CustomControlLibrary1"

    But, it's still not directly possible to use the custom control from within the Xaml.

    <UserControl1/> <-- this is not recognized

    <UserControl x:Class="UserControl1"/> <-- x:Class is only allowed on root elements

    I tried x:SubClass too. Same effect.



  • snjvsax

    I can instantiate the control. I went ahead, for kicks and giggles and ran the WPF project and even though there were 2 errors (see below) the project ran and worked! Obviously a bug in VS 2008 that needs to be worked out...

    Error 1 Assembly 'PZControls' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built. C:\SWIM\PZ\c#\TestApp_WPF\MainPage.xaml 8 19 TestApp_WPF

    Error 2 The type 'local:Customer' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. C:\SWIM\PZ\c#\TestApp_WPF\MainPage.xaml 204 30 TestApp_WPF

    Cheers!

    Jon


  • Padmaja B

    Blast! Wasn't the dash. Checked the Feb CTP breaking changes and again updated the Xaml.

    <Window x:Class="WindowsApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowsApplication1" Height="300" Width="300"
    xmlns:my="clr-namespace:CustomControlLibrary1;assembly=CustomControlLibrary1"
    >
    <my:UserControl1/>
    </Window>

    That red line won't compile.



  • James Clark

    Can I just tell you about how often I forget to uncomment the line:

    this.InitializeComponent();

    Guess my head isn't in the right place today. Everything works as expected.

  • tripl3z3ro

    Thanks Thomas. That doesn't compile though. I am trying to vary it till it at least compiles, but no luck so far.

    Hard to believe that it's so difficult to use an user control from Xaml. So I must be missing something somewhere.

    As you said I am inclined to think I need to add some sort of xmlns or similar attribute. Can't figure out what though.

    Thomas S. Andersen wrote:

    Did you set the xmlns

    ...
    <Window...
    xmlns:local="clr-namespace:MyProject;assembly:MyAssembly">
    ...
    <local:MyControl />
    ...

    (This is from the top of my head, so it might not work.)



  • Raman Iyer

    I've tried to do the same thing and everything builds just fine, but my custom control is never displayed. Here is what I did.

    - Created a Control Library project in EID.
    - Renamed Scene1.xaml & Scene1.xaml.cs to use MyControl instead of Scene1.
    - Changed the class name to MyControl in both MyControl.xaml and MyControl.xaml.cs.
    - Compiled the assembly.
    - Created a new Standard Application project in EID.
    - Added a reference to the assembly created with my control library project from above.
    - Added an instance of MyControl to Scene1 in my application.
    - Everything builds and runs, but I don't see the custom control in my application. I have done this same type of thing without compiling the control in a separate assembly and everything worked.

    Are those the same steps you followed

  • badwiring

    Did you set the xmlns

    ...
    <Window...
    xmlns:local="clr-namespace:MyProject;assembly:MyAssembly">
    ...
    <local:MyControl />
    ...

    (This is from the top of my head, so it might not work.)


  • WPF Custom Control project related issue