一步一步:
-
如果尚未完成,请使用nuget将AvalonEdit安装到项目中
-
将Avalon编辑器添加到窗口中,它将如下所示:
<avalonEdit:TextEditor
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
Name="MyAvalonEdit"
FontFamily="Consolas"
FontSize="10pt"
ShowLineNumbers="True"
LineNumbersForeground="#FF2B91AF" />
-
向项目中添加一个新的xml文件(将其放在“resources”文件夹中,或者可以放在任何地方),并将其命名为sql.xshd(xml语法突出显示定义)(或者可以随意命名)
-
从这里的答案之一粘贴语法定义
-
将生成操作更改为“嵌入资源”
-
记下项目的默认命名空间(右键单击项目
->属性->应用程序)
-
在窗口加载中添加以下代码以连接语法
hightlighting;将DefaultNameSpace和Folder替换为适当的
private void Window_Loaded(object sender, RoutedEventArgs e)
{
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DefaultNamespace.Folder.sql.xshd"))
{
using (var reader = new System.Xml.XmlTextReader(stream))
{
MyAvalonEdit.SyntaxHighlighting =
ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(reader,
ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
}
}
}
-
如果资源名称有问题,可以找到名称
在大会的所有资源中:
// Get names of embedded resources
var resourceNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
我在Dirk Bahle's找到了一个很好的SQL xshd
open source editor
此处为项目:
https://github.com/Dirkster99/Edi/blob/master/Edi/Edi/AvalonEdit/Highlighting/SQL.xshd
<?xml version="1.0"?>
<!-- Shades of Red-Brown: #a31515, #cf4315, #ffb96e, #ffdc95 -->
<!-- Shades of Bright-Blue: #0077dc #008fe4 #8dbbdc #8de8ff -->
<SyntaxDefinition name="SQL" extensions=".sql"
xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<!-- T-SQL Reference: http://msdn.microsoft.com/de-de/library/ms189826%28v=sql.90%29.aspx -->
<Color name="Digits" foreground="DarkBlue" exampleText="3.1415f"/>
<Color name="Comment" foreground="Green" exampleText="string text = "Hello, World!""/>
<Color name="Punctuation" foreground="Red" exampleText="string text = "Hello, World!""/>
<Color name="String" foreground="Olive" exampleText="string text = "Hello, World!""/>
<Color name="String2" foreground="#993" exampleText="string text = "Hello, World!""/>
<Color name="Keyword" fontWeight="bold" foreground="Blue" exampleText="SELECT"/>
<Color name="Keyword1" fontWeight="normal" foreground="Blue" exampleText="NOCOUNT"/>
<Color name="GoKeyword" fontWeight="bold" foreground="Red" exampleText="GO"/>
<Color name="MethodCall" foreground="MidnightBlue" fontWeight="bold" />
<Color name="Variable" foreground="Red" exampleText="@Variable" />
<Color name="Variable1" foreground="Red" exampleText="@@Variable" />
<Color name="ObjectReference" foreground="Teal" exampleText="Customer.Name" />
<Color name="ObjectReference1" foreground="Teal" exampleText="dbo.Customer.Name" />
<Color name="ObjectReferenceInBrackets" foreground="Teal" exampleText="[Customer].[Name]" />
<Color name="ObjectReferenceInBrackets1" foreground="Teal" exampleText="[dbo].[Customer].[Name]" />
<Color name="CommentMarkerSetTodo" foreground="Red" fontWeight="bold" />
<Color name="CommentMarkerSetHackUndone" foreground="#E0E000" fontWeight="bold" />
<RuleSet name="CommentMarkerSet">
<Keywords color="CommentMarkerSetTodo">
<Word>TODO</Word>
<Word>FIXME</Word>
</Keywords>
<Keywords color="CommentMarkerSetHackUndone">
<Word>HACK</Word>
<Word>UNDONE</Word>
</Keywords>
</RuleSet>
<RuleSet ignoreCase="true">
<Span color="String" multiline="true" >
<Begin>'</Begin>
<End>'</End>
</Span>
<Span color="String2" multiline="true" >
<Begin>"</Begin>
<End>"</End>
</Span>
<!-- span for escape sequences -->
<Span color="Comment" begin="--" end="\n" ruleSet="CommentMarkerSet"/>
<Span color="Comment" multiline="true" ruleSet="CommentMarkerSet">
<Begin>/\*</Begin>
<End>\*/</End>
</Span>
<Keywords color="Keyword" >
<Word>ABORT</Word>
<Word>BETWEEN</Word>
<Word>CRASH</Word>
<Word>DIGITS</Word>
<Word>ACCEPT</Word>
<Word>BINARY_INTEGER</Word>
<Word>CREATE</Word>
<Word>DISPOSE</Word>
<Word>ACCESS</Word>
<Word>BODY</Word>
<Word>CURRENT</Word>
<Word>DISTINCT</Word>
<Word>ADD</Word>
<Word>BOOLEAN</Word>
<Word>CURRVAL</Word>
<Word>DO</Word>
<Word>ALL</Word>
<Word>BY</Word>
<Word>CURSOR</Word>
<Word>DROP</Word>
<Word>ALTER</Word>
<Word>CASE</Word>
<Word>DATABASE</Word>
<Word>ELSE</Word>
<Word>AND</Word>
<Word>CHAR</Word>
<Word>DATA_BASE</Word>
<Word>ELSIF</Word>
<Word>ANY</Word>
<Word>CHAR_BASE</Word>
<Word>DATE</Word>
<Word>END</Word>
<Word>ARRAY</Word>
<Word>CHECK</Word>
<Word>DBA</Word>
<Word>ENTRY</Word>
<Word>ARRAYLEN</Word>
<Word>CLOSE</Word>
<Word>DEBUGOFF</Word>
<Word>EXCEPTION</Word>
<Word>AS</Word>
<Word>CLUSTER</Word>
<Word>DEBUGON</Word>
<Word>EXCEPTION_INIT</Word>
<Word>ASC</Word>
<Word>CLUSTERS</Word>
<Word>DECLARE</Word>
<Word>EXISTS</Word>
<Word>ASSERT</Word>
<Word>COLAUTH</Word>
<Word>DECIMAL</Word>
<Word>EXIT</Word>
<Word>ASSIGN</Word>
<Word>COLUMNS</Word>
<Word>DEFAULT</Word>
<Word>FALSE</Word>
<Word>AT</Word>
<Word>COMMIT</Word>
<Word>DEFINITION</Word>
<Word>FETCH</Word>
<Word>AUTHORIZATION</Word>
<Word>COMPRESS</Word>
<Word>DELAY</Word>
<Word>FLOAT</Word>
<Word>AVG</Word>
<Word>CONNECT</Word>
<Word>DELETE</Word>
<Word>FOR</Word>
<Word>BASE_TABLE</Word>
<Word>CONSTANT</Word>
<Word>DELTA</Word>
<Word>FORM</Word>
<Word>BEGIN</Word>
<Word>COUNT</Word>
<Word>DESC</Word>
<Word>FROM</Word>
<Word>FUNCTION</Word>
<Word>NEW</Word>
<Word>RELEASE</Word>
<Word>SUM</Word>
<Word>GENERIC</Word>
<Word>NEXTVAL</Word>
<Word>REMR</Word>
<Word>TABAUTH</Word>
<Word>GOTO</Word>
<Word>NOCOMPRESS</Word>
<Word>RENAME</Word>
<Word>TABLE</Word>
<Word>GRANT</Word>
<Word>NOT</Word>
<Word>RESOURCE</Word>
<Word>TABLES</Word>
<Word>GROUP</Word>
<Word>NULL</Word>
<Word>RETURN</Word>
<Word>TASK</Word>
<Word>HAVING</Word>
<Word>NUMBER</Word>
<Word>REVERSE</Word>
<Word>TERMINATE</Word>
<Word>IDENTIFIED</Word>
<Word>NUMBER_BASE</Word>
<Word>REVOKE</Word>
<Word>THEN</Word>
<Word>IF</Word>
<Word>OF</Word>
<Word>ROLLBACK</Word>
<Word>TO</Word>
<Word>IN</Word>
<Word>ON</Word>
<Word>ROWID</Word>
<Word>TRUE</Word>
<Word>INDEX</Word>
<Word>OPEN</Word>
<Word>ROWLABEL</Word>
<Word>TYPE</Word>
<Word>INDEXES</Word>
<Word>OPTION</Word>
<Word>ROWNUM</Word>
<Word>UNION</Word>
<Word>INDICATOR</Word>
<Word>OR</Word>
<Word>ROWTYPE</Word>
<Word>UNIQUE</Word>
<Word>INSERT</Word>
<Word>ORDER</Word>
<Word>RUN</Word>
<Word>UPDATE</Word>
<Word>INTEGER</Word>
<Word>OTHERS</Word>
<Word>SAVEPOINT</Word>
<Word>USE</Word>
<Word>INTERSECT</Word>
<Word>OUT</Word>
<Word>SCHEMA</Word>
<Word>VALUES</Word>
<Word>INTO</Word>
<Word>PACKAGE</Word>
<Word>SELECT</Word>
<Word>VARCHAR</Word>
<Word>IS</Word>
<Word>PARTITION</Word>
<Word>SEPARATE</Word>
<Word>VARCHAR2</Word>
<Word>LEVEL</Word>
<Word>PCTFREE</Word>
<Word>SET</Word>
<Word>VARIANCE</Word>
<Word>LIKE</Word>
<Word>POSITIVE</Word>
<Word>SIZE</Word>
<Word>VIEW</Word>
<Word>LIMITED</Word>
<Word>PRAGMA</Word>
<Word>SMALLINT</Word>
<Word>VIEWS</Word>
<Word>LOOP</Word>
<Word>PRIOR</Word>
<Word>SPACE</Word>
<Word>WHEN</Word>
<Word>MAX</Word>
<Word>PRIVATE</Word>
<Word>SQL</Word>
<Word>WHERE</Word>
<Word>MIN</Word>
<Word>PROCEDURE</Word>
<Word>SQLCODE</Word>
<Word>WHILE</Word>
<Word>MINUS</Word>
<Word>PUBLIC</Word>
<Word>SQLERRM</Word>
<Word>WITH</Word>
<Word>MLSLABEL</Word>
<Word>RAISE</Word>
<Word>START</Word>
<Word>WORK</Word>
<Word>MOD</Word>
<Word>RANGE</Word>
<Word>STATEMENT</Word>
<Word>XOR</Word>
<Word>MODE</Word>
<Word>REAL</Word>
<Word>STDDEV</Word>
<Word>NATURAL</Word>
<Word>RECORD</Word>
<Word>SUBTYPE</Word>
<Word>TRUNCATE</Word>
</Keywords>
<Keywords color="Keyword1">
<Word>NOCOUNT</Word>
</Keywords>
<Keywords color="GoKeyword" >
<Word>GO</Word>
</Keywords>
<Rule color="ObjectReference1">([\d\w]+)\.([\d\w]+)\.([\d\w]+)</Rule>
<Rule color="ObjectReference">([\d\w]+)\.([\d\w]+)</Rule>
<Rule color="ObjectReferenceInBrackets1">([\d\w]+)\.([\d\w]+)\.([\d\w]+)</Rule>
<Rule color="ObjectReferenceInBrackets">\[([\d\w]+)\]\.\[([\d\w]+)\]\.\[([\d\w]+)\]</Rule>
<Rule color="ObjectReferenceInBrackets">\[([\d\w]+)\]\.\[([\d\w]+)\]</Rule>
<Rule color="Punctuation">
[?,.;()\[\]{}+\-/%*<>^+~!|&]+
</Rule>
<Rule color="MethodCall">[\d\w_]+(?=(\s*\())</Rule>
<Rule color="Variable1">@@([\w]+)</Rule>
<Rule color="Variable">@([\w]+)</Rule>
<!-- Digits -->
<Rule color="Digits">
\b0[xX][0-9a-fA-F]+ # hex number
|
( \b\d+(\.[0-9]+)? #number with optional floating point
| \.[0-9]+ #or just starting with floating point
)
([eE][+-]?[0-9]+)? # optional exponent
</Rule>
</RuleSet>
</SyntaxDefinition>