【问题标题】:Image source changing in C# for windows phone 8.1 RT在 C# 中为 windows phone 8.1 RT 更改图像源
【发布时间】:2015-02-12 05:20:45
【问题描述】:

我想在运行时使用 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));
        }

它没有显示编译时错误,但是当我运行它并单击按钮时,它显示异常。它说“在 mscorlib.ni.dll 中发生了 'System.ArgumentException' 类型的异常,但未在用户代码中处理。”

【问题讨论】:

  • 您是否在 Windows Phone Blank App 中打开过? @彼得托尔
  • 对不起,没有看到标题中的“RT”(我只是看了标签)。解决方案如下。

标签: c# xaml windows-phone


【解决方案1】:

提示在异常中:

给定的 System.Uri 无法转换为 Windows.Foundation.Uri

您需要为通用 XAML 应用程序使用绝对 URI:

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

【讨论】:

  • 什么意思?上面的黄色文本是您在代码异常中获得的文本。灰色文本是您应该替换的内容。
  • 你在那个位置有图片吗?您是否尝试在 XAML 中直接输入 URI?
  • 抱歉,它成功了。我忘了更改我的文件夹名称。
【解决方案2】:

在异步代码块中,执行以下操作:

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

GetBimageAsFile 函数:

    /// <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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多