我正在尝试使用 WebBrowser 在 WPF 应用程序中读取 HTML 文件,并且我想在 WebBrowser 和我从 HTML 文件导航的文本之间添加水印。
我尝试在
xaml.cs
文件中使用 CSS,但它不起作用。我确实有两种带有按钮的模式,第一种模式只是背景颜色和水印,当我按下按钮时,它会导航 HTML 中的内容,例如图像和文本。我尝试像下面那样做。
private void LoadDefaultWebBrowserContent()
{
string defaultHtml = "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=11\" />" +
"<html><head><style>" +
"html, body { height: 100%; margin: 0; padding: 0; background-color:#2C3040 !important; color: white; }" +
".watermark { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(-45deg); font-family:'Roboto', sans-serif; font-size: 38px;" +
"color: rgba(102, 105, 117, 0.5); white-space: nowrap; pointer-events: none; }" +
"</style></head><body>" +
"<div class=\"watermark\">ASC-YardimPanel</div>" +
"</body></html>";
ContentWebBrowser.NavigateToString(defaultHtml);
}
虽然这作为启动屏幕已经足够好了
private string ApplyHtmlStylesForDetailsWindow(string content)
{
return "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=11\" />" +
"<html><head><style>" +
"html, body { height: 100%; margin: 0; padding: 0; background-color:#2C3040 !important; color: white; }" +
".watermark { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(-45deg); font-size: 20px;" +
"color: rgba(102, 105, 117, 0.5); white-space: nowrap; pointer-events: none; }" +
"h1, h2, h3, h4, h5, h6, p, span, div {{ font-family: 'Roboto', sans-serif; font-size:12px; color: #9A9EB1 !important; }}" +
"th, td {{ font-size:12px; font-family: 'Roboto', sans-serif !important; padding: 10px; border: 1px solid white !important }}" +
"td {{ background-color: transparent !important; }}" +
"img {{ width:60%; height: 60% !important;}}" +
"</style></head><body>" +
"<div class=\"watermark\">ASC-YardimPanel</div>" +
$"{content}" +
"</body></html>";
这不起作用(实际上起作用,但“.watermark”内的“transform”行在顶部工作时不起作用)。
我不知道如何删除我自己的问题,所以我只会说我是如何解决它的。我没有使用 WebBrowser,而是使用了 WebView2 NuGet,它解决了问题。