Bra*_*ler 16 c# wpf winforms avalonedit
我知道,我必须用一个ElementHost显示在WinForm的WPF控件,但由于WPF控件是第三方软件,而且只配备了一个XML文件和DLL文件.
控件是AvalonEdit,我将两个ICSharpCode.AvalonEdit.xml和ICSharpCode.AvalonEdit.dll文件添加到我的项目中,然后我去了Project -> Add Reference并添加了DLL作为参考.现在我可以ICSharpCode在我的代码中访问命名空间,所有的类和方法都暴露出来,但从这一点来说,我不确定如何在我的WinForm中实际使用该控件.
我期待WPF控件出现在解决方案资源管理器中,但事实并非如此.我尝试将ElementHost控件添加到我的WinForm,但是当我尝试选择托管内容时,没有控件出现,所以它不知道我的WPF控件.如何在WinForm中使用AvalonEdit WPF控件?
Tom*_*nes 20
如果您希望能够在设计时设置托管内容,则控件需要成为解决方案的一部分.实现此目的的一种方法是创建一个自定义WPF用户控件,其中包含您要使用的AvalonEdit组件.即
创建WPF用户控件库项目并创建包含AvalonEdit组件的用户控件.
将用户控件项目添加到Winforms解决方案.
现在,您应该能够选择新的用户控件作为托管内容.
或者您可以直接在代码中添加AvalonEdit控件,如下所示:
public Form1()
{
InitializeComponent();
ElementHost host= new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100,100);
AvalonEditControl edit = new AvalonEditControl();
host.Child = edit;
this.Controls.Add(host);
}
Run Code Online (Sandbox Code Playgroud)
不确定控件的调用方式,因此请根据需要更换AvalonEditControl.
public Form1()
{
InitializeComponent();
ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
textEditor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
textEditor.FontSize = 12.75f;
string dir = @"C:\Temp\";
#if DEBUG
dir = @"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif
if (File.Exists(dir + "CSharp-Mode.xshd"))
{
Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
// Apply the new syntax highlighting definition.
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();
}
//Host the WPF AvalonEdiot control in a Winform ElementHost control
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
host.Child = textEditor;
this.Controls.Add(host);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9312 次 |
| 最近记录: |