Welcome to WindowsClient.net | My Blog | Sign in | Join

Rob Relyea - XAMLified

WPF, Silverlight and XAML

Syndication

Sponsors





  • advertise here
Relative URIs in XAML, the underlying mechanism

Some URI Properties

Often, in WPF/Silverlight there are objects with a Source property – Frame, Image, ResourceDictionary, and NavigationWindow are some of them – which enable you to refer to an image or another XAML file to load.

IUriContext

XAML/WPF introduced IUriContext, which is used in 2 ways by XAML:

  1. Any object that implements IUriContext will have IUriContext.BaseUri set during XAML/BAML load.
  2. Any type converter or markupextension can access IUriContext as one of the 3 service providers available in .NET 3.x to type converters or markup extensions.

For properties of type Uri, the Uri type converter helps convert the specified uri string into a Uri object.  The UriConverter is not aware of IUriContext, so your component may need to combine the IUriContext.BaseUri with the relative Uri specified as the Source property.

xml:base

In .NET 3, XAML doesn’t support xml:base to set a different base Uri.  Current builds of .NET 4 do…

xml:base=”http://someplace.com”

Tooling Difficulty

Since some properties (Image.Source) have XAML text syntaxes which allow a URI, but aren’t of type System.Uri, there is currently no way for XAML based tools to understand the complete set of properties that can take a URI text syntax.  We hope to address this in the future.  For now, tools need to hard code the fact that there are several type converters that really just want to take a URI text syntax.

Type Converters that support URI text syntax:

  • ImageSourceConverter

(I’ll grow this list over time)

Why this came up

Mike Shim (a PM on the XAML team) and I were discussing options for a bug in .NET 4 that came our way yesterday…NavigationWindow was relying on an attached property for the BaseUri…instead of IUriContext.  For now, we plan to fix NavigationWindow to use IUriContext…and determine the complete solution for v3 compat with the attached property next milestone.

Summary

Given this info, you should be able to build a XAML Vocabulary which uses relative Uris…or just understand how WPF does it.

 

Related Content

Ashish Shetty describing Resources in WPF Applications and Silverlight 2: Demystifying URI references for app resources

MSDN: Pack URIs in WPF

Shawn Wildermuth’s Silverlight XAML Relative URI’s and “Site of Origin”

Published Friday, April 03, 2009 8:45 AM by Rob_Relyea
Filed under: ,

Comments

# re: Relative URIs in XAML, the underlying mechanism@ Friday, April 03, 2009 8:09 PM

Great job by Mike!

by John "Z-Bo" Zabroski

# Dew Drop - Weekend Edition - April 4-5, 2009 | Alvin Ashcraft's Morning Dew@ Saturday, April 04, 2009 8:13 PM

Pingback from  Dew Drop - Weekend Edition - April 4-5, 2009 | Alvin Ashcraft's Morning Dew

# re: Relative URIs in XAML, the underlying mechanism@ Friday, July 30, 2010 9:32 AM

John-

Can you please explain your XmlDoucment scenario more completely. Sample code? Expected behavior? Actual behavior?

Thanks, Rob

# re: Relative URIs in XAML, the underlying mechanism@ Monday, August 02, 2010 1:08 PM

I'm not sure of the best solution here. Likely, looking at System.Environment object, I think you'll find CurrentDirectory, or the like. Using that, you should be able to create the file you wanted.

I'd recommend using the XML Discussion Forum on MSDN at: social.msdn.microsoft.com/.../threads

Thanks, Rob

# re: Relative URIs in XAML, the underlying mechanism@ Tuesday, August 03, 2010 2:36 AM

Thanks for your reply Rob.

by John

Leave a Comment

(required) 
(required) 
(optional)
(required)