我正在使用一些C#自定義控件(窗體,按鈕等),它們使用皮膚系統,並且依賴於項目文件夾中的外部圖像(在zip文件中)。現在,表單設計人員無法顯示控件,因爲我無法獲得正確的zip文件路徑。我需要的是一種在設計時獲得組件或解決方案的途徑。C#設計時間路徑
我使用了兩個項目:
DLL - 包含自定義控件。
主機應用程序 - 引用DLL並使用自定義控件。
在我的DLL自定義控件類,在運行時,我只是使用:
string skinPath = "./Skins/" + skin + ".zip";
這完美的作品,但在設計時,窗體設計器顯示錯誤:
Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Skins\Black.zip'.
查看了網站上的類似問題,我也試過以下內容:
1)
if (designMode)
{
EnvDTE.DTE dte = GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
string path = Path.GetDirectoryName(dte.Solution.FullName);
}
表單設計顯示錯誤:
Object reference not set to an instance of an object.
2)
if (designMode)
{
ITypeResolutionService typeResService = GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
string path = typeResService.GetPathOfAssembly(Assembly.GetExecutingAssembly().GetName());
}
表單設計顯示錯誤:
Object reference not set to an instance of an object.
3)使用各種不同的路徑的Assembly類。
沒有任何工作。 我正在使用Visual C#2010 Express。
呃。不要使用'as'那樣的。改用cast。 –
對不起,但它不是我的代碼。我一直在複製和粘貼來自很多不同問題的代碼,嘗試不同的解決方案。 – Snip3r
然後將'Black.zip'複製到'C:/ Program Files(x86)/ Microsoft Visual Studio 10.0/Common7/IDE/Skins /':) –