Can Silverlight initiate Page Refreshes?

2019-01-14 16:14发布

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?

5条回答
你好瞎i
2楼-- · 2019-01-14 16:26

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-14 16:26

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

查看更多
老娘就宠你
4楼-- · 2019-01-14 16:31

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();
查看更多
爷、活的狠高调
5楼-- · 2019-01-14 16:34

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.

查看更多
该账号已被封号
6楼-- · 2019-01-14 16:49

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!

查看更多
登录 后发表回答