最近在做WPF 客户端发送信息的功能 想要实现发送的信息包含超链接 图片等等,由于以前WEB做过文本编辑器 所以打算通过Frame 加载html页面来实现文本的编辑 然后调用C#代码取得文本编辑器里面的html。
- XAML代码
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="244*" />
<RowDefinition Height="67*" />
</Grid.RowDefinitions>
<Frame x:Name="frame" />
<WrapPanel Grid.Row="1" >
<Button Content="html" Width="100" Height="30" Click="Button_Click_1"/>
<Button Content="GO" Width="100" Height="30" Click="Button_Click"/>
</WrapPanel>
</Grid>
</Window>
2. 后台代码
private void Button_Click(object sender, RoutedEventArgs e)
{
WebBrowser wb = (WebBrowser)frame.Content;
mshtml.HTMLDocumentClass htmldocument = (mshtml.HTMLDocumentClass)wb.Document;
string html = wb.InvokeScript("getHtml").ToString(); //调用html页面函数 取得文本编辑器的html
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.frame.Source = new Uri(Directory.GetCurrentDirectory() + "/HtmlEdit.html", UriKind.Absolute);
}
3.html 页面
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Simple Examples</title>
<style>
form {
margin: 0;
}
textarea {
display: block;
}
</style>
<link rel="stylesheet" href="kindeditor-4.1.10/themes/default/default.css" />
<script charset="utf-8" src="kindeditor-4.1.10/kindeditor-min.js"></script>
<script type="text/javascript">
var editor;
KindEditor.ready(function (K) {
editor = K.create("textarea[name='content']", {
resizeType: 1,
allowPreviewEmoticons: false,
allowImageUpload: false,
items: [
'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
'insertunorderedlist', '|', 'emoticons', 'image', 'link']
});
});
function getHtml() {
return editor.html();
}
</script>
</head>
<body scroll="no">
<form>
<textarea name="content" style="width:700px;height:120px;visibility:hidden;"></textarea>
</form>
</body>
</html>
通过这样就能实现wpf 使用html页面的文本编辑器