2014-03-19 60 views
1

我正在構建一個Windows Phone 8應用程序,它需要從創建的按鈕(按下它)訪問帶有一些預定圖像(不是默認的Windows Phone圖庫應用程序)的圖像庫。然後,目標是圖像庫返回該圖庫中選定項目的圖像(或路徑)(我不知道它會返回什麼)到我正在構建的應用程序,以便在列表框控件上使用該圖像。如何使用XAML和C#從Windows Phone 8中的文件夾創建圖庫?

您能告訴我如何創建需要使用XAML和C#的圖像庫嗎?

非常感謝!

+1

有什麼不適合你試過的東西? –

回答

1

創建一個類,

PhotoItem.cs

公共類PhotoItem { 公共字符串PhotoName {獲得;組; } public BitmapImage Photo {get;組; }

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

什麼是「CodeBehind.cs」?我在哪裏必須放置代碼:「public class PhotoItem {public string PhotoName {get; set;} public BitmapImage Photo {get; set;}」?我需要在XAML中編寫什麼?我還沒有看到代碼....非常感謝您的快速回復! ;) – DrewJXW

+0

再次感謝您的快速回復!我將在接下來的幾個小時嘗試它,並檢查是否有效。我的聲望是9,所以在這一刻我不能給你+1。抱歉! :(只要我達到15的聲望,我會盡快給你) – DrewJXW

相關問題