惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
J
Java Code Geeks
L
LangChain Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
B
Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog

博客园 - Sheva

Two Xaml Tricks - Sheva I See Cool Stuff Today DataTemplate Is A Bit Naughtier To Play With XmlnsDefinitionAttribute Is Pretty Nifty Application.DoEvents In WPF 征召译者 C# Trivia Test CreateParams Is Pretty Cool! In-Place Editing In WPF SingleLinkedList Implementation Data Binding In WPF C# Application Markup Language Regex 101 Exercise I10 - Extract repeating hex blocks from a string AvalonPaint: Features Added Regex 101 Exercise I9 - Count the number of matches Crossbow? WTF? The Architecture Of Avalon Drop Shadow Effect In WPF XamlReader.Load(): Build Up Your Own XamlPad
Crossbow: Hosting WPF Controls In Winforms Application
Sheva · 2006-02-28 · via 博客园 - Sheva

    For all of you who follow up my blog closely, you will probably remember that I have written a little application in which you can edit your xaml snippet and have that sinppet executed in real time, and this is what exactly the XamlPad.exe which is shipped with the WINFX SDK does. But in that little application, I just used the plain text editor for editing Xaml, you know, no syntax highlighting, no line numbering, no auto indentation etc. Fortunately the TextEditorControl which is available from the SharpDevelop project meets exactly what I need. So I can reuse that control without reinventing the wheel. I have tried to use the TextEditorControl in my WPF version of XamlPad, but It fails, even fails with no clue. But I know this control can work pretty well in Winforms application, so instead of hosting this control in WPF, I just use this control in Winforms application, and use Crossbow to host the necessary WPF controls in this Winforms application.
    Just as before, you have to include some important assemblies into your project, they are WindowsBase.dll, PresentationCore.dll, PresentationFramework.dll, UIAutomationProvider.dll, UIAutomationTypes.dll, and WindowsFormsIntegration.dll.
    Then you can add a Winforms TabControl into containing Form through classical Drag & Drop operation, and set those properties for the TabControl instance, immediately after that you can write something like the following in code:

 private void SetupExecutionPad()
        {
            ElementHost host 
= new ElementHost();
            host.Dock 
= DockStyle.Fill;
            executionPad 
= new ContentControl();
            host.Controls.Add(executionPad);
            tabExectionView.Controls.Add(host);
        }


    This private helper method will perform the necessary operations to host WPF control. First, we need to instantiate an ElementHost control, Just as WindowsFormsHost control does in WPF, this control is something like a wrapper or container which can has a single WPF control as its child control. and then you can create any WPF control(here we create a ContentControl for displaying the dynamically compiled Xaml) and add it to the ElementHost control's ControlCollection.
    Next, we should initialize some TextEditorControl's properties to make it support XML syntax highlighting and other flashy features.

private void SetupXamlEditor()
        {
            xamlEditor.Document.HighlightingStrategy 
= HighlightingStrategyFactory.CreateHighlightingStrategy("XML");
            xamlEditor.Document.FormattingStrategy 
= new DefaultFormattingStrategy();
            DefaultTextEditorProperties properties 
= new DefaultTextEditorProperties();
            properties.Font 
= new Font("Verdana", 10f);
            properties.IndentStyle 
= IndentStyle.Smart;
            properties.ShowSpaces 
= false;
            properties.LineTerminator 
= "\r\n";
            properties.ShowTabs 
= false;
            properties.ShowInvalidLines 
= false;
            properties.ShowEOLMarker 
= false;
            properties.UseAntiAliasedFont 
= true;
            properties.EnableFolding 
= true;
            properties.TabIndent 
= 3;
            properties.AllowCaretBeyondEOL 
= false;
            properties.AutoInsertCurlyBracket 
= true;
            properties.BracketMatchingStyle 
= BracketMatchingStyle.After;
            properties.ConvertTabsToSpaces 
= false;
            properties.ShowMatchingBracket 
= true;
            xamlEditor.TextEditorProperties 
= properties;
        }


    Okay, this is what we should do to revamp the XamlPad application, the following is the screenshot of this little fancy application:

    XamlPad in ExecutionView:

    XamlPad in XamlView

To make this application a full-blown Xaml IDE, there are hoards of things which I have to do, but I just need a simple primitive XamlPad, and this one serves me pretty well in writing Xaml snippets.
    For all of you who are interested in this little application, you can download its source code from here