Silverlight 3中区分大小写的UriMapper问题 [英] Case sensitive UriMapper issue in Silverlight 3

查看:47
本文介绍了Silverlight 3中区分大小写的UriMapper问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight 3的Navigation API中,UriMapper类区分大小写.对于以下uri映射

In Navigation API of Silverlight 3 the UriMapper class is case sensitive. For the following uri mapping

<nav:Frame Source="/Home">
  <nav:Frame.UriMapper>
    <uriMapper:UriMapper>
      <uriMapper:UriMapping
        Uri=""
        MappedUri="/Views/HomePage.xaml"/>
      <uriMapper:UriMapping
        Uri="/entity/{code}"
        MappedUri="/Views/EntityEditorPage.xaml?code={code}"/>
      <uriMapper:UriMapping
        Uri="/{pageName}"
        MappedUri="/Views/{pageName}Page.xaml"/>
    </uriMapper:UriMapper>
  </nav:Frame.UriMapper>
</nav:Frame>

"/entity/123"正确映射到"/Views/EntityEditorPage.xaml?code=123" 但"/Entity/123"将失败,并出现"/Views/Entity/123Page.xaml找不到"的异常.

the "/entity/123" is correctly mapping to "/Views/EntityEditorPage.xaml?code=123" but "/Entity/123" will fail with the "/Views/Entity/123Page.xaml not found" exception.

如何将UriMapper区分大小写?

How can I turn the UriMapper to case-insensitive?

谢谢.

推荐答案

Safor,

我完全按照安东尼的建议做了.

I did exactly what Anthony suggested for my own application.

这是您的XAML修改为使用CustomUriMapper:

Here is your XAML modified to use a CustomUriMapper:

<nav:Frame Source="/Home">
    <nav:Frame.UriMapper>
        <app:CustomUriMapper>
            <app:CustomUriMapping Uri="" MappedUri="/Views/HomePage.xaml"/>
            <app:CustomUriMapping Uri="/entity/{code}" MappedUri="/Views/EntityEditorPage.xaml?code={code}"/>
            <app:CustomUriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}Page.xaml"/>
        </app:CustomUriMapper>
    </nav:Frame.UriMapper>
</nav:Frame>

这是CustomUriMapping和CustomUriMapper类的代码:

Here is the code for the CustomUriMapping and the CustomUriMapper classes:

using System;
using System.Collections.ObjectModel;
using System.Windows.Markup;
using System.Windows.Navigation;

namespace YourApplication
{
    // In XAML:
    // <app:CustomUriMapper>
    //     <app:CustomUriMapping Uri="/{search}" MappedUri="/Views/searchpage.xaml?searchfor={search}"/>
    // </app:CustomUriMapper>

    public class CustomUriMapping
    {
        public Uri Uri { get; set; }
        public Uri MappedUri { get; set; }

        public Uri MapUri(Uri uri)
        {
            // Do the uri mapping without regard to upper or lower case
            UriMapping _uriMapping = new UriMapping() { Uri = (Uri == null || string.IsNullOrEmpty(Uri.OriginalString) ? null : new Uri(Uri.OriginalString.ToLower(), UriKind.RelativeOrAbsolute)), MappedUri = MappedUri };
            return _uriMapping.MapUri(uri == null || string.IsNullOrEmpty(uri.OriginalString) ? null : new Uri(uri.OriginalString.ToLower(), UriKind.RelativeOrAbsolute));
        }
    }

    [ContentProperty("UriMappings")]
    public class CustomUriMapper : UriMapperBase
    {
        public ObservableCollection<CustomUriMapping> UriMappings { get { return m_UriMappings; } private set { m_UriMappings = value; } }
        private ObservableCollection<CustomUriMapping> m_UriMappings = new ObservableCollection<CustomUriMapping>();

        public override Uri MapUri(Uri uri)
        {
            if (m_UriMappings == null)
                return uri;

            foreach (CustomUriMapping mapping in m_UriMappings)
            {
                Uri mappedUri = mapping.MapUri(uri);
                if (mappedUri != null)
                    return mappedUri;
            }

            return uri;
        }
    }
}

吉姆·麦考迪(Jim McCurdy),祝你好运

Good luck, Jim McCurdy

这篇关于Silverlight 3中区分大小写的UriMapper问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