如何實現 WPF 代碼查看器控件

?如何實現 WPF 代碼查看器控件

CodeViewer

作者:WPFDevelopersOrg - 驚鏵

原文鏈接[1]:https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用.NET40

  • Visual Studio 2019;

  • 代碼展示需要使用到AvalonEdit是基于WPF的代碼顯示控件,項目地址[2],支持C#javascript,C++,XML,HTML,Java等語言的關鍵字高亮顯示。

  • AvalonEdit也是支持自定義的高亮配置,對于需要編寫腳本編輯器的場景非常適用。

  • 可通過配置CustomHighlighting.xshd文件,可以對高亮顯示做自定義設置。

  • 以下能夠實現ifelse高亮格式設置,代碼如下:

<?xml?version="1.0"?>
<SyntaxDefinition?name="Custom?Highlighting"?xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008"><RuleSet><Keywords?fontWeight="bold"?foreground="Blue"><Word>if</Word><Word>else</Word></Keywords></RuleSet>
</SyntaxDefinition>

1)新建 SourceCodeModel.cs用作記錄代碼源碼地址源碼類型等。

namespace?WPFDevelopers.Samples.Controls
{public?class?SourceCodeModel{public?CodeType?CodeType?{?get;?set;?}public?string?Haader?{?get;?set;?}public?string?CodeSource?{?get;?set;?}}public?enum?CodeType{Xaml,CSharp,}
}

2)新建 CodeViewer.xaml 代碼如下:

<ResourceDictionary?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"><Style?TargetType="{x:Type?controls:CodeViewer}"><Setter?Property="FontSize"?Value="{StaticResource?NormalFontSize}"/><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?controls:CodeViewer}"><TabControl?x:Name="PART_TabControl"><TabControl.Resources><Style?TargetType="TabPanel"><Setter?Property="HorizontalAlignment"?Value="Right"/></Style></TabControl.Resources><TabItem?x:Name="PART_TabItemContent"?Header="Sample"?Content="{TemplateBinding?Content}"/></TabControl><ControlTemplate.Triggers><Trigger?Property="Content"?Value="{x:Null}"><Setter?Property="Visibility"?TargetName="PART_TabItemContent"?Value="Collapsed"/><Setter?Property="SelectedIndex"?TargetName="PART_TabControl"?Value="1"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>
</ResourceDictionary>

3)新建 CodeViewer.cs 繼承ContentControl 代碼如下:

  • Content用來展示控件。

  • 增加公共集合屬性用做存放代碼信息SourceCodes,重寫控件時循環SourceCodes增加TabItemPART_TabControl中。

using?ICSharpCode.AvalonEdit;
using?ICSharpCode.AvalonEdit.Editing;
using?ICSharpCode.AvalonEdit.Highlighting;
using?System;
using?System.Collections.ObjectModel;
using?System.IO;
using?System.Windows;
using?System.Windows.Controls;
using?static?System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;namespace?WPFDevelopers.Samples.Controls
{[TemplatePart(Name?=?TabControlTemplateName,?Type?=?typeof(TabControl))]public?class?CodeViewer?:?ContentControl{private?static?readonly?Type?_typeofSelf?=?typeof(CodeViewer);public?ObservableCollection<SourceCodeModel>?SourceCodes?{?get;?}?=?new?ObservableCollection<SourceCodeModel>();private?const?string?TabControlTemplateName?=?"PART_TabControl";private?TabControl?_tabControl?=?null;static?CodeViewer(){DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,new?FrameworkPropertyMetadata(_typeofSelf));}public?override?void?OnApplyTemplate(){base.OnApplyTemplate();_tabControl?=?GetTemplateChild(TabControlTemplateName)?as?TabControl;foreach?(var?item?in?SourceCodes){var?tabItem?=?CreateTabItem(item);_tabControl.Items.Add(tabItem);}}TabItem?CreateTabItem(SourceCodeModel?codeModel){if(codeModel==?null)return?null;var?partTextEditor?=?new?TextEditor();partTextEditor.Options?=?new?TextEditorOptions?{?ConvertTabsToSpaces?=?true?};partTextEditor.TextArea.SelectionCornerRadius?=?0;partTextEditor.SetResourceReference(TextArea.SelectionBrushProperty,?"WindowBorderBrushSolidColorBrush");partTextEditor.TextArea.SelectionBorder?=?null;partTextEditor.TextArea.SelectionForeground?=?null;partTextEditor.IsReadOnly?=?false;partTextEditor.ShowLineNumbers?=?true;partTextEditor.FontFamily?=?DrawingContextHelper.FontFamily;partTextEditor.Text?=?GetCodeText(codeModel.CodeSource);var?tabItem?=?new?TabItem{Content?=?partTextEditor};switch?(codeModel.CodeType){case?CodeType.Xaml:partTextEditor.SyntaxHighlighting?=?HighlightingManager.Instance.GetDefinitionByExtension(".XML");tabItem.Header?=?codeModel.Haader?==?null???"Xaml"?:?codeModel.Haader;break;case?CodeType.CSharp:partTextEditor.SyntaxHighlighting?=?HighlightingManager.Instance.GetDefinitionByExtension(".CS");tabItem.Header?=?codeModel.Haader?==?null???"CSharp"?:?codeModel.Haader;break;}return?tabItem;}string?GetCodeText(string?codeSource){var?code?=?string.Empty;var?uri?=?new?Uri(codeSource,?UriKind.Relative);var?resourceStream?=?Application.GetResourceStream(uri);if?(resourceStream?!=?null){var?streamReader?=?new?StreamReader(resourceStream.Stream);code?=?streamReader.ReadToEnd();return?code;}return?code;}}
}

