把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 想用的可以去下載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM