![](/img/trans.png)
[英]Saving an image from the web to the Saved Pictures folder in Windows Phone 8.1 RT C#
[英]Image source changing in C# for windows phone 8.1 RT
我想在運行時使用C#更改圖像源。 我試過了
在MainPage.xaml中,
<Image x:Name="myImage" HorizontalAlignment="Left"
Height="125"
Margin="86,76,0,0"
VerticalAlignment="Top"
Width="220" />
<Button Content="Button"
HorizontalAlignment="Left"
Margin="134,230,0,0"
VerticalAlignment="Top"
Click="Button_Click"/>
並在MainPage.xaml.cs中
private void Button_Click(object sender, RoutedEventArgs e)
{
myImage.Source = new BitmapImage(new Uri("/Assets/WorldCupFlags/Sri_Lanka.png", UriKind.Relative));
}
它沒有顯示編譯時錯誤,但是當我運行它並單擊Button時,它顯示了異常。 它說:“ mscorlib.ni.dll中發生了'System.ArgumentException'類型的異常,但未在用戶代碼中處理。”
提示是例外:
給定的System.Uri無法轉換為Windows.Foundation.Uri
您需要為Universal XAML應用程序使用絕對URI:
myImage.Source = new BitmapImage(new Uri(
"ms-appx:///Assets/WorldCupFlags/Sri_Lanka.png", UriKind.Absolute));
在異步代碼塊中,執行以下操作:
imgMyImageControl.Source = await GetBitmapAsFile("Folder\\ImageFileName.png");
GetBimpageAsFile函數:
/// <summary>
/// Gets a bitmap image stored on the local file system
/// </summary>
/// <param name="strIFile">The input file path name</param>
/// <returns>The requested bitmap image, if successful; else, null</returns>
public static async Task<BitmapImage> GetBitmapAsFile(string strIFile)
{
try
{
StorageFile fOut = null;
BitmapImage biOut = null;
FileRandomAccessStream fasGet = null;
if (!strIFile.Equals(""))
{
fOut = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(strIFile);
if (fOut != null)
{
fasGet = (FileRandomAccessStream)await fOut.OpenAsync(FileAccessMode.Read);
if (fasGet != null)
{
biOut = new BitmapImage();
if (biOut != null)
{
await biOut.SetSourceAsync(fasGet);
return biOut;
}
else
YourApp.App.ShowMessage(true, "Error", "GetBitmapAsFile", "Bitmap [" + strIFile + "] is not set.");
}
else
YourApp.App.ShowMessage(true, "Error", "GetBitmapAsFile", "File stream [" + strIFile + "] is not set.");
}
}
else
YourApp.App.ShowMessage(true, "Error", "GetBitmapAsFile", "Input file path name is empty.");
}
catch (Exception ex)
{
YourApp.App.ShowMessage(true, "Error", "GetBitmapAsFile", "[" + strIFile + "] " + ex.Message);
}
return null;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.