Silverlight / F#读取URL参数

时间:2011-03-14 16:13:37

标签: silverlight f#

我在SL(xaml)中有网页,后面有F#支持。 我最初将页面设置为弹出窗口:

type SomePage(window : ChildWindow, id: int) as this =
inherit UriCanvasControl("/AssemblyName;component/somePage.xaml", "Some Page")

我开始这样做没有问题:

let someWindow = new ChildWindow()
someWindow.Content <- new SomePage(someWindow , id) // assume have id from somewhere
someWindow.Title <- "Some Page"
someWindow.Show()

现在,我想将该页面更改为自己的页面而不是某个弹出窗口。我已经在SomePage的xaml和fs中进行了必要的调整,以作为页面工作。但是,我无法传递“id”参数(不再需要窗口参数)。

以下是我的导航方式:

let parent = this.Parent :?> Frame
parent.Navigate(new Uri("/AssemblyName;component/somePage.xaml?id=" + id, UriKind.Relative)) |> ignore

所以我现在在网址中输入了id,但是如何阅读呢?

现在页面;

type SomePage() as this =
    inherit UriUserControl("/AssemblyName;component/somePage.xaml", "Some Page")

1 个答案:

答案 0 :(得分:0)

这是另一种尝试 - 您应该能够使用NavigationContext.QueryString属性来获取查询字符串指定的参数:

type SomePage() as this =
  inherit UriUserControl("/AssemblyName;component/somePage.xaml", "Some Page") 
  let id = 
    if not(this.NavigationContext.QueryString.ContainsKey("id")) then 0
    else int (this.NavigationContext.QueryString.["id"])
  // .. the rest of the page

编辑 以下内容仅适用于WPF - Silverlight不支持重载。

您可以构造要导航到的页面(作为标准.NET对象),然后调用Navigate的重载,而不是以Uri作为参数调用Navigate拿一页。然后,您可以轻松地将所需的任何参数指定为构造函数参数:

// Declaration of parameterized page
type SomePage(id) as this =
  inherit UriUserControl("/AssemblyName;component/somePage.xaml", "Some Page") 

// Code that navigates to SomePage page with 'id' as argument
let parent = this.Parent :?> Frame
let somePage = new SomePage(id)
parent.Navigate(somePage)