4)新建 WPFDevelopers.SamplesCode.csproj 項目,在VS右鍵項目添加現有項目將所需要讀取的代碼文件添加為鏈接就能得到以下地址:

<Resource?Include="..\WPFDevelopers.Samples\ExampleViews\AnimationNavigationBar3DExample.xaml"><Link>ExampleViews\AnimationNavigationBar3DExample.xaml</Link>
</Resource>

4)修改Example 代碼如下:

<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.AnimationNavigationBar3DExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"mc:Ignorable="d"?d:DesignHeight="450"?d:DesignWidth="800"><controls:CodeViewer><!--此處放展示控件--><controls:CodeViewer.SourceCodes><controls:SourceCodeModel?CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/AnimationNavigationBar3DExample.xaml"?CodeType="Xaml"/><controls:SourceCodeModel?CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/AnimationNavigationBar3DExample.xaml.cs"?CodeType="CSharp"/></controls:CodeViewer.SourceCodes></controls:CodeViewer>
</UserControl>
d7aa6c89e0806dbb80313c34ebb1ab24.gif

參考資料

[1]

原文鏈接: https://github.com/WPFDevelopersOrg/WPFDevelopers

[2]

地址: https://github.com/icsharpcode/AvalonEdit

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280967.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280967.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280967.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

談大數據也談人工智能 郭為告訴你一個不一樣的神州控股

毋庸置疑&#xff0c;我們深處一個數據無處不在的時代&#xff0c;也就是大數據時代。作為中國智慧城市領導者的神州數碼控股有限公司&#xff08;以下簡稱“神州控股”&#xff09;近年來也在積極布局大數據&#xff0c;不過在神州控股董事局主席郭為看來&#xff0c;神州控股…

Django02: pycharm上配置django

1.setting導入 File-->Setting-->Project-->Project Interface 2.new project 新窗口 圖片畫錯 3.調試 點擊右上角調試

vue-cli中配置sass

https://www.cnblogs.com/rainheader/p/6505366.html轉載于:https://www.cnblogs.com/aidixie/p/10213997.html

php 常用函數

用到就記下來&#xff0c;持續更新......... __call(string $func_name, array args){}public方法不存在 調用此函數 通過pg_系列函數與Postgres 數據庫交互 note: php 取得對象的某一共有屬性&#xff0c;若不存在則 查看是否有get方法(魔術方法) 若有則取get方法的返回值&am…

dropbox_來自提示框:望遠鏡激光瞄準器,Dropbox桌面和Kindle剪輯轉換

dropboxOnce a week we round up some great reader tips and share them with everyone; this week we’re looking at telescope laser sights, syncing your desktop with Dropbox, and converting your Kindle Clippings file. 每周一次&#xff0c;我們收集一些很棒的讀者…

在 EF Core 7 中實現強類型 ID

本文主要介紹 DDD 中的強類型 ID 的概念&#xff0c;及其在 EF 7 中的實現&#xff0c;以及使用 LessCode.EFCore.StronglyTypedId 這種更簡易的上手方式。背景在楊中科老師 B 站的.Net Core 視頻教程[1]其中 DDD 部分講到了強類型 ID&#xff08;Strongly-typed-id&#xff09…

如何快速打造一款高清又極速的短視頻APP?

2019獨角獸企業重金招聘Python工程師標準>>> 整個短視頻的市場規模一直在增長&#xff0c;網絡數據顯示2018年已經突破100億大關&#xff0c;在2019年預測將超過200億。縱觀行業&#xff0c;在生活資訊、美食、搞笑、游戲、美妝等領域&#xff0c;短視頻流量巨大但競…

Django03: django加入APP

