OK, so I thought this would be really simple even though I have only just got by previously when working with WPF project resource files.
When my application starts I want to load in a local (shipped with the application) html file into the Webbrowser control. However I cannot find an easy way to do this! Any ideas?
I just now ran into this same problem. I was hoping to simply do this:
<WebBrowser x:Name="myWebBrowser" Source="page.html"/>
But instead, I get this error:
Relative URIs are not allowed. Parameter name: source
So that's annoying. Instead, I ended up with a solution very similar to yours in code behind:
myWebBrowser.Source = new Uri(new System.IO.FileInfo("page.html").FullName);
I'm sure there's some XAML contortionist kung-fu method with which to get around that issue, but I have no clue what it is. ^_^
Got round this in the end by setting the build action of the file to 'Copy Always' and then using Environment.CurrentDirectory
to get the application directory:
string appDir = Environment.CurrentDirectory;
Uri pageUri = new Uri(appDir + "/page.html");
myWebBrowser.Source = pageUri;
That seems to work fine.
Late to the show, but I stumbled on this when I was looking for the same thing. Got it to work in a more WPF fashioned way. The WebBrowser has a method NavigateToStream() so you can simply set the Resources Stream.
StreamResourceInfo info = Application.GetResourceStream(new Uri("Page.html", UriKind.Relative));
if (info != null)
myWebBrowser.NavigateToStream(info.Stream);
This way you can keep it a resource and you do not have to copy it to disk.
Set "Build Action" of the file to "Embedded Resource" and then use this:
webBrowserMain.NavigateToStream(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("PsHandler.Resources.gnu.html"));
Here is how it looks in project: http://i.stack.imgur.com/dGkpH.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With