WPF使用avalonEdit进行C#代码编辑

WPF使用avalonEdit进行C#代码编辑

在这里插入图片描述

<Window x:Class="工业视觉检测系统.Views.WorkFlowEditView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:工业视觉检测系统.Views"
         xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
        mc:Ignorable="d"
        Title="WorkFlowEditView" Height="450" Width="800">
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition  Height="7*"  />
            <RowDefinition Height="2*"  />
            
            
            
        </Grid.RowDefinitions>

        <!--编译调试-->

        <Button Grid.Row="0" x:Name="btn_Complie" Content="编译调试" Click="btn_Complie_Click" ></Button>

        
        <!--代码编辑-->
        <avalonEdit:TextEditor
                   Grid.Row="1" 
                Name="CodeEditor"
                SyntaxHighlighting="C#"
                FontFamily="Consolas"
                FontSize="10pt" 
                LineNumbersForeground="Black" 
                ShowLineNumbers="True">



        </avalonEdit:TextEditor>

        
        <!--信息显示-->
        <RichTextBox  Grid.Row="2"  x:Name="RTX_Message" Height="100" >
            <FlowDocument>
                <Paragraph>
                    <Run Text="信息打印平台:"/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>

    </Grid>
</Window>

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace 工业视觉检测系统.Views
{
    /// <summary>
    /// WorkFlowEditView.xaml 的交互逻辑
    /// </summary>
    public partial class WorkFlowEditView : Window
    {
        public WorkFlowEditView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 编译
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Complie_Click(object sender, RoutedEventArgs e)
        {
            // 获取代码内容
            string text = CodeEditor.Document.Text; //new TextRange(RTX_Edit.Document.ContentStart, RTX_Edit.Document.ContentEnd).Text;

            // 进行编译
            string[] allDlls = Directory.GetFiles("./").Where(item=>Path.GetExtension(item).Equals(".dll")).ToArray();

            CodeCompiler.Compile(allDlls , text, "");

            // 获取编译结果
           foreach (string s in CodeCompiler.ErrorMessage)
           {


                RTX_Message.Dispatcher.Invoke(new Action(() => { RTX_Message.AppendText(s + "\r\n"); }));

            }

            RTX_Message.Dispatcher.Invoke(new Action(() => { RTX_Message.AppendText(CodeCompiler.Message + "\r\n"); }));
 
        }


        /// <summary>
        /// 代码编译
        /// </summary>
        static class CodeCompiler
        {
            // 信息
            static public string Message;
            // 报错信息
            static public List<string> ErrorMessage = new List<string>();

            /// <summary>
            /// 编译
            /// </summary>
            /// <param name="references"></param>
            /// <param name="source"></param>
            /// <param name="outputfile"></param>
            /// <returns></returns>
            public static bool Compile(string[] references, string source, string outputfile)
            {
                // 编译参数  
                CompilerParameters param = new CompilerParameters(references, outputfile, true);

                param.ReferencedAssemblies.Add(@"工业视觉检测系统.exe");

                param.TreatWarningsAsErrors = false;
                // 获取或设置一个值,该值指示是否生成可执行文件。若此属性为false,则生成DLL,默认是false。
                param.GenerateExecutable = false;
                // 
                param.IncludeDebugInformation = true;

                //  提供对C#代码生成器和代码编译器的实例的访问 
                CSharpCodeProvider provider = new CSharpCodeProvider();

                // 表示从编译器返回的编译结果。
                CompilerResults result = provider.CompileAssemblyFromSource(param, new string[] { source });

                Message = "";
                ErrorMessage.Clear();


                /*
                 *反射调用
                 */
                if (!result.Errors.HasErrors)
                {

                    Type t = result.CompiledAssembly.GetType("MyClass");


                    if (t != null)
                    {
                        object o = result.CompiledAssembly.CreateInstance("MyClass");
                        Message = (string)t.InvokeMember("GetResult", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, o, null);
                    }

                    return true;
                }

                /*
                 *出编译错误  
                 */
                foreach (CompilerError error in result.Errors)
                {
                    if (error.IsWarning) continue;
                    ErrorMessage.Add("Error(" + error.ErrorNumber + ") - " + error.ErrorText + "\t\tLine:" + error.Line.ToString() + "  Column:" + error.Column.ToString());
                }
                return false;
            }

        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值