使用命令在已有project創建 1.創建 在manage.py同級運行命令 python manage.py startapp app01 2.django中加入app 在settings.py里的INSTALLED_APPS加入app01.apps.App01Config, INSTALLED_APPS [django.contrib.admin,django.contrib.auth,django.contrib.contenttype…

[面經]春季跳槽面筋總結 [2018年3月17]

春季跳槽面筋總結 人人都說金三銀四&#xff0c;由于一些個人的原因&#xff0c;博主也在今年的三月份抽空面了幾家公司&#xff0c;這里來總結下學習到的東西。 先簡單的說下博主的情況&#xff1a; 2015年7月份畢業&#xff0c;到現在加上實習可以算三年工作經驗base武漢&…

如何將Windows 10帳戶還原為本地帳戶(在Windows Store劫持它之后)

If your Windows 10 user account is currently a Microsoft account (by your choice or because you got, one way or another, roped into it) it’s easy to revert it back to a local account if you know where to look. Read on as we show you how. 如果您的Windows 1…

【譯】Dapr 是一個“10倍好”平臺 !?

譯者注在正式閱讀本文之前&#xff0c;我們有必要先了解下什么是“10 倍好”。10 倍好理論最早出自彼得蒂爾的《從 0 到 1》&#xff0c;他說一個新創企業&#xff0c;要想獲得快速成長&#xff0c;其提供的解決方案要比現有方案好 10 倍以上&#xff0c;這個好 10 倍&#xff…

04.jQuery 基本語法筆記

jQuery是什么 jQuery是一個輕量級的、兼容多瀏覽器的JavaScript庫。jQuery使用戶能夠更方便地處理HTML Document、Events、實現動畫效果、方便地進行Ajax交互&#xff0c;能夠極大地簡化JavaScript編程。它的宗旨就是&#xff1a;“Write less, do more.“ jQuery引入到HTML …

1. ReactJS基礎(開發環境搭建)

本文主要介紹通過React官方提供的create-react-app腳手架進行開發環境的搭建。 1.安裝node環境(安裝過程這里不做介紹&#xff0c;可參考其他博文) 在cmd中輸入node -v 如果可以看到相應版本號&#xff0c;說明node環境安裝成功 2.npm全局安裝create-react-app腳手架 3.cmd命令…

軟件工程(2018)第一次作業

(1) 回顧你過去將近3年的學習經歷 當初你報考的時候&#xff0c;是真正喜歡計算機這個專業嗎&#xff1f; 在高中的時候&#xff0c;我們就開設了微機課&#xff0c;當時上課的內容不僅有Microsoft word,excel,powerpoint的使用&#xff0c;還有編程的基本入門&#xff0c;當時…

“云計算+DevOps”的正確打開方式

以我們的經驗看&#xff0c;技術和工具是很重要&#xff0c;但是技術和工具本身卻不能產生價值&#xff0c;而將DevOps和云計算結合卻可以。事實上&#xff0c;云計算的特性決定了&#xff0c;云計算和DevOps勢必如影隨形&#xff0c;而云計算與DevOps的結合也正在為企業用戶提…

微服務和分布式系統中的授權解決方案

本文是 《精讀 Mastering ABP Framework》 2.3 探索橫切關注點 - 使用授權和權限系統 一節的擴充內容&#xff0c;重點探討了授權在分布式和微服務系統中遇到的挑戰&#xff0c;以及 ABP Framework 中采用的解決方案。認證 & 授權? 認證&#xff08;Authentication&#x…

pat 團體天梯賽 L2-012. 關于堆的判斷

L2-012. 關于堆的判斷 時間限制400 ms內存限制65536 kB代碼長度限制8000 B判題程序Standard作者陳越將一系列給定數字順序插入一個初始為空的小頂堆H[]。隨后判斷一系列相關命題是否為真。命題分下列幾種&#xff1a; “x is the root”&#xff1a;x是根結點&#xff1b;“x a…

04-1.jQuery事件與補充each/data

目錄 事件 事件綁定 常用事件 阻止后續事件執行 補充 each .data() 事件 事件綁定 .on( events [, selector ],function(){}) events&#xff1a; 事件selector: 選擇器&#xff08;可選的&#xff09;function: 事件處理函數 普通綁定&#xff0c;沒有選擇器&#x…

【刷出存在感】鋒會圓桌現場

【編者按】本文為鋒會|路由器專場的圓桌全文&#xff08;有刪減&#xff09;。 圓桌嘉賓&#xff1a;&#xff08;自左向右依次&#xff09; 極路由聯合創始人 丁衣 知道創宇研究部總監&#xff08;安全專家&#xff09; 余弦 WRTnode創始人&#xff08;開源硬件領域&#xff0…

如何從命令行瀏覽和連接到無線網絡

() We are always on the lookout for geeky ways to impress our friends, and recently we came across a way to connect to our wireless network from the command prompt, so today we’ll show you how to do it as well. 我們一直在尋找令人印象深刻的方式來打動我們的…