Getting URI for XAML types at runtime

I am trying to do some code like this work:

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

The problem is the GetPackUriForType method - I cannot find anything in the .NET platform that will do this.

In .g.cs files that are created at compile time, a 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 this URI does not look public. I know that IUriContext can be used at runtime, but I'm trying to avoid creating an instance of the type to get its URI.

The only solution I can come up with is to try to accept a URI using namespace-based conventions. But I would like a less fragile solution.

+3
source share
1 answer

.g.cs , . .g.resources URI. baml .g.resources, .

V4 ... open baml API.

Type PackUri? PackUri , Application.LoadComponent ... , .g.cs.

, .

+1

Source: https://habr.com/ru/post/1723834/


All Articles