开发者

Passing Parameters to Silverlight

开发者 https://www.devze.com 2022-12-31 08:35 出处:网络
Any ideas on ho开发者_StackOverflow社区w to pass parameters to Silverlight on startup from URL QueryString?

Any ideas on ho开发者_StackOverflow社区w to pass parameters to Silverlight on startup from URL QueryString?

Thank You


One approach you can take is to expose a method that can be accessed from JavaScript. So in your xaml.cs file you need to add the following to your constructor:

this.Loaded += new RoutedEventHandler(Page_Loaded);

Then add the following event handler:

void Page_Loaded(object sender, RoutedEventArgs e)
{
    HtmlPage.RegisterScriptableObject("YourControlName", this);
}

and:

[ScriptableMember]
public void YourMethod(string yourData)
{
    // Do your stuff here
}

Then in the ascx or aspx page where your Silverlight control is instantiated add the following JavaScript:

var silverlightControl;

function onSilverlightLoad(sender, args) {
    silverlightControl = sender.getHost();
    var yourData = "some data";
    silverlightControl.Content.YourControlName.YourMethod(yourData);
}

It does also mean that your Silverlight control has to be instantiated via the <object... tag rather than via <asp:Silverlight...


Although Chris's method will work, it's easier to pass startup information through Silverlight's initialization parameters feature.


If all you need to do is get at key-value pairs of the query string, there is a much simpler way using the HtmlPage class:

HtmlPage.Document.QueryString["your_key"];
0

精彩评论

暂无评论...
验证码 换一张
取 消