前台代码:
<!--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();
}
}