Usercontrol Nightmare

I do not know what I am doing wrong but I am starting to pull my hair out.

I am using VS and EID to create a quite complex UI, and I am trying to use usercontrols to encapsulate as much as I can.

However...

Any user control I create and put on a window or page either never renders and just shows up with the classname of the usercontrol in the top left corner, OR when you place it on the page in EID it says "Cannot instantiate <ClassName>" which takes up the whole of the page/window and means you can't position it properly (but will render when you run it).

I must be missing something here, should a newly created UserControl (in VS) with no extra functionality and just simple xaml not just render correctly straight off

Cheers

Simon




Answer this question

Usercontrol Nightmare

  • Tres London - MSFT

    Hi Simon,

    I am a bit reluctant to make guesses about why the control cannot be instantiated correctly in EID since you should most likey see this issue to be fixed in the next release.

    Regarding the UserControl content property, I don't think there is a workaround for that - today we look for the existence of Content property on a Content control (UserControl is a ConetentControl) and set the value - this is good in most cases such as Button where you want to see each Button with a new content. However, we need to find a better solution - I will keep you posted. The only way today is to clear the Content property using the property grid context menu.

    Thanks,
    -Unni



  • Johan Zietsman

    I've narrowed it down even further.

    It occurs when you have the the project open both in VS and EID, and when you make a change to the layout heirarchy.

    ie 

    <UserControl>
        <Grid>
           <Canvas></Canvas>
           <ListBox></ListBox<
        </Grid>
    </UserControl>

    to

    <UserControl>
        <Canvas>
           <Canvas></Canvas>
           <ListBox></ListBox>
        </Canvas>
    </UserControl>

    It seems when EID tries to reinitialize the control after a build, it is still using the previous version of the .g.cs file and so throws an exception when it tries to cast the new first UIElement to the previous type.

    Cheers

    Simon



  • Bas van Atteveldt

    No,

    I get the big red box with the exclamation, but having attached the debugger to ExpID.exe and dragging my control on to the surface it's apparent that the exception is being thrown in the connect method of the control.

    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
    switch (connectionId)
    {
    case 1:
    this.Sea_BeginStoryboard = ((System.Windows.Media.Animation.BeginStoryboard)(target));
    return;
    case 2:
    this.Canvas = ((System.Windows.Controls.Canvas)(target));
    return;
    case 3:
    .
    .
    .
    }

    I am pretty sure that this is to do with the fact that I have VS and EID open at the same time, and that my controls are in a different project to the application becuase it only occurs when I have made a change to the control which culminates in a reordering of control connectionId's, suggesting that something in the UI project isn't getting flushed properly on a rebuild.



  • Manuel Burggraf

    Hi Simon,

    What is the method that you are using to get around, and which is working for you To the best of my knowledge (and I hope we are talking about the same issue here), you cannot get around the BAML resource loading issue without platform support that will only come around during its next CTP.

    Also, what does the constructor for you UserControl look like

    Thanks,
    -Unni



  • Ludwig

    OK, I'm not surprised because I often need to clean the solution too or manually delete the cache. Thank you for the tip.

  • clarkewu

    "I've solved the issue with only the classname of the object being displayed,"

    I ve the same problem, how did u solve it


  • delasare

    Hi Simon,

    In order to give you an efficient response, can you post a sample or a piece of code



  • KTamas

    Hi Simon,

    This is a known issue - however, I think the problem is a little different from what you discovered. We have issues today in loading the right BAML resource. We are working on resolving this shortly. I will keep you posted.

    Thanks,
    -Unni



  • John Faris

    Unni,

    Currently I am either ignoring it if I am working on the control and not the mainwindow, or otherwise clean, close VS, close EID reload. Sometimes, a clean in VS and then a rebuild of the controls project thru EID, then a rebuild of the UI Project thru EID fixes it. Although this is so rare it could be conincidence.

    Constructor below...

    public
    ChartControl()
    {
    InitializeComponent();
    raceAreaTransformGroup.Children.Add(scaleTransform);
    raceAreaTransformGroup.Children.Add(translateTransform);
    }

    Regarding the default setting of a usercontrol's content property, is there anyway to stop EID, filling it with the classname so the control doesn't render when you place it in the workspace Or is that a question for the expression forum

    Help most appreciated

    S



  • Lawrence Parker

    Unni,

    Can you be a little more elaborative, I am doing so much with usercontrols that I see this quite a lot, any hints of workarounds or better practices to avoid it would be very much appreciative.

    Simon



  • Gary Strader

    Hi Simon,

    When you say EID throws and exception, do you mean that the disable surface get disabled with the big red box with an exclmation sign on it

    Thanks,
    -Unni



  • Connie Daniel

    Chaz,

    Sorry should have posted this at the time

    I've solved the issue with only the classname of the object being displayed, for some reason EID was putting the classname in the content property so that as being rendered as text. Haven't found a way of getting around this other than changing it after the fact.

    The Instantiation issue appears to occur when I have a canvas on a grid. When it tries to connect everything up when you add the control in EID, it tries to cast the target to a Grid and throws an excepetion saying Type System.Windows.Controls.Canvas cannot be cast to a System.Windows.Controls.Grid.

    Code below: (Usercontrol, usercontrol autogen code, instance of control on page)

    <UserControl x:Class="SiSoft.RegattaManager.CourseSetter.UI.Controls.ChartControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="Auto" Height="Auto" xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

    <Grid x:Name="Grid" RenderTransformOrigin="0.5,0.5">
    <
    Grid.ColumnDefinitions>
    <ColumnDefinition/>
    </
    Grid.ColumnDefinitions>
    <
    Grid.RowDefinitions>
    <
    RowDefinition/>
    </
    Grid.RowDefinitions>
    <
    Canvas
    Width="1550" Height="700" ClipToBounds="True"
    x:Name="Canvas" RenderTransformOrigin="0.5,0.5">
    <
    Canvas
    Margin="20,20,20,20" Width="1500" Height="650"
    Canvas.Left="0" Canvas.Top="0" x:Name="RaceArea"
    RenderTransformOrigin="0.5,0.5"
    >
    <
    Canvas
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,0,0"
    Width="1500" Height="650" Canvas.Left="0" Canvas.Top="0" x:Name="Shallow"
    RenderTransformOrigin="0,0"
    >
    <Path Stroke="#FF000000" StrokeThickness="0.5" Data="...>
    <Path.Fill>...</Path.Fill>
    </Path>
    </Canvas>
    .
    .
    .
    </Canvas>
    </Canas>
    </Grid>
    </UserControl>

    ----

    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
    {
    switch (connectionId)
    {
    case 1:
    this.Grid = ((System.Windows.Controls.Grid)(target)); //Exception here when control added to page from EID
    return;
    case 2:
    .
    .
    .
    }
    }

    ----

    <Canvas>
    <ControlNS:ChartControl x:Name="ChartControl"/>
    </Canvas>

    ---

    Hope this is helpful

    Simon



  • Jamie Sellars

    Solved the issue, I think WPF is keeping a cached copy of the usercontrol.g.cs file, if the issue arises then doing a clean on the solution in VS and then restarting EID seemed to help

  • Usercontrol Nightmare