Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

修復 Xamarin Mono for Android:讓 axml 編輯器恢復 XML 標籤自動提示

編輯:Android 開發實例

問題背景

這兩天用 Mono for Android 以 .NET 開發 Android,發現舊版開發工具在停止除錯時會卡上好幾十秒,還常常跳出錯誤對話框,非常影響心情。於是昨天換裝了當時最新的 Xamarin Mono For Android 4.6,卡頓和彈窗都消失了,順暢很多。

但很快遇到一個更麻煩的問題:axml 檔案在編輯器中完全沒有標籤自動提示,這對開發效率打擊很大。

先搞懂 Visual Studio 的自動提示原理

遇到問題要學會分析問題。Visual Studio 對 XML 檔案之所以能提供自動提示與自動完成,原理是這樣的:

VS 會讀取目前編輯的 XML 檔案所宣告的 schema,然後到安裝路徑下的 Xml\Schemas 資料夾中,找出與該 schema 對應的 xsd 檔。xsd 是對 XML 格式的標準約束,VS 有了這份約束,就能推導出可用的標籤與屬性。像 Web.config 這類檔案的自動提示,就是靠這個機制運作的。

動手排查

既然原理清楚了,就來驗證。打開 Xml\Schemas 資料夾,果然發現一個 monodroidcatalog.xml,檔名和 Mono for Android 明顯相關。內容如下:

<SchemaCatalog xmlns="https://schemas.microsoft.com/xsd/catalog">
  <Schema href="%ProgramFiles%/MSBuild/Novell/android-layout-xml.xsd" />
  <Schema href="%ProgramFiles%/MSBuild/Novell/schemas.android.com.apk.res.android.xsd"
          targetNamespace="https://schemas.android.com/apk/res/android" />

  <Association extension="axml"
               schema="%ProgramFiles%/MSBuild/Novell/android-layout-xml.xsd" />
</SchemaCatalog>

這個檔案的角色顯而易見:它是一個「指路者」,告訴 VS「xsd 檔在 %ProgramFiles%/MSBuild/Novell/ 底下」,並且把 .axml 副檔名關聯到 android-layout-xml.xsd。

但實際打開 %ProgramFiles%/MSBuild/Novell/,裡面根本沒有這些檔案。問題找到了。

解決辦法

在磁碟上搜尋這兩個檔名,會在 Xamarin Studio 的附加元件目錄下找到它們:

C:\Program Files\Xamarin Studio\AddIns\MonoDevelop.MonoDroid\schemas\

裡面正好有 android-layout-xml.xsd 與 schemas.android.com.apk.res.android.xsd。

把這兩個檔案複製到 catalog 檔所指向的位置:

%ProgramFiles%\MSBuild\Novell\

完成後重啟 Visual Studio,再打開任何 axml 檔,標籤與屬性的自動提示就回來了。

小結

這個問題的本質是安裝程式沒有把 schema 檔放到 catalog 所宣告的路徑,屬於安裝缺陷而非設定錯誤。遇到 IDE 智慧提示失效時,先找出它依賴的 schema 目錄,再確認檔案是否真的存在,通常都能很快定位。

轉自:cnblogs.com/rupeng

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved