【发布时间】:2015-09-03 08:19:33
【问题描述】:
我有以下方法
private void PushToMainWindow(UserControl child) // where child is IMainWindowPlugin
{
_navigationStack.Push((IMainWindowPlugin)child);
...
// Here I do stuff that makes use of the child as a usercontrol
child.Width = 500;
child.Margin = new Thickness(0,0,0,0);
...
}
我想做的是通知编译器我将只接受同样实现 IMainWindowPlugin 接口的 UserControl 对象。
我知道我可以执行 if 语句并抛出或强制转换并检查 null,但这些都是运行时解决方案,我正在寻找一种方法来预先告诉开发人员对他可以添加的 UserControl 类型。有没有办法在 c# 中这样说?
更新: 添加了更多代码以显示用户控件用作用户控件,因此我不能只将子作为接口传递。
【问题讨论】:
-
如果 IMainWindowPlugin 是您的接口,您可以创建从 UserControl 继承的基类,并为此接口提供空实现。还要检查这个:stackoverflow.com/questions/178333/…
标签: c# wpf user-controls