Can Silverlight initiate Page Refreshes?
Asked Answered
M

5

19

UPDATE: An alternative title for this could be: How do I call javascript from my silverlight 2.0 application.

Here is a quick question for all you Silverlight gurus.

I have a Silverlight app that displays a stopwatch countdown. The app is hosted in an ASP.Net web application, What I want it to do is when the stopwatch hits zero, the app forces a server page refresh of the hosting page.

Is this possible?

If so, any chance of a code snippet?

Musil answered 16/2, 2009 at 9:38 Comment(0)
N
20

Apparently you can call a JS script from Silverlight using

HtmlPage.Window.CreateInstance

or

HtmlPage.Window.Invoke

The JavaScript to refresh a page is

location.reload(true)

I'm not a Silverlight or JavaScript expert though, so not sure if it works in all browsers, or even at all.

EDIT:

Scott posted a comment to this answer with his final solution.

He needed to create a JavaScript client function on the ASP.Net page called reload() that did the location.reload(true). Then it was a simple matter from his C# code to reload:

HtmlPage.Window.Invoke("reload");

As @R4cOON suggested, you can also use:

System.Windows.Browser.HtmlPage.Document.Submit();
Nonstandard answered 16/2, 2009 at 11:23 Comment(2)
HtmlPage.Window.Invoke("location.reload(true);"); didn't work for me after all. When I find out exactly what does, I'll post it here. thanks anyway.Musil
ok, this was 90% of the way there. All I needed was to create a javascript client function on the ASP.Net page in my case, called reload(), that did the location.reload(true). Then it was a simple matter from my C# code to have this line: HtmlPage.Window.Invoke("reload"); Thanks again.Musil
M
25

Why not simply stay on the Silverlight side and call

System.Windows.Browser.HtmlPage.Document.Submit();

Works a treat for me. The whole page gets reloaded and the Silverlight control kicks backs in.

Miseno answered 18/6, 2009 at 8:8 Comment(3)
This was exactly the sort of thing I was looking for. I'll check it out soon, and +1 your answer if it works the way I was expecting. Thanks!Musil
What do you mean? Of course it won't work on the server, Silverlight is on the client side! Do you mean you want to restart your web server from the client?Miseno
This works if the page has a <form> element on it (e.g. any Web Forms page). Without a <form> element (e.g. many ASP.NET MVC pages), there is nothing to submit, and you get an InvalidOperationException.Savitt
N
20

Apparently you can call a JS script from Silverlight using

HtmlPage.Window.CreateInstance

or

HtmlPage.Window.Invoke

The JavaScript to refresh a page is

location.reload(true)

I'm not a Silverlight or JavaScript expert though, so not sure if it works in all browsers, or even at all.

EDIT:

Scott posted a comment to this answer with his final solution.

He needed to create a JavaScript client function on the ASP.Net page called reload() that did the location.reload(true). Then it was a simple matter from his C# code to reload:

HtmlPage.Window.Invoke("reload");

As @R4cOON suggested, you can also use:

System.Windows.Browser.HtmlPage.Document.Submit();
Nonstandard answered 16/2, 2009 at 11:23 Comment(2)
HtmlPage.Window.Invoke("location.reload(true);"); didn't work for me after all. When I find out exactly what does, I'll post it here. thanks anyway.Musil
ok, this was 90% of the way there. All I needed was to create a javascript client function on the ASP.Net page in my case, called reload(), that did the location.reload(true). Then it was a simple matter from my C# code to have this line: HtmlPage.Window.Invoke("reload"); Thanks again.Musil
F
6

Just a quick note on why HtmlPage.Window.Invoke("location.reload(true);"); doesn't work: it appears from my quick testing that the way Silverlight implements Invoke is to look up a property with the given name on the specified JavaScript object and call it. So this code would say, "Find a property on the window object named 'location.reload(true);' and call it with zero arguments." There is, instead, a GetProperty method that will let you get the window's location property and invoke reload on that with the parameter true. The final code looks like this:

((ScriptObject)HtmlPage.Window.GetProperty("location")).Invoke("reload", true);

Fryer answered 2/11, 2012 at 21:30 Comment(1)
This is a good option if the host HTML page does not contain a form element that can be submitted.Laith
P
2

In my case I didn't want to do asp.net kind of postback and lose my Silverlight page context so I refreshed my page by navigating to it. That way my language changes I made in my nav bar were reflected on my page or View as they come in 2008 template.

this.ContentFrame.Navigate(new Uri("", UriKind.Relative));

In this case I was on my home page. "" means home page if you examine the Silverlight templated navigation solution.

Postbox answered 3/11, 2009 at 0:4 Comment(0)
A
1

It is possible for a silverlight app to call out into javascript on the page which in turn could force your page refresh. So yes this is definitely possible!

Amii answered 16/2, 2009 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.