Silverlight 3中区分大小写的UriMapper问题

时间:2010-07-01 06:50:25

标签: silverlight silverlight-3.0 navigation case-sensitive

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

<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 not found”异常。

如何将UriMapper变为不区分大小写?

感谢。

3 个答案:

答案 0 :(得分:2)

SAFOR,

我完全按照安东尼的建议完成了自己的申请。

以下是您修改的XAML以使用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类的代码:

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

答案 1 :(得分:1)

UriMapper使用正则表达式尝试将您的映射更改为“[V | v] iews / EntityEditorPage.xaml?code = {code}”,对于初学者来说这将使视图中的V不区分

答案 2 :(得分:0)

你不能轻易做到这一点。最终,您需要派生自己的UriMapperBase并自己完成所有映射逻辑。除非你可以使用一些简化的映射,否则它可能不值得做。

相关问题