/// <summary> /// 图标改变事件 /// </summary> /// <param name="e"></param> protected virtual void onIconChanged(EventArgs e ) { if (IconChanged!=null) { IconChanged(this,e); } } public event EventHandler TitleChanged; public event EventHandler IconChanged; } } public class PropertyPad : AbstractPadContent {
private static PropertyGrid grid = null; private static IDesignerHost host = null; public override Control Control { get { return grid; } }
public PropertyPad() :base("属性",null) { grid = new PropertyGrid(); grid.PropertyValueChanged += new PropertyValueChangedEventHandler(PropertyChanged); }
if (selectionService != null) { selectionService.SelectionChanging += new EventHandler(SelectionChangingHandler); selectionService.SelectionChanged += new EventHandler(SelectionChangedHandler); } }
public static void SelectionChangingHandler(object sender, EventArgs args) { } public static void SelectionChangedHandler(object sender, EventArgs args) { ISelectionService selectionService = sender as ISelectionService;
if (selectionService != null) { ICollection selection = selectionService.GetSelectedComponents(); if (selection.Count > 0) { object[] selArray = new object[selection.Count]; selection.CopyTo(selArray, 0); grid.SelectedObjects = selArray; } else { grid.SelectedObjects = new object[0]; grid.SelectedObject = null; } } }
{
/// <summary>
/// 返回标题
/// </summary>
string Title
{
get;
} /// <summary>
/// 返回图标
/// </summary>
Bitmap Icon
{
get;
} /// <summary>
/// 包含的控件
/// </summary>
Control Control
{
get;
} /// <summary>
/// 重新绘制
/// </summary>
void RedrawContent();
/// <summary>
/// 标题改变时响应此事件
/// </summary>
event EventHandler TitleChanged;
/// <summary>
/// 图标改变时响应此事件
/// </summary>
event EventHandler IconChanged;
}public abstract class AbstractPadContent:IPadContent
{
/// <summary>
/// 标题
/// </summary>
string title; /// <summary>
/// 图标
/// </summary> Bitmap icon;
/// <summary>
/// 包含的控件
/// </summary>
public abstract Control Control
{
get;
} /// <summary>
/// 标题
/// </summary>
public virtual string Title
{
get
{
return title;
}
}
/// <summary>
/// 图标
/// </summary>
public virtual Bitmap Icon
{
get
{
return icon;
}
} /// <summary>
/// 没有图标的构造函数
/// </summary>
/// <param name="Title"></param>
public AbstractPadContent(string Title):this(Title,null)
{
}
/// <summary>
/// 用标题和图标构造
/// </summary>
/// <param name="Title"></param>
/// <param name="iconResourceName"></param>
public AbstractPadContent(string Title,string iconResourceName)
{
this.title=Title;
if (iconResourceName!=null)
{
this.icon=null; }
} /// <summary>
/// 重绘导航条
/// </summary>
public virtual void RedrawContent()
{
}
/// <summary>
/// 卸载资源
/// </summary>
public virtual void Dispose()
{
if (icon!=null)
{
icon.Dispose();
}
} /// <summary>
/// 标题改变事件
/// </summary>
/// <param name="e"></param>
protected virtual void OnTitleChanged(EventArgs e)
{
if (TitleChanged!=null)
{
TitleChanged(this,e);
}
}
/// <summary>
/// 图标改变事件
/// </summary>
/// <param name="e"></param>
protected virtual void onIconChanged(EventArgs e )
{
if (IconChanged!=null)
{
IconChanged(this,e);
}
} public event EventHandler TitleChanged;
public event EventHandler IconChanged;
}
}
public class PropertyPad : AbstractPadContent
{
private static PropertyGrid grid = null;
private static IDesignerHost host = null;
public override Control Control
{
get
{
return grid;
}
}
public PropertyPad() :base("属性",null)
{
grid = new PropertyGrid();
grid.PropertyValueChanged += new PropertyValueChangedEventHandler(PropertyChanged);
}
public static void SetDesignableObject(object obj)
{
grid.SelectedObjects = null;
grid.SelectedObject = obj;
}
public static void SetDesignerHost(IDesignerHost host)
{
PropertyPad.host = host;
ISelectionService selectionService = (ISelectionService)host.GetService(typeof(ISelectionService));
if (selectionService != null)
{
selectionService.SelectionChanging += new EventHandler(SelectionChangingHandler);
selectionService.SelectionChanged += new EventHandler(SelectionChangedHandler);
}
}
public static void SelectionChangingHandler(object sender, EventArgs args)
{
} public static void SelectionChangedHandler(object sender, EventArgs args)
{
ISelectionService selectionService = sender as ISelectionService;
if (selectionService != null)
{
ICollection selection = selectionService.GetSelectedComponents();
if (selection.Count > 0)
{
object[] selArray = new object[selection.Count];
selection.CopyTo(selArray, 0);
grid.SelectedObjects = selArray;
}
else
{
grid.SelectedObjects = new object[0];
grid.SelectedObject = null;
}
}
}
void PropertyChanged(object sender, PropertyValueChangedEventArgs e)
{
if (host != null)
{
DesignerTransaction transaction = host.CreateTransaction("属性改变了");
transaction.Commit();
}
}
}
有问题。这是怎么回事?是我的接口定义有问题吗?
我在做菜单命令时使用了Command模式,也有这样的转换,运行正常,只不过接口定义得比较简单。还有,用反射好像只能解决创建对象的问题,不能解决这样从类到接口的强制转换问题吧?
感觉有点势单力孤,希望大家多出点意见。
其中
(IPad) content=(IPad) aPadView
里面的 content变量是什么类型的?content和aPadView都要强制转化为IPad接口类型,是其中哪一个抛出异常??我写过类似的代码,没遇到这种问题。
foreach (IPadContent content in contents)
{
ShowPad(content);
}其功能是,我将一组要处理的类保存在XML文件中的某个结点下,使用[(AddInTreeSingleton.AddInTree.GetTreeNode(viewContentPath).BuildChildItems(this)创建该结点下具体的对象,创建后以数组方式返回,并将其转换成接口类型。但是在转换时,抛出异常了。我检查过了,创建的对象是正确的,其type是ViewPad,但是就是不能转换成接口。
真是郁闷啊。