web browser content changing event windows phone - wpf

I am using a web browser in my windows phone app to display an advertisement. I used iframe to load the advertisement link in web browser. After every 10 sec a new advertisement will load.
How can I get the event when the content in web browser changing ?.. ie when the time the new advertisement is loading.
Because I need to parse an xml each time when the content in the web browser changes.
Thanks
Arun

You need to write a bit of JavaScript that will send a notification to your C# code. When you IFrame loads the next advert, send a notification:
window.external.notify("Advert changed");
Then handle the ScriptNotify event on the brwsoer control:
void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == ""Advert changed")
{
// do something!
}
}
See a worked example on MSDN here.

Related

How to stop CefSharp ChromiumWebBrowser from following links

Right now we use the old WPF WebBrowser control to show a web site in our program. Since this old control is based on Internet Explorer 11 and a lot of newer web sites show a message, that they are not running on Internet Explorer 11, we are looking for a replacement for this old control.
We took a look into WebView2 but unfortunately this browser doesn't show up in our setup (WPF control hosted in an ElementHost in WinForms, hosted in an ocx in COM, used in MS Access).
So we take a look in CefSharp now. I didn't get to trying if this browser shows up in our setup because I don't find a solution for a simple requirement in my test program.
I'd like to restrict the user to follow links that might be part of the web site. In WebView2 there's a NavigationStarting event which can be canceled, but I didn't find anything like that in CefSharp.
There's an AddressChanged event, but this also fires when the web site is re-directing.
So, is it somehow possible with CefSharp to load an initial website (including any re-directions) and then doing nothing when a link inside the web site is clicked?
You can cancel navigation in OnBeforeBrowse
public class CustomRequestHandler : CefSharp.Handler.RequestHandler
{
protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
if (request.TransitionType == TransitionType.LinkClicked)
{
//Cancel the request by returning true
return true;
}
return base.OnBeforeBrowse(chromiumWebBrowser, browser, frame, request, userGesture, isRedirect);
}
}
browser.RequestHandler = new CustomRequestHandler();

Web Page Loaded Event using FiddlerCore

When I open a URL in the browser (Internet Explorer), using FiddlerCore I can get all the raw HTTP requests/responses. However
I would like to identify the time when a Web Page is completely loaded in the browser. Is it possible to do this? Is there any event which I can attach to?
Thanks
Shashank
You could start a Stopwatch when you connect to the webpage,this would be in the FiddlerApplication.BeforeRequest. And stop the stopwatch when the Session.state in the FiddlerApplication.AfterSessionComplete. But that would only be for the first request. If you want to accurately measure a complete "page load" you should do this for all resources (.js/.css/etc.) on the page.

How can I refresh browser in silverlight 4 application?

I have an Silverlight 4 application, in log-in process I have some validations and data preparation, for any reason in each step if I get an exception, I want to stop my application and refresh the browser but I want save my exception message from my last session and then update my error text block in log-in screen and inform my user about exception message.how can I save my variable in session and then refresh browser?
On silverlight side you may use HtmlPage namespace ,
Beside this refreshing a page in silverlight is not a good practice,and shows your app. as if broken.Siverlight is a RIA. using benefits of this Rich platform is better I think.
HtmlPage.Window.Navigate(HtmlPage.Document.DocumentUri, "_self");
/* You may also add Query String to your URL as Bryant said,
Uri myUri=new Uri(HtmlPage.Document.DocumentUri.toString()+ "?Exception=True", UriKind.RelativeOrAbsolute);
HtmlPage.Window.Navigate(myUri, "_self");
*/
Hope helps!
Just take the current url and append your error message to the query string and then use javascript to open the current page to that url. You can then get the query string from silverlight to get the error message.

Add a new page to a Silverlight project

I know this may sound pretty dumb but how do I add a new webpage to the Silverlight Project?
I did a "Add new Item" and select an xaml file.
Now I want to open that file via the webbrowser. The File is called PrintPage.xaml.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Navigate(new Uri("PrintPage.xaml" , UriKind.Relative), "_blank");
}
Page not found error.
Xaml pages are Silverlight Pages, not Web pages. With Silverlight you are always on the same Web page, but displaying different Silverlight pages internally within the Silverlight object.
If you want to change Silverlight pages, start with either the Silverlight Navigation App or Silverlight Business App templates as an example. The Silverlight navigation systems all work using browser "bookmark" links (they have a # at the end of the HTML page) and use the parameters after the # to determine the target page. By using bookmark links the hosting web page does not get refreshed (otherwise the Silverlight application would reload).
If you actually want to go to a new web page, with a new Silverlight application, you want to add an ASPX or HTML page instead and browse to that.
Use this code
HtmlPage.Window.Invoke("ShowBrowserIFrame", url);
url is path of your hemp page, and ShowBrowserIFrame is java script function, used to open html web page.
function ShowBrowserIFrame(url) {
BrowserDivContainer.css('display', 'block');
$('<iframe id="' + jobPlanIFrameID + '" src="' + url + '" style="height:100%;width:100%;" />')
.appendTo(BrowserDivIFrameContainer);
slHost.css('width', '0%');
}
Follow this link to add html page inside silver light project.

Silverlight based gadget to open link in a browser?

I have written a sidebar gadget which displays a series of links using silverlight. I can host the silverlight in a web site and when I click on the links they open in a new tab. When I package it up as a gadget however the links appear in the gadget, and they can be clicked on, but they do not open a browser window to display the link.
What do I need to do to get this to work?
It's best to launch external links from gadgets using your preferred shell execute method; doing so will launch them in the default browser. When developing gadgets, all my links have an onclick handler which points to the following method:
function launchLink() {
if (this.href.slice(0,7) == "http://") {
System.Shell.execute(this.href);
return false;
}
}
Theoretically, you could modify this slightly and invoke it from your Silverlight code using the HTML bridge.
JS code
function launchLink(href) {
System.Shell.execute(href);
}
Silverlight
// HtmlPage requires using System.Windows.Browser
HtmlPage.Window.Invoke("launchLink", "http://some.com/");

Resources