尝试具有参考这个..在这个例子中我已经使用一个按钮来激活PhotoChooserTask
。 XAML就是这样。
XAML
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Name="btnSet" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Content="Set Image" Click="btnSet_Click" />
</Grid>
</Grid>
该按钮Click事件。
C#
private void btnSet_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoTask = new PhotoChooserTask();
photoTask.Completed += photoTask_Completed;
photoTask.PixelHeight = 1280;
photoTask.PixelWidth = 768;
photoTask.Show();
}
在photoTask_Completed
事件处理程序,你可以将图像保存到IsolatedStorage
void photoTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = e.ChosenPhoto.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
}
}
然后在OnNavigatedTo
事件中,你可以从IsolatedStorage
加载图像,并将其设置为的背景。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
byte[] data;
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
using(IsolatedStorageFileStream stream = isoStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
}
MemoryStream ms = new MemoryStream(data);
bmp = new BitmapImage();
bmp.SetSource(ms);
ImageBrush img = new ImageBrush();
img.ImageSource = bmp;
LayoutRoot.Background = img;
}
}
}
变量fileName
保存保存到IsolatedStorage
图像的名称。每次从库中选择新图像时,图像都会被覆盖。希望这可以帮助。
谢谢yooooooooooooooooooooooou very veryyyyyyyyyyyyyyy much^_^ – mohammad 2014-10-02 10:10:04
不客气:D – 2014-10-02 10:36:58