Change Webbrowser background color in WPF - html

I have webbrowser control in my xaml code and i want to change it's document's background color.
<WebBrowser Source="http://google.com" x:Name="IE" Navigated="IE_Navigated" />
private void IE_Navigated(object sender, NavigationEventArgs e)
{
HtmlDocument document = (HtmlDocument)IE.Document;
var color = System.Drawing.Color.Black;
document.BackColor = color;
}
it's not working;

The reason why it doesn't work is because you have a mix of WPF WebBrowser and WinForms WebBrowser and they are not compatible.
Your XAML is creating a WPF WebBrowser, but in the Navigated event, you are casting IE.Document as HtmlDocument, which is the type for the WinForms WebBrowser. Consequently that cast will fail.
Even if you change it to work, there is a further problem in that the Navigated event is fired once downloading has started, not after it has finished (as documented on MSDN). The WPF WebBrowser does not have a DocumentCompleted event like the WinForms WebBrowser does.
If you want this functionality in a WPF application, a simple approach is to use the WinForms WebBrowser instead of the WPF version. This is done by hosting it in a WindowsFormsHost as shown below:
<wfi:WindowsFormsHost Name="host" />
You have to include a reference to System.Windows.Forms (which you probably already have) and WindowsFormsIntegration, and then define the namespace as:
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
And in the code-behind:
webBrowser = new System.Windows.Forms.WebBrowser();
host.Child = webBrowser;
Then to navigate:
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
webBrowser.Navigate("http://google.com");
Then to set the background to black, you would do so on the body, like this:
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument document = (HtmlDocument)webBrowser.Document;
document.Body.Style = "background-color:black";
}
I have tested this and it works.

Related

Back Navigation using Xamarin Razor on WebViewClient

Please help me to find the way how to navigate back on webviewclient using back button on android.
I am using Xamarin hybrid app using Razor views,
I am using loadDataWithBaseURL method to find each method and move on that page, like following:
if (method == "SignIn")///sign-in.html
{
var template = new SignIn();
var page = template.GenerateString();
webView.LoadDataWithBaseURL("file:///android_asset/", page, "text/html", "UTF-8", null);
}
The above code I am using in ShouldOverrideUrlLoading(WebView webView, string Url)
the URL is passed as "hybrid:SignIn?"
Now back navigation is not working either on using OnBackPressed() method, or using OnKeyDown()
I also tried, WebBackForwardList, I can see in debug mode te current items in it , but no any url to go back on.
The url is always "about:blank"
and page is showing blank on GoBack() method.

Print a picture of a web application in asp.net

Hello I have my web application but I want to give the possibility to download and Printer-friendly image attached to a form, I have no idea how to do because I'm obviously new to C # and asp.net . As I can start ?
i try with onserverclick but i donĀ“t have idea with i do to communicate with printer is my question How communicate with printers controllers to print a image?
I implemented the following and I get an error with the printer, but I do not get no window available printers.
protected void imprime_tiff(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pqr);
pd.Print();
}
void pqr(object o, PrintPageEventArgs e)
{
System.Drawing.Image i = System.Drawing.Image.FromFile("C:\\Users\\MaxImage\\Desktop\\firma6.png");
Point p = new Point(100, 100);
e.Graphics.DrawImage(i, p);
}
and in my html i put the next
<button name="printButton" id="printButton" type="button" class="btn btn- default" onserverclick=" imprime_tiff" runat="server" >
Imprime
</button>
You won't be able to interact directly with the printer from a web browser. PrintDocument is only for WinForms applications and won't work with web applications.
The client browser needs to trigger the printing either manually (File > Print...) or through interaction with the page such as clicking on a button after which you can trigger the print dialog using JavaScript - https://developer.mozilla.org/en-US/docs/Web/API/Window/print
You can control the rendering of your page to make it "printer friendly" using a print style-sheet, there is a thorough write up here: http://www.smashingmagazine.com/2011/11/24/how-to-set-up-a-print-style-sheet/

Unhandled exception when firing WinRT 8.1 WebView ScriptNotify

