C# 将BitmapImage设置为UWP中的ImageSource不工作

C# 将BitmapImage设置为UWP中的ImageSource不工作,c#,xaml,uwp,C#,Xaml,Uwp,我正在尝试使用BitmapImage在代码中设置图像的源,但没有显示任何内容 这是我的密码: xaml: 这是一个权限问题。您的应用无法直接读取c:\users\XXX,因此无法从该路径加载位图图像。看 假设c:\Users\XXX\Pictures是当前用户的图片库,并且应用程序具有图片库功能,则您可以获取图像文件的代理句柄并将其加载 我假设您在这里的代码经过简化以供演示,因为图片库是应用程序控制之外的以用户为中心的位置。该应用程序通常不能假设存在硬编码的图像名称 // . .

我正在尝试使用
BitmapImage
在代码中设置
图像的源,但没有显示任何内容
这是我的密码:

xaml:


这是一个权限问题。您的应用无法直接读取c:\users\XXX,因此无法从该路径加载位图图像。看

假设c:\Users\XXX\Pictures是当前用户的图片库,并且应用程序具有图片库功能,则您可以获取图像文件的代理句柄并将其加载

我假设您在这里的代码经过简化以供演示,因为图片库是应用程序控制之外的以用户为中心的位置。该应用程序通常不能假设存在硬编码的图像名称

        // . . .
        await SetImageAsync("image.jpg");
        // . . . 
    }

    private async Task SetImageAsync(string imageName)
    {
        // Load the imageName file from the PicturesLibrary
        // This requires the app have the picturesLibrary capability
        var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(imageName);
        using (var imageStream = await imageFile.OpenReadAsync())
        {
            var image = new BitmapImage();
            image.DecodePixelWidth = 100;

            // Load the image from the file stream
            await image.SetSourceAsync(imageStream);
            this.img.Source = image;
        }
    }

你有没有viewModel?然后你可以将source属性设置为binding?也可以查看该链接是针对wpfc的,这与你的图像源不更新时遇到的问题不一样吗?我还没有进入UWP,希望它使用与WPF相同的逻辑来处理类似的事情。
var image =  new BitmapImage(new Uri(@"C:\Users\XXX\Pictures\image.jpg", UriKind.Absolute));
image.DecodePixelWidth = 100;
this.img.Source = image;
        // . . .
        await SetImageAsync("image.jpg");
        // . . . 
    }

    private async Task SetImageAsync(string imageName)
    {
        // Load the imageName file from the PicturesLibrary
        // This requires the app have the picturesLibrary capability
        var imageFile = await KnownFolders.PicturesLibrary.GetFileAsync(imageName);
        using (var imageStream = await imageFile.OpenReadAsync())
        {
            var image = new BitmapImage();
            image.DecodePixelWidth = 100;

            // Load the image from the file stream
            await image.SetSourceAsync(imageStream);
            this.img.Source = image;
        }
    }