Getting a URI from a XAML type

574 views
Skip to first unread message

Paul Stovell

unread,
Nov 24, 2009, 7:26:59 AM11/24/09
to wpf-di...@googlegroups.com
Hi all,

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

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

The problem is I can't find any way of getting the URI from a type.

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

        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.

I was considering using using ResourceReader and Assembly.GetManifestResourceStream to get all the BAML resources and implement a dumb BAML reader, but that seems a little dangerous.

The only half decent solution I can come up with is to try to assume the URI using conventions based on the namespace. But that's whack!

--
Paul Stovell

Daniel Vaughan

unread,
Nov 25, 2009, 9:44:23 AM11/25/09
to WPF Disciples
Sounds like a job for T4 Paul. :)
Prompted by your question, I've incorporated the generation of pack
URIs for all XAML files into the T4 Metadata generation stuff I did a
while ago. Seems to be working ok. I just blogged about it:

http://danielvaughan.orpius.com/post/Using-T4-to-Generate-Pack-URIs-for-XAML-Files.aspx



On Nov 24, 1:26 pm, Paul Stovell <p...@paulstovell.com> wrote:
> Hi all,
>
> I'm trying to make a bit of code like this work:
>
>   Type pageType = typeof(Page1);
>   Uri pageUri = GetPackUriForType(pageType);
>
> The problem is I can't find any way of getting the URI from a type.
>
> In the .g.cs files that are built, the URI is embedded as part of the
> InitializeComponent:
>
>         public void InitializeComponent() {
>             if (_contentLoaded) {
>                 return;
>             }
>             _contentLoaded = true;
>             System.Uri resourceLocater = new System.Uri("*
> /PageCollection;component/pages/page1.xaml*", System.UriKind.Relative);

Jeremiah Morrill

unread,
Mar 20, 2014, 2:17:23 PM3/20/14
to wpf-di...@googlegroups.com
Not to be a Johnny-Come-Lately, seeing as this mail was from 2009, but I thought I needed this functionality recently and came across this old thing.  I ended up prototyping this function, quick and dirty like, before realizing I didn't need it.

Anyways, I put it up on codepaste here for posterity: http://codepaste.net/439f6u

--
Microsoft MVP - Client Application Development
HJT, Inc Software Developer

Peter O'Hanlon

unread,
Mar 20, 2014, 2:38:04 PM3/20/14
to WPF Disciples
Very nice Jer. Much appreciated.


--

---
You received this message because you are subscribed to the Google Groups "XAML Disciples" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wpf-disciple...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Peter O'Hanlon

Sacha Barber

unread,
Mar 21, 2014, 5:21:26 AM3/21/14
to wpf-di...@googlegroups.com
Good to see some people are still use XAML, I love WPF always will

Sacha Barber
sacha....@gmail.com
Reply all
Reply to author
Forward
0 new messages