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

发布于 2024-09-07 14:16:42 字数 767 浏览 3 评论 0原文

在 Silverlight 3 的导航 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 设为不区分大小写?

谢谢。

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>

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.

How can I turn the UriMapper to case-insensitive?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尹雨沫 2024-09-14 14:16:42

Safor,

我完全按照安东尼为我自己的申请所建议的去做。

以下是修改为使用 CustomUriMapper 的 XAML:

<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

Safor,

I did exactly what Anthony suggested for my own application.

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>

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;
        }
    }
}

Good luck, Jim McCurdy

来日方长 2024-09-14 14:16:42

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

UriMapper uses regular expression try changing your mapping to "[V|v]iews/EntityEditorPage.xaml?code={code}" for starters this will make the V in view case insensi

牵你的手,一向走下去 2024-09-14 14:16:42

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

You can't do this easily. Ultimately you will need to derive your own UriMapperBase and do all the mapping logic yourself. Its probably not a worthwhile thing to do unless you can use some simplified mappings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文