I'm trying to create an MVVM Caliburn-based WinRT 8.1 app (I know that CM won't be perfectly compatible with 8.1 until version 2.0 is out, but the error does not seem to be related, as it is raised also when the handler is placed in the view code behind). One of its views contains a WebView control, whose content is set via NavigateToString (HTML contents come from app's installed assets). The HTML loaded into this control includes several hyperlinks, most of them representing cross-references to other asset-based HTML content. So when users click the link I want to override the standard navigation action, get my viewmodel notified, and let it load another HTML content from the app assets.
Here is what I did, following the post Open links in external browser in WebView (WinRT):
in the XAML code, I added to the WebView control an attribute for attaching the ScriptNotify event to my VM: cal:Message.Attach="[Event ScriptNotify] = [Action GetFromLink($eventArgs)]" (see https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions).
in my VM, the method signature is public void GetFromLink(NotifyEventArgs e).
whenever my VM loads some HTML into the WebView, it first injects a script in the HTML head which replaces the click handler of each anchor representing a cross-reference (all these anchors are marked by a class="xref" attribute). This script is hold in a constant in my VM:
private const string SCRIPT = "for (var i = 0; i < document.links.length; i++) {" +
"var className = document.links[i].getAttribute(\"class\");" +
"if (className && className === \"xfer\") {" +
"document.links[i].onclick = function() {" +
"window.external.notify('url:' + this.href);" +
"return false;" +
"}}}";
Now, when I launch the app, load an item containing one of these xref's and click on it, I get an unhandled exception telling me that "navCancelInit is undefined". I suppose this error is surfacing from JS code, but I cannot see where and how this function should be defined.
According to http://msdn.microsoft.com/library/windows/apps/br227713, I do not need any additional step for ScriptNotify when HTML has been loaded via NavigateToString. Could anyone suggest a solution?
I got an answer from a MS guy about this, so credit is not mine for this answer: it is a timing issue. I must ensure that the page has fully loaded before running the script which changes the DOM; pretty simple, if you think. Just moving the script at the end of the page, or wrapping it in an onload handler, should make the trick. Hope this can save some hair-pulling to others!
If you listen to this event: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.domcontentloaded.aspx WebView.DOMContentLoaded event, All of the script should be loaded in the WebView and you should be able to access and execute, if you are trying to do this before the scripts won't be loaded.

Navigation to other page using User Control

How can we navigate to other page on click on button in user control in windows store app? I tried by making a new frame object and calling navigate method, but no luck till yet.
thanks.
The Frame is a ContentControl that hosts the pages. If you want to navigate back and forth between pages you need to use a single Frame control. The default one is created in the App class in the default Visual Studio templates. You can save the instance reference of that Frame like by having a static property on the App class like: public static Frame RootFrame { get; private set; } and then set it where it is constructed - App.RootFrame = new Frame(). Then you can navigate simply by calling App.RootFrame.Navigate().

Playing Soundbite in WP8 App

I'm trying to write a WP8 app that plays a short sound when a button is pressed, but I cannot seem to figure out how to play the sound. Here's a quick example of my code:
XAML
<Rectangle x:Name="Rect1" Grid.Row="0" Grid.Column="0" Tap="RectTapped" Fill="White" />
App.cs
private void RectTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
MediaElement sound = new MediaElement();
sound.AutoPlay = false;
sound.Source = new Uri("Assets\\Sounds\\bark-1.wav", UriKind.Relative);
sound.Play();
}
When testing on my Nokie 820 device no sound plays. I can't understand why.
Is there something I'm doing wrong? The .wav is in my resources.
I've read that MediaElement shouldn't be used for this task. I've tried using the SoundEffect class in Xna.Framework.Audio; following the example from MSDN but that also fails because I couldn't use Content.Load as Load was not an available method of the Content class.
I've also looked at XAudio2, but as I do not know C++ I can't get my head around the examples.
You need to add MediaElement to your XAML tree
this.LayoutRoot.Children.Add(sound);
Instead of a Rectangle, use a Button. Also, MediaElement is good for playing short sounds in Silverlight applications. Make sure that the control is a part of your visual tree (add it in XAML). Then bind to the button Click event handler:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
myMediaElement.Source = new Uri("/Assets/Sounds/bark-1.wav", UriKind.Relative);
myMediaElement.Play();
}
You should be using the XNA SoundEffect class instead of the MediaElement because, well, you're playing sound effects.
The documentation is bad for this area, but this is how you do it:
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
so to play a soundeffect from your app package:
var stream = Application.GetResourceStream(filepath);
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
The advantage over MediaElement is that the SoundEffect does not need to be in the visual tree (and also it doesn't screw up the background audio player). The disadvantage is that you have none of the (sometimes useful) events that you have on MediaElement.
For Windows Phone 8.1 Silverlight, take the first two answers (from #DenDelimarsky and #onmyway133) to play the sound.
Without adding MediaElement to XAML sound not played in my case.
Use below code to play sound:
private void YourClickHandler(object sender, RoutedEventArgs e)
{
MediaElement myMediaElement = new MediaElement();
myMediaElement.Source = new Uri("/MP3/AirHornTwoBlows.mp3", UriKind.Relative);
LayoutRoot.Children.Add(myMediaElement);
myMediaElement.Play();
}
Remove the line
sound.AutoPlay=false;
or change it to:
sound.AutoPlay = true;
it will work then.