当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How can I associate css to html string, received from server?

问题描述:

I'm working on a metro app.

Say I receive a string (html) and I load that string in a webview.

How can I associate a .css to that html?

Update:

WebViewColumnRight as suggested in an answers is an android method; I'm working on a metro app, c#.

网友答案:

As JanivZ said the one way is that your returned html string should contain the reference to the required CSS so that it can be loaded automatically.

or the other option you have is, you can use WebView.loadDataWithBaseURL

htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +    htmlData;
webView.loadDataWithBaseURL(baseUrl, htmlData, "text/html", "UTF-8", null);

After this WebView will be able to find your CSS file from the base URL directory. And if both HTML and CSS are coming from the same base URL then no need to specify a baseURL in webView.loadDataWithBaseURL

网友答案:

I would say that the 'returned string' should contain the references necessary to load the css file ( like below for example)

<head>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>

and have the elements in it reference rules defined in that css file. No ?

网友答案:

If you are doing a WEBVIEW.NavigateToString(stringHtmlContent) then please inject a

string styleSheetString = "<link rel='stylesheet' type='text/css' href='test.css' />"

strUpdatedHtmlContent = stringHtmlContent.Insert(stringHtmlContent.IndexOf("<head>",styleSheetString);

then do

WEBVIEW.NavigateToString(strUpdatedHtmlContent)

I just hope this helps. O one more thing please take care of the path of the css and all which I just figured from the above discussions...

网友答案:

External stylesheet that points to full path where css file can be found.

 <head>
    <link rel="stylesheet" type="text/css" href="full path to css file on server">
    </head>

Internal Style Sheet that comes with your html and requires no dependency on a css file

<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>

Inline Style is a messier way to do what an Internal style sheet does(has no dependency on external css file)

<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
分享给朋友:
您可能感兴趣的文章:
随机阅读: