Windows phone應用開發[17]-xap提交異常處理

在windows phone 應用提交操做上早在2011年時就寫過一篇Windows phone 應用開發[4]-應用發佈,那時wp應用提交官方市場的流程繁雜[超過了5步].由於上傳和填寫應用信息頁面採用silverlight技術作的.加載速度有些慢再加上操做用戶體驗很差.致使不少開發者都在提交過程很是痛苦.在wp 7.8 更新發布後. ms官方也逐漸爲了使提交應用更簡單.在應用提交流程上作了很大的簡化.其中最關鍵當屬徹底去掉silverlight頁面.只在上傳xap包時保留了. 流程精簡爲兩步[應用信息和xap包提交]. 另外還增長每一個步驟單獨保存操做.這樣即便由於中間網絡或是其餘緣由失敗.也能夠保存應用信息 無需重複輸入.css

而最近從朋友那拿到一個windows phone 開發者帳號.因此作了一個簡單的測試語音的應用.在提交時碰到從未碰到的異常.註明如今新版應用提交過程會驗證xap應用信息. 若是存在驗證異常則沒法提交應用 異常信息超過45條以下:html

2011: The background agent can’t use Microsoft.Phone.Shell.ShellTile::Create, which assembly TileAgent.dll is trying to use. Update your file and then try again.shell

2011: The background agent can’t use Coding4Fun.Toolkit.Controls, which assemblyCoding4Fun.Toolkit.Controls.dll is trying to use. Update your file and then try again.windows

2011: The background agent can’t use System.Windows.Controls.MediaElement::Stop, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.api

2011: The background agent can’t use System.Windows.Controls.MediaElement::Play, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.網絡

2011: The background agent can’t use System.Windows.Controls.MediaElement::set_Position, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.app

2011: The background agent can’t use System.Windows.Controls.MediaElement::set_AutoPlay, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.ide

2011: The background agent can’t use System.Windows.Controls.MediaElement, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.測試

2011: The background agent can’t use System.Windows.Controls.MediaElement::SetSource, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.spa

2011: The background agent can’t use System.Windows.Media.VideoBrush, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.

2011: The background agent can’t use System.Windows.Controls.MediaElement::.ctor, which assembly Microsoft.Phone.dll is trying to use. Update your file and then try again.

能夠看到主要由於後臺任務background agent 中調用不收支持的API致使的.在windows phone 後臺任務ms官方規定全部可能涉及UI的操做api都是禁止調用的.官方在windows phone後臺代理概述中提到後臺任務中限制的api列表windows phone 後臺代理不支持的api.可見System.Windows.Controls命名空間下MediaElement也是不支持的.而官方對這個錯誤表示代碼2011 能夠在瞭解應用提交錯誤中找到對應的說明:

2011

後臺代理沒法使用程序集 [assembly name] 正嘗試使用的 [resource]。請更新文件,而後重試。

知道大概緣由再來看看具體的實際代碼.首先看看解決方案項目結構.:

2013-07-03_105642

項目結構說明,VoiceControl使用啓動項目.設計UI. Common則用來封裝一些通用的操做或第三方可複用的類庫.TaskAgent就是後臺任務用來定時請求雲端語音識別資源而後更新控制播放並更新livetitle. 整個項目結構很簡單.項目間引用關係以下:

2013-07-03_111354

VoiceControl做爲UI項目分別引用了Common類庫和後臺任務TaskAgent. 然後臺任務TaskAgent項目則引用Common類庫.既然異常提示說咱們後臺任務TaskAgent項目使用限制的APi.後來我review分析後臺任務中操做的代碼.後臺代碼中根本沒有使用任何涉及UI操做的api.後臺任務其實就是作了兩件事:

A:請求雲端語音識別數據並更新Lock Screen識別出來的內容.

B:更新首屏live titles磁鐵

在來看看TaskAgent引用:

2013-07-03_113038

引用第三方分別Json.net序列化操做的. RestSharp則用來封裝windows phone 全部後臺數據請求. 都沒有涉及任何UI操做.因此這兩個第三方引用庫被排除.問題惟一可能出在及時Common類庫中.在來看看Common類庫的引用關係:

2013-07-03_113717

在Common中採用第三方類庫Coding4Fun中ToastPrompt控件實現相似Toast頂部通知提示效果.而這部分是涉及UI的操做的.放在Common類庫中目的是爲了保證這部分代碼隨着項目遷移能夠複用.但個人後臺任務並無調用該代碼.卻很奇怪的提示Coding4Fun.Tookit.Control嘗試去使用.這是爲什麼?

