Getting a URI from a XAML type

564 vues
Accéder directement au premier message non lu

Paul Stovell

non lue,
24 nov. 2009, 07:26:5924/11/2009
à 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

non lue,
25 nov. 2009, 09:44:2325/11/2009
à 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

non lue,
20 mars 2014, 14:17:2320/03/2014
à 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

non lue,
20 mars 2014, 14:38:0420/03/2014
à 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

non lue,
21 mars 2014, 05:21:2621/03/2014
à wpf-di...@googlegroups.com
Good to see some people are still use XAML, I love WPF always will

Sacha Barber
sacha....@gmail.com
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message