tags:

views:

115

answers:

1

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
    #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.

+1  A: 

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

Rob Relyea
I use reflection to resolve a type based on some metadata, but WPF's Navigation system causes problems if I instantiate a page and navigate to it directly (it keeps a reference so the page is never GC'd). So I have to call NavigationService.Navigate with a URI - which means mapping the type to a URI somehow. Does BAML change between 3.5 and 4.0? I could look at the 4.0 BAML reader logic and implement something similar for 3.5.
Paul Stovell
BAML hasn't changed in 4.0. Format is the same. Compiler writes it identically. Baml2006Reader is new public API in 4.0.
Rob Relyea