UAP開發錯誤之The given System.Uri cannot be converted into a Windows.Foundation.Uri(windows phone背景更換)

來源:互聯網
上載者:User

標籤:

今天博主在開發一款windows phone應用時,希望實現app背景的更換,思路很簡單。使用ApplicationDataContainer容器儲存我的圖片路徑,每次載入應用時讀取這個路徑以決定我用什麼背景,然後在更換背景時改變這個容器的值就好了。相關代碼如下:

       ApplicationDataContainer localsetting = ApplicationData.Current.LocalSettings;

            localsetting.Values["Background"] = "ms-appx:///Assets/Images/background2.jpg";       Button btn = sender as Button; if(string.Equals(btn.Tag,"1")) { localsetting.Values["Background"] = "ms-appx:///Assets/Images/background1.jpg"; } else if(string.Equals(btn.Tag,"2")) { localsetting.Values["Background"] = "ms-appx:///Assets/Images/background2.jpg"; } else { localsetting.Values["Background"] = "ms-appx:///Assets/Images/background3.jpg"; }

在載入應用時,把Grid的背景用容器路徑所對應的圖片刷一下,其中的ContentPanel是一個Grid:

       if (!localsetting.Values["Background"].Equals(null))            {                string strImageUri = localsetting.Values["Background"].ToString();                if (!string.IsNullOrEmpty(strImageUri))                {                    ImageBrush imageBrush = new ImageBrush();                    imageBrush.ImageSource = new BitmapImage(new Uri(strImageUri, UriKind.RelativeOrAbsolute));                    ContentPanel.Background = imageBrush;                }            }

值得注意的是:我的圖片放在工程中下面的Assets檔案夾下面的Image檔案夾中,一開始我容器中存放的值是類似這樣的:

 localsetting.Values["Background"] = "Assets/Images/background2.jpg";

然後就會報錯:The given System.Uri cannot be converted into a Windows.Foundation.Uri,現在的Uri定義好像已經修改,具體參考:https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.foundation.uri(v=vs.85).aspx

我的解決方案就是在路徑前面加個ms-appx:///就行了,注意,這是個絕對路徑,UriKind應該設定為UriKind.RelativeOrAbsolute或者UriKind.Absolute

 

UAP開發錯誤之The given System.Uri cannot be converted into a Windows.Foundation.Uri(windows phone背景更換)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.