简体   繁体   English

Windows Phone 8.1 RT在C#中更改图像源

[英]Image source changing in C# for windows phone 8.1 RT

I want to change image source in runtime using C#. 我想在运行时使用C#更改图像源。 I tried this. 我试过了

In the MainPage.xaml, 在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"/>

and in the MainPage.xaml.cs 并在MainPage.xaml.cs中

private void Button_Click(object sender, RoutedEventArgs e)
        {
            myImage.Source = new BitmapImage(new Uri("/Assets/WorldCupFlags/Sri_Lanka.png", UriKind.Relative));
        }

It shows no compile time error but when I run this and click the Button it shows an exception. 它没有显示编译时错误,但是当我运行它并单击Button时,它显示了异常。 It says "An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code." 它说:“ mscorlib.ni.dll中发生了'System.ArgumentException'类型的异常,但未在用户代码中处理。”

The hint is in the exception: 提示是例外:

The given System.Uri cannot be converted into a Windows.Foundation.Uri 给定的System.Uri无法转换为Windows.Foundation.Uri

You need to use an absolute URI for Universal XAML apps: 您需要为Universal XAML应用程序使用绝对URI:

myImage.Source = new BitmapImage(new Uri(
  "ms-appx:///Assets/WorldCupFlags/Sri_Lanka.png", UriKind.Absolute));

Within an async code block, do this: 在异步代码块中,执行以下操作:

imgMyImageControl.Source = await GetBitmapAsFile("Folder\\ImageFileName.png");

The GetBimpageAsFile function: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM