【问题标题】:Why does the string change itself when passing as a QueryString?为什么字符串在作为 QueryString 传递时会自行改变?
【发布时间】:2013-06-08 14:56:48
【问题描述】:

我从事一个基于 Windows Phone 7.5 的项目。

我的第 1 页将 URL 作为 QueryString 传递。 e.Value.ToString() 是 http://xiciimgs.xici.net/d189532038.0/001_7384_%B8%B1%B1%BE_%B8%B1%B1%BE_s.jpg ,这是正确的字符串。

    private void myWB1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string passingURL = e.Value.ToString();
        if (!String.IsNullOrEmpty(passingURL)) 
        {
           App.goToPage("/PictureViewPage.xaml?pictureurl=" + passingURL);
        }
    }

在第 2 页,我尝试通过下面的代码获取 URL,

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string strUrl = "";
        base.OnNavigatedTo(e);
        NavigationContext.QueryString.TryGetValue("pictureurl", out strUrl);
        strUrl = Uri.EscapeUriString(strUrl);
    }

在 TryGetValue 之后,strUrl 是 "http://xiciimgs.xici.net/d189532038.0/001_7384_¸±±¾_¸±±¾_s.jpg" ,

在 Uri.EscapeUriString 之后,strUrl 原来是"http://xiciimgs.xici.net/d189532038.0/001_7384_%C2%B8%C2%B1%C2%B1%C2%BE_%C2%B8%C2%B1%C2%B1%C2%BE_s.jpg"

小小惊喜,App.gotoPage 除了导航什么也没做:

    public static void goToPage(string targetUri)
    {
        var rootFrame = (App.Current as App).RootFrame;
        rootFrame.Navigate(new System.Uri(targetUri, System.UriKind.Relative));
    }

我的问题是为什么会发生这种情况以及如何获得正确的网址?

【问题讨论】:

    标签: c# silverlight windows-phone-7 query-string urlencode


    【解决方案1】:

    终于找到问题了,
    Uri.EscapeUriString先传值,避免导航时出现格式异常。

    private void myWB1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string passingURL = e.Value.ToString();
        passingURL = Uri.EscapeUriString(passingURL);
        if (!String.IsNullOrEmpty(passingURL)) 
        {
           App.goToPage("/PictureViewPage.xaml?pictureurl=" + passingURL);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多