如何在Windows Phone 8中使用XAML和c#从文件夹创建图库?

本文关键字:文件夹 创建 XAML Windows Phone | 更新日期: 2023-09-27 17:50:52

我正在构建一个Windows Phone 8应用程序,它需要从一个创建的按钮(按下它)访问一个带有一些预定图像的图片库(不是默认的Windows Phone图片库应用程序)。然后,目标是图片库返回该图片库中所选项目的图像(或路径)(我不知道它将返回什么)到我正在构建的应用程序,以便在列表框控件上使用该图像。

你能告诉我如何使用XAML和c#创建我需要的图片库吗?

非常感谢!

如何在Windows Phone 8中使用XAML和c#从文件夹创建图库?

创建一个类,

PhotoItem.cs

公共类PhotoItem{公共字符串PhotoName{获取;设置;}公共BitmapImage Photo{获取;设置;}

public static List<PhotoItem> GetPhotos()
{
    return new List<PhotoItem>()
    {
        new PhotoItem(){PhotoName="Image1",Photo = new BitmapImage(new Uri("/Images/Image1.jpg", UriKind.Relative))},
        new PhotoItem(){PhotoName="Image2",Photo = new BitmapImage(new Uri("/Images/Image2.jpg", UriKind.Relative))},
    };
}

}

PhotoItemViewModel.cs

public class PhotoItemViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<PhotoItem> photoList;
        public ObservableCollection<PhotoItem> PhotoList
        {
            get
            {
                return photoList;
            }
            set
            {
                photoList = value;
                NotifyPropertyChanged();
            }
        }
        public void LoadData()
        {
            PhotoList = new ObservableCollection<PhotoItem>(PhotoItem.GetPhotos());
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

XAML

   <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector ItemsSource="{Binding PhotoList}">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding PhotoName}"></TextBlock>
                        <Image Source="{Binding Photo}"></Image>
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>

和CodeBehind.cs

 public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.LoadData();
            DataContext = viewModel;
        }