見過一番測試發現.當在Common類庫中除掉Coding4Fun.Tookit.Control應用全部關於後臺引用異常提示都消失了.好吧換一種說法就明白了:

2013-07-03_114757

當Common類庫中引用涉及UI操做的第三方庫Coding4Fun.Toolkit.Controls.同時後臺任務引用Common類庫.即便後臺TaskAgent沒有調用Common涉及UI操做的代碼.只要TaskAgent引用了Common類庫.若是Common類庫中包含涉及任何可能UI操做封裝的代碼或是第三方可能會操做UI的引用關係. 那麼TaskAgent就會認爲你可能會在後臺操做UI.致使提交代碼時提示如上異常.這種作法的目的是從引用關係上就直接杜絕使用可能在後臺操做UI操做的可能.

知道具體的緣由.ok.fix it up.

你忽然發現不少Common類庫代碼已經VoiceControl中被大量引用了.若是要刪除Common操做UI的代碼.致使不少UI項目VoiceControl引用都須要手工替換.都快要上線了.這樣大幅的修改.不免會出現一些不可預測的Bug.好吧對於這樣手工活我向來都是抵制的.因此嘗試從引用關係上解決這個問題 .

在使用後臺項目中.咱們在選擇封裝時.最好的選擇是把類庫和涉及UI的操做各自獨立成一個項目.而至於剛纔多出引用替換. 咱們能夠不修改類和方法的命名.從新創建一個Components項目.把全部涉及UI操做的封裝都放到該項目.而後再VoiceControl UI 項目上只須要添加一個引用.而無需修改實際代碼. 這樣減小可能出現Bug可能.這樣一來項目結構和引用關係又發生變化:

2013-07-03_120401

這時在提交Xap安裝包.全部涉及Coding4Fun和MediaElement異常就所有消失了.惟獨剩下一個:

2011: The background agent can’t use Microsoft.Phone.Shell.ShellTile::Create, which assembly TileAgent.dll is trying to use. Update your file and then try again.

well.可見在後臺任務TaskAgent建立磁貼的操做也是不容許的.其實一樣個人後臺任務只是負責更新磁貼而並未建立.緣由仍是同樣咱們引用的Common類庫中設計磁貼Create操做.從而致使後臺任何可能try to use. 官方對於這點說明很明確:

ShellTile 類的 Update(ShellTileData) 方法

ShellTile 類的 Delete()()()() 方法

ShellTile 類的 ActiveTiles 屬性

這些方法能夠用於修改正在運行的後臺代理中的 shell 磁貼。請注意,不能在後臺代理中建立[Create方法] shell 磁貼

找到Common類庫設計封裝live Title的類可見建立Title方法以下:

   1:      public void CreateFilpLiveTitle(string navigationUrl,string title,string backTitle,string backContent,string wideBackContent,
   2:             string smallBackgroundImage,string backgroundImage,string wideBackgroundImage, string backBackgroundImage, 
   3:             string wideBackBackgroundImage)
   4:          {
   5:              if (!CheckLiveTitleExistByUrl(navigationUrl))
   6:              {
   7:                  Uri titleUri = new Uri(navigationUrl, UriKind.RelativeOrAbsolute);
   8:                  ShellTileData titleData = CreateFilpTitleData(title, backTitle, backContent, wideBackContent, smallBackgroundImage, backgroundImage,
   9:                      wideBackgroundImage, backBackgroundImage, wideBackBackgroundImage);                
  10:                  ShellTile.Create(titleUri, titleData, true);
  11:              }
  12:          }

在一開始修改這個異常時,我把這部分代碼註釋掉後用來測試.發現依然沒法經過.xap包在上傳過程當中會分析代碼.一樣把這部分代碼的操做放到Components項目中進行統一.而後再VoiceControl項目只需添加引用便可而無需修改實際的代碼.

在上傳Xap安裝包發現驗證經過.沒有提示任何異常.

可見在涉及後臺任務的項目中.類庫和UI操做可複用封裝都須要各自獨立成單獨的項目. 而問題是全部這些提示後臺可能try to use 操做UI的API在實際開發和調試過程都不會有任何提示.因此要保證後臺任務引用足夠的Clean並可複用.在發包上線時儘可能不要手動修改實際代碼.能夠嘗試從引用關係解決關係依賴.代碼就不貼了.

Contact me[@chenkaihome]

參考與引用:

windows phone background agent microsoft.phone.dll submission issue

why i got the background agent can’t use scheduledactionService::Add while sumitting my app?

Error 2011 the backgoud agent can’t use microsoft.phone.shell.applicationbarStateChangedEventArgs

相關文章
相關標籤/搜索