【问题标题】:How can I create an image gallery from a folder in Windows Phone 8 using XAML and C#?如何使用 XAML 和 C# 从 Windows Phone 8 中的文件夹创建图像库?
【发布时间】:2014-03-19 11:59:59
【问题描述】:

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

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

非常感谢!

【问题讨论】:

  • 您的尝试有什么问题?

标签: c# image xaml windows-phone-8 gallery


【解决方案1】:

创建一个类,

PhotoItem.cs

公共类 PhotoItem { 公共字符串 PhotoName { 获取;放; } 公共 BitmapImage 照片 { 获取;放; }

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

【讨论】:

  • 什么是“CodeBehind.cs”?我必须将代码放在哪里:“public class PhotoItem { public string PhotoName { get; set; } public BitmapImage Photo { get; set; }”?我必须在 XAML 中写什么?我还没有看到代码....非常感谢您的快速回复! ;)
  • 再次感谢您的快速回复!我将在接下来的几个小时内尝试并检查是否有效。我的声望是 9,所以现在我不能给你 +1。对不起! :( 声望达到 15 级我会尽快给你 :)
猜你喜欢
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多