C#设计时间路径

Sni*_*p3r 6 c# visual-studio-2010

我正在使用一些使用皮肤系统的C#自定义控件(表单,按钮等),并且依赖于项目文件夹中的外部图像(在zip文件中).现在,表单设计器无法显示控件,因为我无法获得zip文件的正确路径.我需要的是一种在设计时获得装配或解决方案的途径.

我正在使用两个项目:
DLL - 包含自定义控件.
主机应用程序 - 引用DLL并使用自定义控件.

在我的DLL自定义控件类中,在运行时,我只是使用:

string skinPath = "./Skins/" + skin + ".zip";
Run Code Online (Sandbox Code Playgroud)

哪个工作完美,但在设计时,表单设计器显示错误:

Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Skins\Black.zip'.
Run Code Online (Sandbox Code Playgroud)

看了网站上的类似问题后,我也尝试了以下内容:
1)

if (designMode)
{
    EnvDTE.DTE dte = GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
    string path = Path.GetDirectoryName(dte.Solution.FullName);
}
Run Code Online (Sandbox Code Playgroud)

表单设计器显示错误:

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

2)

if (designMode)
{
    ITypeResolutionService typeResService = GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
    string path = typeResService.GetPathOfAssembly(Assembly.GetExecutingAssembly().GetName());
}
Run Code Online (Sandbox Code Playgroud)

表单设计器显示错误:

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

3)使用Assembly类的各种不同路径.

到目前为止还没有任何效果.我正在使用Visual C#2010 Express.

Mag*_*tLU 1

您的第二次尝试 ( ITypeResolutionService) 应该可以正常工作。只要确保你打电话GetService足够晚,这样Site属性就不会为空。OnHandleCreated很好,控制构造函数太快并产生NullReferenceException.