I'm struggling with the browser control in my WEC7 application. I'm trying to display a local file in the webBrowser control, and everything appears to be working except that the page does not show up in the control on my form. All I see is a white rectangle where the webBrowser control is.
I made a stand-alone test app which does nothing but load a local file into the webBrowser control. I found code elsewhere on stackoverflow which seemed pretty clear. This is the code that loads the page:
private void LoadPageBtn_Click(object sender, EventArgs e)
{
try
{
string applicationDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetNam‌e().CodeBase);
string myFile = Path.Combine(applicationDirectory, #"HTMLPage1.htm");
Uri uri = new Uri(myFile);
webBrowser1.Navigate(uri);
}
catch (Exception ex)
{
Debug.WriteLine("ERROR: " + ex.Message);
}
}
The file HTMLPage1.htm is really basic and shows up fine in any desktop browser. If I provide a bogus file name in the code then I get a file not found exception, so I'm pretty sure the file is being deployed correctly on the target (set to "always copy" in the file properties).
I catch the Navigating, Navigated, and Complete events from the webBrowser control and output some debug stuff, including the URL from the WebBrowserNavigatingEventArgs. When the code runs I get the following debug output:
Navigating: file:///Program Files/webtest/HTMLPage1.htm
Complete:
I never see the Navigated event but I'm not sure that's a problem.
And the darn webBrowser control continued to show a white rectangle. Can anyone suggest what I may be missing?
I have posted the code here in case anyone would be kind enough to try it out themselves:
https://drive.google.com/file/d/0B75fBmfP8FI4YmpvYXFXcGN1Qzg/view?usp=sharing
OK, I found out what's going on. The answer was actually found on the Toradex support forum. The WebBrowser control is just a wrapper around IE and therefore IE needs to be included in the OS before you can use the WebBrowser in your application.
My OS build does not include the web browser components, so I'm in the process of rebuilding the OS and including all the IE7 stuff.
Related
I'm working with a winform TabControl showing WebBrowsercontrols to display youtube videos.
However with two videos or more it becomes really annoying as all videos start directly.
I basically need to find out if there is a JS function, html code or a simple WebBrowser property to change, so videos are paused.
It might come in handy to find something like that for video quality too.
Has anybody ever heard of/seen where this option is stored? Or maybe the Js function itself being invoked when manually setting the quality?
EDIT:
b.DocumentCompleted += delegate { b.DocumentText=b.DocumentText.Insert(b.DocumentText.IndexOf("class=\"video-stream html5-main-video\""), "autoplay=false ");};
b.Url = new System.Uri(inp[s], System.UriKind.Absolute);
Basically this should add a new Event handler on each webbrowser form that modifies the DocumentText when the Uri that is called during creation has loaded.
Even though the browser debugger shows
<video tabindex="-1" class="video-stream html5-main-video" controlslist="nodownload" style=... src=...></video>
this isn't in the actual source code.
However I found
$oa=function(a){g.S(a.o,"video-stream");g.S(a.o,"html5-main-video");var b=a.app.g;b.zc&&a.o.setAttribute("data-no-fullscreen",!0);b.Oh&&(a.o.setAttribute("webkit-playsinline",""),a.o.setAttribute("playsinline",""));b.Nr&&a.o&&a.P(a.o,"click",a.o.play,a.o)};
in the base.js. Is it possible that youtube generates the html from the js?
How can I modify the video-tag attributes then?
I tried to modify when the event handler manipulates the video tag, since there may be DocumentCompleted events thrown from scripts or anything.
delegate (object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e){
if (e.Url.AbsolutePath != ((System.Windows.Forms.WebBrowser)sender).Url.AbsolutePath){
//...
However it still fails as there is no occurance of the specific class that on the video tag.
I now dodged this by loading the Url only when the browser tab is selected, if someone finds a real solution, feel free to share
I am facing this issue, working on Active reports 9. Every thing is fine as per our application we generate the report and in UI user will be viewing in a c# Web browser control.
Now the issue am facing is when client(user) clicks on the link present in pdf i.e. on Web Browser control. With in the same window the link is opening. They want the link to open in new window.
The problem q=am facing is if its Html control i would have used target="_blank" property but not , and its a windows application i cant even use Java script. I just gone through the properties of Picture control used in Report, theres only Hyperlink property which states in pdf it converts it to href or a tag.
Need some assistance as soon as possible is that possible to do in web browser control or should change any properties for picture control in Code behind.
Hope this help you. It worked for me.
Add the Navigating event on your webBrowser control. This will open the link in a new Browser window. In my case Google Chrome.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
try
{
if (!(e.Url.ToString().ToLower().Contains("file") || e.Url.ToString().ToLower().Contains("pdf")))
{
e.Cancel = true;
//Open Link
Process.Start(e.Url.ToString());
}
}
catch (Exception err)
{
//Handle Exception
}
}
I have a problem with blocking iframes in the webbrowser control. Currently I am using this code:
foreach (HtmlElement x in ((WebBrowser)sender).Document.GetElementsByTagName("iframe"))
{
MessageBox.Show("iframe!"); //DEBUG
x.OuterHtml = #"<iframe src=""about:blank"" frameborder=""0"" style=""display:none;""></iframe>";
// x.OuterHtml = String.Empty; //gives the same result
}
It works but when navigating to www.popuptest.com , the application just freezes completely because of this code. It shows 2 "iframe!" message boxes and freezes after closing the second one.
I have found the 2 iframes in the source code of webpage (in the advertisements shown on that website). This is the code that is causing it to freeze:
(a=document.createElement("IFRAME"),a.frameBorder=0,a.style.height=0,a.style.width=0,a.style.position="absolute",t=a,document.body&&(document.body.appendChild(a),a=t))&&(a=a.contentWindow)&&(r="1",a.document.open(),a.document.write("<!doctype html><html><head></head><body></body></html>"),a.document.close(),k(a.document))
I guess it is because of the frame being created in a different way? I have tested it both on win7/IE10 and winXP/IE6 and the result is the same. On winXP, however, it crashes and opens the debugger instead of freezing and that is how I got the faulty code.
Is there a better/safer method of removing the content of iframes?
I would try disabling frames using Download Control (DLCTL_NO_FRAMEDOWNLOAD). Here's how it can possibly be done, although I haven't tried that myself. Let us know if that works for blocking frames or not.
I can't seem to find a good way to print .htm files in c# using .net 4.0, visual studio 2010 and windows forms. When i tried to print it directly, it printed the raw html data instead of printing the "page" itself.
The only way i know to print it, is to use a WebBrowser control. When i print the document, it doesn't print colors and the page isn't printed correctly. For example, the edges are not drawn and so on.
Code for Web Browser :
public void Print()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(Core.textLog);
}
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
((WebBrowser)sender).ShowPrintDialog();
//// Print the document now that it is fully loaded.
//((WebBrowser)sender).Print();
//// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
What can i do?
Thank you!
Printing web pages will forever be the bane of your existence. There just isn't a solution out there that prints HTML directly to your printer really, really well. And even if you do find a program that does it well, it's only a matter of time until you try to print a page with some unsupported formatting, in which case you're right back where you started.
What we do is print HTML to a pdf file with a program called wkhtmltopdf. Then we open it in Acrobat (which has excellent printing support) and print from there. I can't say enough good things about wkhtmltopdf. It's command line driven, and its super, super fast. Best of all, its free. It has a companion program called wkhtmltoimage that will print to most popular image formats, too (bmp, jpg, png, etc).
After downloading/installing the program, you can run a quick test by going to your command prompt, navigating to the install folder, and typing:
wkhtmltopdf "http://YouWebAddress.com" "C:/YourSaveLocation.pdf"
It also has a ton of command line switches that give you greater control over the outputs (headers, footers, page numbering, etc etc).
Ok, as i said, problem was that edge are not drawn and neither are the backgrounds.
Here is how i solved it.
Hashtable values = new Hashtable();
values.Add("margin_left", "0.1");
values.Add("margin_right", "0.1");
values.Add("margin_top", "0.1");
values.Add("margin_bottom", "0.1");
values.Add("Print_Background", "yes");
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Internet Explorer\PageSetup", true))
{
if (key == null) return;
foreach (DictionaryEntry item in values)
{
string value = (string)key.GetValue(item.Key.ToString());
if (value != item.Value.ToString())
{
key.SetValue(item.Key.ToString(), item.Value);
}
}
}
So before i print, i go to regedit, change the values, and the document gets printed perfectly. Hope this helps other people that have the same problem when printing from webbrowser control in windows forms.
I'm writing an app for Windows Phone 7/Silverlight. When the app is either tombstoned and reactivated while on the app page containing the WebBrowser control (I've saved the Uri in app state) or that same app page is navigated to by NavigationService.GoBack() or the phone back button, it seems that as long as the control still has the webbrowser.source value, it should then render just fine, but this is not the case. Unless I use the Navigate() method, it shows a white/blank screen, no matter what I try. Unfortunately, using the Navigate() method causes the web content to download again, unnecessarily. It's especially frustrating when only a GoBack() is used to get back to the application page with the WebBrowser control, which is quite frequent in my app.
private void OnWebBrowserLoaded(object sender, RoutedEventArgs e)
{
//webBrowser1.Source = CurrentUri; //does not work, results in white/blank browser page
webBrowser1.Navigate(CurrentUri); //works, but page has to reload from web, bad UX
}
Any suggestions on a way around this problem? I've also tried putting this same code in the page loaded handler. It behaves in the same poor manner.
I've also tried saving off the HTML (SaveToString) and reloading it from app state (NavigateToString), but the web page does not render completely for some reason, even though the HTML appears fine. Also, I'd like to have access to the Host and Uri properties. I could probably work around that, if I could get the HTML to render OK from NavigateToString.
Thanks,
Jay
You should use browsertask:
using Microsoft.Phone.Tasks;
WebBrowserTask browse = new WebBrowserTask();
browse.Uri = new Uri(URL, UriKind.RelativeOrAbsolute);
//new Uri(URL,UriKind.RelativeOrAbsolute);
browse.Show();
This should solve your issue.
URL will be the URL of the page you want to visit.