UserControl 的多级继承在 Visual Studio DesignerView 中引发错误
multiple levels of inheritance of UserControl throws error in Visual Studio DesignerView
今天我遇到了 "solved" 几天前遇到的问题。
但首先是事实:
Visual Studio 2013 设计视图
我有一个自定义的 UserControl,它首先继承自 UserControl。
一段时间后,我需要在 CustomControl 和 UserControl 之间使用一个 Base-Class 来在我的控件上传播一些虚函数(所有控件都应该继承 "BaseControl")。
所以 类 看起来像:
public class CustomControl : BaseControl
{
}
public class BaseControl : UserControl
{
public virtual bool Finished()
{
return false;
}
}
现在,如果我尝试使用 Visual Studio Designer 打开 CustomControl,我会收到以下错误:
Visual Studio cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed
几天前,这个问题只在调试配置下出现,我在下班后使用 [DesignTimeVisible(false)]
-attribute @BaseControl 修复了它。
但是在我的解决方案中将文件移动到另一个项目后,我又遇到了错误。
winforms 用户控件:
修改设计者在CustomControl.Designer.cs
文件中生成的代码:
partial class CustomControl:BaseControl
{
...
}
此外,BaseControl
应该有 .Designer.cs
文件,否则将无法运行。
BaseControl
的代码隐藏:
public partial class BaseControl : UserControl
{
public BaseControl()
{
// this code is generated automatically
InitializeComponent();
}
public virtual bool Finished()
{
return false;
}
}
wpf 用户控件:
BaseControl
的代码应位于普通 BaseControl.cs
文件中。 BaseControl
不应有任何 .xaml
或 .xaml.cs
文件(这很重要)
namespace MyNamespace
{
public class BaseControl:UserControl
{
//...
}
}
在您 CustomControl
的 .xaml.cs
文件中,应该有:
namespace MyNamespace
{
public partial class CustomControl: BaseControl
{
//....
}
}
在您的 CustomControl
的 .xaml
文件中:
<local:BaseControl x:Class="MyNamespace.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<Grid>
</Grid>
</local:BaseControl>
而不是:
<UserControl x:Class="MyNamespace.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
</Grid>
</UserControl>
如果您的项目类型是 x64,这些问题也会出现。不知道为什么,也许是因为 VS 仍然是一个 32 位应用程序,但是在与设计器一起工作时切换到 AnyCPU 会有所帮助。
汤姆
今天我遇到了 "solved" 几天前遇到的问题。 但首先是事实: Visual Studio 2013 设计视图
我有一个自定义的 UserControl,它首先继承自 UserControl。 一段时间后,我需要在 CustomControl 和 UserControl 之间使用一个 Base-Class 来在我的控件上传播一些虚函数(所有控件都应该继承 "BaseControl")。
所以 类 看起来像:
public class CustomControl : BaseControl
{
}
public class BaseControl : UserControl
{
public virtual bool Finished()
{
return false;
}
}
现在,如果我尝试使用 Visual Studio Designer 打开 CustomControl,我会收到以下错误:
Visual Studio cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed
几天前,这个问题只在调试配置下出现,我在下班后使用 [DesignTimeVisible(false)]
-attribute @BaseControl 修复了它。
但是在我的解决方案中将文件移动到另一个项目后,我又遇到了错误。
winforms 用户控件:
修改设计者在CustomControl.Designer.cs
文件中生成的代码:
partial class CustomControl:BaseControl
{
...
}
此外,BaseControl
应该有 .Designer.cs
文件,否则将无法运行。
BaseControl
的代码隐藏:
public partial class BaseControl : UserControl
{
public BaseControl()
{
// this code is generated automatically
InitializeComponent();
}
public virtual bool Finished()
{
return false;
}
}
wpf 用户控件:
BaseControl
的代码应位于普通 BaseControl.cs
文件中。 BaseControl
不应有任何 .xaml
或 .xaml.cs
文件(这很重要)
namespace MyNamespace
{
public class BaseControl:UserControl
{
//...
}
}
在您 CustomControl
的 .xaml.cs
文件中,应该有:
namespace MyNamespace
{
public partial class CustomControl: BaseControl
{
//....
}
}
在您的 CustomControl
的 .xaml
文件中:
<local:BaseControl x:Class="MyNamespace.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace">
<Grid>
</Grid>
</local:BaseControl>
而不是:
<UserControl x:Class="MyNamespace.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
</Grid>
</UserControl>
如果您的项目类型是 x64,这些问题也会出现。不知道为什么,也许是因为 VS 仍然是一个 32 位应用程序,但是在与设计器一起工作时切换到 AnyCPU 会有所帮助。 汤姆