【发布时间】:2009-11-25 03:17:11
【问题描述】:
我正在尝试编写一些类似这样的代码:
Type pageType = typeof(Page1);
Uri pageUri = GetPackUriForType(pageType);
问题在于 GetPackUriForType 方法 - 我在 .NET 框架中找不到任何可以执行此操作的方法。
在编译时构建的 .g.cs 文件中,URI 作为 初始化组件代码:
public void InitializeComponent() {
if (_contentLoaded) {
return;
}
_contentLoaded = true;
System.Uri resourceLocater = new System.Uri("/PageCollection;component/pages/page1.xaml", System.UriKind.Relative);
#line 1 "..\..\..\Pages\Page1.xaml"
System.Windows.Application.LoadComponent(this, resourceLocater);
#line default
#line hidden
}
但该 URI 似乎并未在任何地方公开。我知道 IUriContext 可以 在运行时使用,但我试图避免实例化类型只是为了得到 它的 URI。
我能想出的唯一解决方案是尝试假设 URI 使用基于命名空间的约定。但我想要一个不那么脆弱的解决方案。
【问题讨论】: