I'm able to load a local HTML file into my WebView like this (this works fine):
var fileName = "Views/Default.html";
var localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
var url = new NSUrl(localHtmlUrl, false);
var request = new NSUrlRequest(url);
WebView.LoadRequest(request);
I'd like to reference a CSS file (also local) in my HTML file:
<link href="Content/style.css" rel="stylesheet">
The CSS file does exist inside of a Content folder, the build action is set to Content for said file.
How can I reference/load the CSS file in question via my html file? Possible?
UPDATE: Had css and html in separate folders. Put them both in a Content folder then updated the hrefs which solved the issue while using LoadRequest.
Check this link
https://developer.xamarin.com/recipes/ios/content_controls/web_view/load_local_content/
Section Additional information
Html generated in code can also be displayed, which is useful for customizing the content. To display an Html string, use the LoadHtmlString method instead of LoadRequest. Passing the path to the Content directory helps the web view resolve relative Urls in the Html, such as links, images, CSS, etc.
// assumes you've placed all your files in a Content folder inside your app
string contentDirectoryPath = Path.Combine (NSBundle.MainBundle.BundlePath, "Content/");
string html = "<html><a href='Home.html'>Click me</a></html>";
webView.LoadHtmlString(html, new NSUrl(contentDirectoryPath, true));
Related
I am parsing CSS file, and receive background-image property value in CSS, but the URL is not a full URL. It looks like this url(../images/logo.png)
How to get a full URL link for background-image property in CSS like http://www.aaa.aaa/aaa/aaa/aaa.png OR How to get an URL link for the image folder which contains that specific image?
You can get the absolute path by using Server.MapPath
var absoluteUrl = this.Server.MapPath("../images/logo.png".Replace("..","~"));
This will result in http://www.yoursite.com/images/logo.png
I loaded a html local file in a WebBrowser control within a WinForm. In the html code, I defined some variables in %variables%.
My question is how to transfer/reference strings/data from WinForm to the %viarables% defined in the loaded html page, and refresh the loaded html page displayed on the WebBrowser.
Following is the code of loading a local html file from. Any suggestion will be appreciated.
By the way, it is a WinForm application, not asp.Net.
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/{1}", curDir + "\\Forms", fileName));
Instead of trying to replace your variables after loading the HTML, which could be problematic, why not read the source file, replace the variables, then load the updated document into the web browser control? This seems much more straightforward.
For example:
string dir = Path.Combine(Directory.GetCurrentDirectory(), "forms");
string html = File.ReadAllText(Path.Combine(dir, fileName));
foreach (string variable in GetListOfVariables())
{
html = html.Replace(variable, GetReplacementForVariable(variable));
}
webBrowser1.DocumentText = html;
Do you have control over contents of the HTML file? You could try InvokeScript method paired with a Javascript function prepared in the document beforehand.
If you don't control the contents, then you can inject javascript from your code. Here are some examples of doing that.
I tried to save rendered C# web browser to my hdd. But it only save html source only, no css or jquery js file. I tried with 3 methods.
It only creates only html file without any css, and jquery js files.
File.WriteAllText(myDocumentPath, webBrowser5.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser5.Document.Encoding));
2.It only creates content text. No html, css, js
File.WriteAllText(#"text.txt", webBrowser5.Document.Body.InnerText);
3.It creates bigger html file, still no css or jquery js files
writer.Write(webBrowser5.DocumentText);
private void button1_Click(object sender, EventArgs e)
{
String source = ("viewsource.html");
StreamWriter writer = File.CreateText(source);
String myDocumentPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\111.html";
//It only creates only html file without any css, and jquery js files.
File.WriteAllText(myDocumentPath, webBrowser5.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser5.Document.Encoding));
//it only creates content text
File.WriteAllText(#"text.txt", webBrowser5.Document.Body.InnerText);
//It creates bigger html file, still no css or jquery js files
writer.Write(webBrowser5.DocumentText);
writer.Close();
}
I found solution on codeproject. MHTML is the way to go.
http://www.codeproject.com/Articles/12977/Harvesting-Web-Content-into-MHTML-Archive
Other solution is save as image:
http://www.codeproject.com/Articles/6601/Capture-an-HTML-document-as-an-image
I'm converting a desktop application that hosts HTML content into an online application. I have various large pieces of prebuilt static html that need to be included in an MVC page depending on user actions. Each of the static html pages includes at least one img tag that references a file that, in a Web Forms page would be located in the same directory. Here's a very simplified example:
Static html:
<html>
<!-- Large chunk of html -->
<img src="logo.gif" >
<!-- More html -->
</html>
SampleController:
Dim html as String = GetFileContentAsString("~/Content/Sample/Static.html")
ViewBag.StaticHTML = PrepareHTMLContent(html)
View:
#Html.Raw(ViewBag.StaticHTML)
The result of the above is a page (e.g. http://localhost:12345/Sample) with a broken image link in the middle of the HTML. I'm already preprocessing the html where possible to strip out useless tags and insert Javascript and CSS links but preprocessing the image paths is unreliable because they could be anywhere in the static html and are quite likely to be inconsistent or otherwise quirky.
So how can I place (or create) the image file in the right location for the static html to pick it up? Is there any other option (bearing in mind that I also need to link CSS and JavaScript files and that the static html has a bunch of other files associated with it that need to be kept in a single location)?
Or is there a way to define or override the location the dynamic MVC page is built?
Easiest thing would be to have /Sample return an HTML page that simply loads /~Content/Sample/Static.html into an iframe so that the browser will resolve relative paths in static.html to be within /~Content/Sample/
I'm using a WebBrowser control and the text displays but it isn't using the linked css, it just appears as plain text.
I'm populating it like so
webBrowser1.DocumentText = some_text;
In some_text is <link rel="stylesheet"href="PF.css"> along with the rest of the html
When I save some_text to a file and have the WebBrowser navigate to it it works fine
webBrowser1.Navigate(#"C:\test.html"); and PF.css is in C:\
I've put PF.css in my project folder, where all the class files are.
How can I make the WebBrowser control use/display my linked css file?
I don't want to save off my string to a file and then navigate to it.
thanks
mshtml.HTMLDocument CurrentDocument = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
mshtml.IHTMLStyleSheet styleSheet = CurrentDocument.createStyleSheet("", 0);
StreamReader streamReader = new StreamReader(#"C:\PF.css");
string text = streamReader.ReadToEnd();
streamReader.Close();
styleSheet.cssText = text;
kind of a krapy way to do it, but everything I read seems to point to the webbrowser control can't do css unless you Navigate to a file/url and it's included in there.
BTW you have to add a ref to Microsoft.mshtml.
Maybe future versions of this control could handle linked stylesheets...
You can use inline style as..
<html>
<head>
<style>
// all my style here
</style>
</head>
<body>
..
..
</body>
</html>
If by put it in your project folder, do you mean your bin folder, or the folder with the .sln file in it? If it's the latter, you have to put it in the same folder as your executable (e.g. bin/Debug/PF.css, bin/Release/PF.css, etc).
Alternatively, you can embed it in the HTML using a <style type="text/css"><!-- CSS Here --></style>, and replace the comment with your CSS. If you don't want to hardcode it in where ever you get some_text from, you can dynamically replace it by loading PF.css:
using (StreamReader sr = new StreamReader("PF.css"))
{
some_text = String.Format(some_text, sr.ReadToEnd());
}
That example assumes some_text contains the following:
<style type="text/css">
{0}
</style>