开发者

Getting URI's for XAML types at runtime

I'm trying to make a bit of code like this work:

Type pageType = typeof(Page1);
Uri pageUri = GetPackUriForType(pageType);

The problem is the GetPackUriForType method - I can't find anything in the .NET framework that will do this.

In the .g.cs files that are built at compile time, the URI is embedded as part of the InitializeComponent code:

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
开发者_如何学Python    #line hidden
}

But that URI doesn't appear to be public anywhere. I know IUriContext can be used at runtime, but I'm trying to avoid instantiating the type just to get its URI.

The only solution I can come up with is to try to assume the URI using conventions based on the namespace. But I'd like a less brittle solution.


The .g.cs contains it as you mention. Also the .g.resources contains all the URIs. But you need to crack open the baml in the .g.resources to understand the root type.

V4 nothing changes here...except cracking open the baml has a public API.

Why are you trying to go from Type to PackUri? If you have a PackUri and want to create the right type, the Application.LoadComponent call should work fine...you shouldn't need to pass in the root instance as the .g.cs does.

Hope that helps. -Rob

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