Freigeben über


UriMapping Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines the pattern for converting a requested uniform resource identifier (URI) into a new URI.

Inheritance Hierarchy

System.Object
  System.Windows.Navigation.UriMapping

Namespace:  System.Windows.Navigation
Assembly:  System.Windows.Controls.Navigation (in System.Windows.Controls.Navigation.dll)

Syntax

'Declaration
Public NotInheritable Class UriMapping
public sealed class UriMapping
<sdk:UriMapping .../> 

The UriMapping type exposes the following members.

Constructors

  Name Description
Public method UriMapping Initializes a new instance of the UriMapping class.

Top

Properties

  Name Description
Public property MappedUri Gets or sets the uniform resource identifier (URI) that is navigated to instead of the originally requested URI.
Public property Uri Gets or sets the pattern to match when determining if the requested uniform resource identifier (URI) is converted to a mapped URI.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method MapUri Converts the specified uniform resource identifier (URI) to a new URI, if the specified URI matches the defined template for converting.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The UriMapping class enables you to specify that a particular URI is converted into another URI. You map one URI to another URI when you want to provide a user-friendly URI that does not map to the physical location of a file. You map a matching URI for only the portion of the URI that pertains to navigation within the frame that contains the mappings. For a browser-integrated application, this portion is found after the fragment delimiter (#). For example, if your browser-integrated Silverlight application is hosted at https://www.contoso.com/Default.aspx, you specify a matching URI for https://www.contoso.com/Default.aspx\#Home by creating an instance of UriMapping that matches Home. Therefore, you can move your Silverlight application to a different Web page or run it as an out-of-browser application and the URIs you have mapped will still work correctly.

You specify the pattern of a URI to match in the Uri property. You specify in the MappedUri property the URI to navigate to when the pattern is matched.

The UriMapper class has a UriMappings property which contains a collection of UriMapping objects. You add UriMapping objects to this collection to define the how URIs are converted in your application. You define the UriMapper object for a frame by assigning the UriMapper object to the UriMapper property of the frame.

The pattern you specify in the Uri property does not have to be the exact URI to match against the requested URI. The pattern can include placeholder segments in the URI that will match any value in that segment. You specify a placeholder segment by enclosing the name of the segment with the curly braces ( { and } ). These placeholder segments act as variables when mapping to the URI. For example, in the App.xaml file you can specify the following UriMapping instance:

<sdk:UriMapping Uri="Products/{type}" 
  MappedUri="/Views/ProductDetail.xaml?producttype={type}">
</sdk:UriMapping>

The pattern Products/{type} will match any request that includes Products followed by a segment. The matching URIs could include Products/bikes, Products/kitchen or any other similar request. The value specified in the second segment is included in the query string of the mapped URI where the matching {type} value is defined.

The query string of the requested URI is not evaluated when determining if the requested URI matches the pattern. For example, a value of Home in the Uri property will match a request with #Home or #Home? and any number of query string values.

Examples

The following example shows an instance of UriMapper that is defined within a frame named ContentFrame. The element contains a collection of UriMapping objects.

<sdk:Frame 
       x:Name="ContentFrame" 
       Style="{StaticResource ContentFrameStyle}" 
       Source="/Home" 
       Navigated="ContentFrame_Navigated" 
       NavigationFailed="ContentFrame_NavigationFailed">
    <sdk:Frame.UriMapper>
        <sdk:UriMapper>
            <sdk:UriMapping 
                Uri="/ProductDetail/{productid}" 
                MappedUri="/Views/ProductDetail.xaml?ProductId={productid}"/>
            <sdk:UriMapping 
                Uri="/Reports/{type}/{selection}" 
                MappedUri="/Views/ReportsPage.xaml?type={type}&amp;selection={selection}"/>
            <sdk:UriMapping 
                Uri="/{pageName}" 
                MappedUri="/Views/{pageName}.xaml"/>
        </sdk:UriMapper>
    </sdk:Frame.UriMapper>
</sdk:Frame>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Other Resources