C# 当目标为x64时,自定义组合框从工具箱中移除,可与任何CPU配合使用
Windows 10 1803 x64内部版本17134.165, Visual Studio 2017 15.7.5 c# 我在表单上有一个自定义组合框。如果我以任何CPU为目标构建解决方案,那么就没有问题,但是如果我将目标更改为x64,那么在设计器中就会出现此错误,并且组合框将从工具箱中删除。designer.cs中的代码是完整的,程序仍然可以正常运行C# 当目标为x64时,自定义组合框从工具箱中移除,可与任何CPU配合使用,c#,visual-studio-2017,toolbox,C#,Visual Studio 2017,Toolbox,Windows 10 1803 x64内部版本17134.165, Visual Studio 2017 15.7.5 c# 我在表单上有一个自定义组合框。如果我以任何CPU为目标构建解决方案,那么就没有问题,但是如果我将目标更改为x64,那么在设计器中就会出现此错误,并且组合框将从工具箱中删除。designer.cs中的代码是完整的,程序仍然可以正常运行 Could not find type 'TestCustomComboBox.ComboBoxCustom'. Please make
Could not find type 'TestCustomComboBox.ComboBoxCustom'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using settings for your current platform or Any CPU.
如果我恢复到任何CPU,那么错误就会消失,组合框会回到工具箱中。我的电脑是x64
完整的测试程序:
using System.Drawing;
using System.Windows.Forms;
namespace TestCustomComboBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
/// <summary>
/// Custom combobox with colour coding
/// </summary>
public class ComboBoxCustom : ComboBox
{
public ComboBoxCustom()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
e.DrawBackground();
var item = (ComboBoxItem)Items[e.Index];
Brush brush = new SolidBrush(item.ForeColor);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{ brush = Brushes.Yellow; }
e.Graphics.DrawString(item.Text, this.Font, brush, e.Bounds.X, e.Bounds.Y);
}
}
public class ComboBoxItem
{
public ComboBoxItem() { }
public ComboBoxItem(string pText, object pValue)
{
text = pText; val = pValue;
}
public ComboBoxItem(string pText, object pValue, Color pColor)
{
text = pText; val = pValue; foreColor = pColor;
}
string text = "";
public string Text
{
get { return text; }
set { text = value; }
}
object val;
public object Value
{
get { return val; }
set { val = value; }
}
Color foreColor = Color.Black;
public Color ForeColor
{
get { return foreColor; }
set { foreColor = value; }
}
public override string ToString()
{
return text;
}
}
public class ComboObject
{
public string DisplayMembers;
public int ValueMembers;
public int argb;
public ComboObject(string theDisplayMembers, int theValueMembers, int theArgb)
{
DisplayMembers = theDisplayMembers;
ValueMembers = theValueMembers;
argb = theArgb;
}
public override string ToString()
{
return DisplayMembers;
}
}
}
使用系统图;
使用System.Windows.Forms;
命名空间TestCustomComboBox
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
}
///
///带有颜色编码的自定义组合框
///
公共类ComboBox自定义:ComboBox
{
公共ComboxCustom()
{
this.DrawMode=DrawMode.OwnerDrawFixed;
}
受保护的覆盖无效OnDrawItem(DrawItemEventArgs e)
{
基础项目(e);
e、 牵引杆接地();
变量项=(ComboBoxItem)项[e.Index];
笔刷=新的SolidBrush(项目前景色);
if((e.State&DrawItemState.Selected)=DrawItemState.Selected)
{brush=brushs.Yellow;}
e、 Graphics.DrawString(item.Text、this.Font、brush、e.Bounds.X、e.Bounds.Y);
}
}
公共类ComboBoxItem
{
公共ComboBoxItem(){}
公共ComboxItem(字符串pText,对象pValue)
{
text=pText;val=pValue;
}
公共ComboxItem(字符串pText、对象pValue、颜色pColor)
{
text=pText;val=pValue;foreColor=pColor;
}
字符串文本=”;
公共字符串文本
{
获取{返回文本;}
设置{text=value;}
}
对象val;
公共对象价值
{
获取{return val;}
设置{val=value;}
}
颜色前景色=颜色。黑色;
公共颜色前景色
{
获取{return foreColor;}
设置{foreColor=value;}
}
公共重写字符串ToString()
{
返回文本;
}
}
公共类组合对象
{
公共字符串显示成员;
公众谘询委员会成员;;
公共int argb;
公共组合对象(显示成员字符串、值成员整数、RGB整数)
{
显示成员=显示成员;
ValueMembers=ValueMembers;
argb=argb;
}
公共重写字符串ToString()
{
返回成员;
}
}
}
AFAIK Visual Studio和/或VS Designer作为32位进程运行,因此当您仅将控件编译为64位时,设计器将无法加载它。所以IIRC您必须编译“AnyCPU”或“x86”的控件,至少在开发期间要使用设计器。。。(也许可以看看下面的相关/类似问题:)谢谢,现在有点道理了。不过,我确实有一个棘手的问题。我调用一个仅在编译为x64时运行的程序集。它确实可以工作,但有警告。我猜它是在任何CPU下设计的,并编译为x64进行部署。顺便问一下,你会如何将一条评论标记为最佳答案?