简体   繁体   English

从文件夹C#UWP中的图像列表显示图像

[英]Image display from list of images in a folder C# UWP

I am currently working on building a UWP application that stores music and want it to display an album image that the user can upload. 我目前正在构建一个UWP应用程序来存储音乐,并希望它显示用户可以上传的专辑图像。 I have figured out how to save the images into a file, but am unable to get them to display, even after trying to bind them to the image control in XAML. 我已经弄清楚了如何将图像保存到文件中,但是即使试图将它们绑定到XAML中的图像控件后也无法显示它们。 I'm fairly new to this, so I apologize if it's a simple fix. 我对此很陌生,因此如果这是一个简单的解决方法,我深表歉意。

Here's my code from my MainPage.xaml.cs: 这是我MainPage.xaml.cs中的代码:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using System.Text;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.Media.Core;

using Windows.Storage;

using Windows.Storage.FileProperties;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Media.Imaging;

using Windows.UI.Xaml.Navigation;



// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409



namespace MyMusicLibrary

{

    /// <summary>

    /// An empty page that can be used on its own or navigated to within a Frame.

    /// </summary>

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }
private async void ImageButton_Click(object sender, RoutedEventArgs e)

        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");


            var image = await picker.PickSingleFileAsync();

            var folder = ApplicationData.Current.LocalFolder;

            var imageFolder = await folder.CreateFolderAsync("imagefolder", CreationCollisionOption.OpenIfExists);

            if (imageFolder != null && image != null)
            {
                var newImage = await image.CopyAsync(imageFolder, image.Name, NameCollisionOption.GenerateUniqueName);
            }


        }

And here is my code from MainPage.xaml: 这是MainPage.xaml中的代码:

<Grid.Background>

    <ImageBrush Stretch="Fill" ImageSource="Assets/headphones.jpg"/>

</Grid.Background>

<Button Content="Add Music" HorizontalAlignment="Center" BorderBrush="Black" Background="Gray" Foreground="White" Margin="50,50,50,50" VerticalAlignment="Top" Click="MusicButton_Click"/>
<Button Content="Add Album Cover" HorizontalAlignment="Center" BorderBrush="Black" Background="Gray" Foreground="White" Margin="50,100,50,50" VerticalAlignment="Top" Click="ImageButton_Click"/>
<Image x:Name="newImage" HorizontalAlignment="Center" Width ="300" Height="300" Margin="0,100,0,0" />

<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True" Margin="0,200,0,0" />

You can try this code, 您可以尝试此代码,

<Image Source="ms-appx:///Assets/logo.png" />

Make sure that Image Properties set "Build Action" to "Content" and "Copy To Output Directory" to "Copy always". 确保“图像属性”将“生成操作”设置为“内容”,将“复制到输出目录”设置为“始终复制”。

[1]:https://i.stack.imgur.com/EFWxV.png

Thanks!!! 谢谢!!!

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

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