Visual studio 2008 IDE not rendering Custom Controls correctly

半腔热情 提交于 2019-12-03 15:45:59

We have an application that had similar non-design-time display problems. By doing some research (and I don't remember exactly where we found it), we ended up creating a file

DesignTimeAttributes.xmta

Its type is that of "Design-Time Attribute File"

and in it, we just had to declare each of the control classes we had defined and qualified it as "DesktopCompatible". This way it apparently tells the designer its ok to draw, and the actual functionality within some controls (also a signature control on a handheld scanner for us), would actually invoke something during runtime that was not available in the designer. The content of the file was something like...

<?xml version="1.0" encoding="utf-16"?>
<Classes xmlns="http://schemas.microsoft.com/VisualStudio/2004/03/SmartDevices/XMTA.xsd">
  <Class Name="MyNamespace.MyControl">
    <DesktopCompatible>true</DesktopCompatible>
  </Class>

  <Class Name="MyNamespace.MyOtherControl">
    <DesktopCompatible>true</DesktopCompatible>
  </Class>

  <Class Name="AnotherNamespace.MySignControl">
    <DesktopCompatible>true</DesktopCompatible>
  </Class>
</Classes>

This was also in addition to the comments provided csauve's answer. If your constructor is trying to initialize something that is device dependent and thus throws an error because the design-time doesn't obviously have the device dlls, controls, or whatever could/would also kill that control at design time. We've created two static functions to test either way via

public static bool IsDesignTime()
{
  return System.ComponentModel.LicenseManager.UsageMode ==
         System.ComponentModel.LicenseUsageMode.Designtime;
}

public static bool IsRunTime()
{
  return System.ComponentModel.LicenseManager.UsageMode ==
         System.ComponentModel.LicenseUsageMode.Runtime;
}

and call them respectively in the constructors...

I think perhaps you need to make your controls aware of when they are in design mode. If avoidable, your control's parameterless constructor should not be performing any operations that are either expensive or have side effects (ie. loading a file from the disk).

I am assuming by your screenshots that you are using WPF. I believe design mode can be determined using DesignerProperties.GetIsInDesignMode(this)

see http://msdn.microsoft.com/en-us/library/system.componentmodel.designerproperties.getisindesignmode.aspx

public partial class MyControl : UserControl
{
   public MyControl()
   {
      InitializeComponent();
      if (!DesignerProperties.GetIsInDesignMode(this))
      {
        //Do expensive operations here
      }
   }
}

You may also want to have a read through http://blogs.msdn.com/b/jgalasyn/archive/2007/10/29/troubleshooting-wpf-designer-load-failures.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!