我正在建立一个Windows Phone 8应用程序,需要从一个创建的按钮(按下它),以访问一些预先确定的图像画廊(不是默认的Windows Phone画廊应用程序)。然后,目标是图片库将该图片库中所选项目的图像(或路径)返回给我正在构建的应用程序,以便在列表框控件上使用该图像。
您能告诉我如何使用XAML和C#创建我需要的图片库吗?
非常感谢!
发布于 2014-03-19 20:04:18
创建一个类,
PhotoItem.cs
公共类照片{公共字符串PhotoItem { get;set;}公共BitmapImage照片{ get;set;}
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;
}https://stackoverflow.com/questions/22505155
复制相似问题