简体   繁体   中英

Silverlight / F# Reading URL parameter

I have web pages in SL (xaml) that are backed by F# behind it. I initially have the page to be a pop up window:

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

It works no problem as I initiated this way:

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

Now, I want to change that page into its own page rather than some pop-up window. I've made the necessary adjustment in the xaml and fs of SomePage to work as a page. However, I am having trouble with passing in that "id" param (the window param is not necessary anymore).

Here's how I navigate:

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

so I got the id in the url now, but how do I read it in?

The page is now;

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

Here is another attempt - You should be able to use the NavigationContext.QueryString property to get the parameter specified by the query string:

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

EDIT The following would work only in WPF - Silverlight doesn't support the overload.

Instead of calling Navigate with Uri as an argument, you can construct the page you want to navigate to (as a standard .NET object) and then call overload of Navigate that takes a page. Then you can easily specify any parameters you need as constructor arguments:

// 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM