简体   繁体   English

Image.Source = new BitmapImage(); 在 UWP 中不起作用

[英]Image.Source = new BitmapImage(); doesn't work in UWP

I have a problem while reading a file in C#.在 C# 中读取文件时遇到问题。 Here's the code:这是代码:

protected override void OnNavigatedTo(NavigationEventArgs EvArgs)
    {
        base.OnNavigatedTo(EvArgs);
        var Args = EvArgs.Parameter as Windows.ApplicationModel.Activation.IActivatedEventArgs;
        var FArgs = Args as Windows.ApplicationModel.Activation.FileActivatedEventArgs;
        if (Args != null)
        {
            if (Args.Kind == Windows.ApplicationModel.Activation.ActivationKind.File)
            {
                var IMG = new Image();
                IMG.Loaded += IMG_Loaded;
                string FP = FArgs.Files[0].Path;
                Uri = FP;
                IMG.Source = new BitmapImage(new Uri(FP, UriKind.RelativeOrAbsolute));
                FV.Items.Add(IMG);
                PathBlock.Text = FArgs.Files[0].Name + " - Image Viewer";

                void IMG_Loaded(object sender, RoutedEventArgs e)
                {
                    IMG.Source = new BitmapImage(new Uri(Uri));
                }
            }
        }
        if (Args == null)
        {
            PathBlock.Text = "Image Viewer";
        }
    }

The problem is at this part:问题出在这部分:

IMG.Source = new BitmapImage(new Uri(FP, UriKind.RelativeOrAbsolute));

The image won't load, even after trying a resource that the app CAN access in its own files:即使在尝试应用程序可以在其自己的文件中访问的资源之后,图像也不会加载:

IMG.Source = new BitmapImage(new Uri("ms-appx:///09.png, UriKind.RelativeOrAbsolute));

It still won't work.它仍然行不通。 This only works with this code:这仅适用于以下代码:

var FOP = new Windows.Storage.Pickers.FileOpenPicker();
StorageFile F = await FOP.PickSingleFileAsync();
IRandomAccessStream FS = await F.OpenAsync(FileAccessMode.Read);
var BI = new BitmapImage();
var IMG = new Image();
await BI.SetSourceAsync(FS);
IMG.Source = BI;

Sadly, I can't use a file stream OR a file picker in the first code example, so I can't make it work as I did here.可悲的是,我不能在第一个代码示例中使用文件流或文件选择器,所以我不能像这里那样让它工作。

Image.Source = new BitmapImage(); Image.Source = new BitmapImage(); doesn't work in UWP在 UWP 中不起作用

The problem is file Path property does not use to set image source directly in UWP platform.问题是文件Path属性不用于直接在 UWP 平台中设置图像源。 Derive from your code, it looks you open the image file with current app, if you can get the IStorageItem from FArgs.Files list, you could also convert it as StorageFile , and then pass the opened file stream to BitmapImage .从您的代码派生,它看起来您使用当前应用程序打开图像文件,如果您可以从FArgs.Files列表中获取IStorageItem ,您也可以将其转换为StorageFile ,然后将打开的文件流传递给BitmapImage

For more detail steps please refer to the following code.更详细的步骤请参考以下代码。

protected async override void OnNavigatedTo(NavigationEventArgs EvArgs)
{
    base.OnNavigatedTo(EvArgs);
    var Args = EvArgs.Parameter as Windows.ApplicationModel.Activation.IActivatedEventArgs;
    var FArgs = Args as Windows.ApplicationModel.Activation.FileActivatedEventArgs;
    if (Args != null)
    {
        if (Args.Kind == Windows.ApplicationModel.Activation.ActivationKind.File)
        {
            var IMG = new Image();
            string FP = FArgs.Files[0].Path;
            var Uri = FP;
            StorageFile imageFile = FArgs.Files[0] as StorageFile;
            using (var stream = await imageFile.OpenReadAsync())
            {
                var bitmp = new BitmapImage();
                bitmp.SetSource(stream);
                IMG.Loaded += (s, e) => { IMG.Source = bitmp; };
            }

            RootLayout.Children.Add(IMG);
        }
    }
    if (Args == null)
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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