前臺代碼:
<!--LayoutRoot 是包含所有頁面內容的根網格-->
<Grid x:Name="LayoutRoot" Background="#eee">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="ContentPanel" Margin="12,0,12,578" Grid.Row="1">
<ListBox Height="768" Margin="-12,0,-12,-578" Name="listBox1" Opacity="8" VerticalAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Width="500" Height="500" Margin="0,5,0,5"
Source="{Binding}"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
<!--演示 ApplicationBar 用法的示例代碼-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="appbar.feature.camera.rest.png" Text="按鈕 1" Click="ApplicationBarIconButton_Click" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
後臺代碼:
private ObservableCollection<BitmapImage> bitmapImages = new ObservableCollection<BitmapImage>();
public Page4(){
InitializeComponent();
LoadImg();
listBox1.ItemsSource = bitmapImages;
}
private void LoadImg()
{
bitmapImages.Clear();
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
try
{
foreach (string file in isf.GetFileNames("baby"+"*.jpg"))//遍歷所有jpg圖片
{
BitmapImage bmpImg = new BitmapImage();
using (Stream stream = isf.OpenFile(file, FileMode.Open))
{
bmpImg.SetSource(stream);
}
bitmapImages.Add(bmpImg);
}
}
catch { }
}
void pcTask_Completed(object sender, PhotoResult e)
{
if (e.Error != null)
{
CommonHelper.MsgBox("執行失敗!");
return;
}
if (e.TaskResult == TaskResult.OK)
{
//BitmapImage img = new BitmapImage();
//img.SetSource(e.ChosenPhoto);//用圖片流初始化BitmapImage圖片對象
string name = (string)IsolatedStorageSettings.ApplicationSettings["name"];
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
string filename = "baby"+name+DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
using (e.ChosenPhoto)
{
Stream stream = isf.CreateFile(filename);
e.ChosenPhoto.CopyTo(stream);
BitmapImage bmpImg = new BitmapImage();
bmpImg.SetSource(stream);
bitmapImages.Add(bmpImg);
}
}
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
PhotoChooserTask pcTask = new PhotoChooserTask();
pcTask.ShowCamera = true;//顯示拍照按鈕,用戶即可以選擇圖片,又可以拍照
pcTask.PixelHeight = 100;//如果設定了PixelHeight和PixelWidth,則選擇後的圖片要求用戶截取成合適的大小!
pcTask.PixelWidth = 100;//可以用來實現上傳QQ頭像的功能。
pcTask.Completed += new EventHandler<PhotoResult>(pcTask_Completed);
pcTask.Show();
}
}