把NetDimension.NanUI项目从C#6.0语法还原到C#5.0

前言

找Cef资料时看到一个比较好的封装NanUI for Winform发布,让Winform界面设计拥有无限可能,下载代码后发现是Vs2015+C#6.0开发的,本机没有VS2015也不想安装。于是想给vs2013装个插件支持C#6.0的语法,http://stackoverflow.com/questions/27093908/how-to-enable-c-sharp-6-0-feature-in-visual-studio-2013 这里有个最佳答案如下图,最后想还是手动改一下代码把C#6.0的语法去掉。:

VS2013不支持的语法

新建一个vs2013的空白解决方案,添加现有项目NetDimension.NanUI,生成出现如下几类错误:

语法错误,应输入“,”
意外的字符“$”
无效的表达式项“[”

?.表达式

类、结构或接口成员声明中的标记“=”无效

 

首先看一下C#6.0新增了哪些新的特性C# 6.0可能的新特性及C#发展历程、https://msdn.microsoft.com/en-us/magazine/dn802602.aspx,然后逐个改造。

语法错误,应输入“,” 

原有代码,用到的特性是:

    Dictionary<string, string> requirements = new Dictionary<string, string>()
        {
            ["资源文件"] = "resources.exe"
        };

新代码

    Dictionary<string, string> requirements = new Dictionary<string, string>()
        {
            {"资源文件", "resources.exe"}
        };

 

意外的字符“$”

原有代码:

        if (localPath.StartsWith("/"))
                localPath = $".{localPath}";

新代码:

            if (localPath.StartsWith("/"))
                localPath = string.Format(".{0}", localPath);

 

类、结构或接口成员声明中的标记“=”无效

老代码

        [Category("NanUI")]
        public bool NonclientModeDropShadow
        {
            get; set;
        } = true;

新代码,在类的构造函数中赋值

        this.NonclientModeDropShadow = true;

 

?.表达式

老代码:

    protected override void OnClosed(EventArgs e)
        {
            messageInterceptor?.ReleaseHandle();
            messageInterceptor?.DestroyHandle();
            messageInterceptor = null;


            base.OnClosed(e);

            nativeForm?.ReleaseHandle();
            nativeForm?.DestroyHandle();


        }

新代码:

        protected override void OnClosed(EventArgs e)
        {
          
            if (messageInterceptor != null)
            {
                messageInterceptor.ReleaseHandle();
                messageInterceptor.DestroyHandle();
            }
            messageInterceptor = null;

            base.OnClosed(e);

            if (nativeForm != null)
            {
                nativeForm.ReleaseHandle();
                nativeForm.DestroyHandle();
            }
        }

总结

C#6.0确实提供了一些新的特性,值得去体验,这里有个vs2015各版本下载的分享地址: http://pan.baidu.com/s/1eQk3s8i 想用的可以去下载。

posted @ 2016-10-26 10:09  Zeroes  阅读(2979)  评论(0编辑  收藏  举报