首先,我们打开Vs2012,选择好Blank App模板。在MainPage.xaml中拉进Button和image两个空间。在Button控件中选择Click事件。经命名等后,代码如下:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button x:Name="Start" Content="Start" HorizontalAlignment="Left" Height="39" Margin="81,40,0,0" VerticalAlignment="Top" Width="88" Click="Start_Click"/> <Image x:Name="image" HorizontalAlignment="Left" Height="320" Margin="81,110,0,0" VerticalAlignment="Top" Width="434"/> </Grid>
接着,在MainPage.xaml.cs中的Click事件里,填入一下代码就可以了:
private async void Start_Click(object sender, RoutedEventArgs e) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.Desktop; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".gif"); openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync(); string fileToken; IStorageItemAccessList futureAccess = StorageApplicationPermissions.FutureAccessList; fileToken = futureAccess.Add(file); BitmapImage src = new BitmapImage(); src.SetSource(await file.OpenAsync(FileAccessMode.Read)); image.Source = src; AutomationProperties.SetName(image, file.Name); }
然后,按F5,演示。