FileOpenPicker’ın Universal App’de Kullanımı
Merhaba Arkadaşlar Bu yazımda FileOpenPicker Kontrolünün Windows 10 Universal app’de nasıl kullanıldığını anlatacağım.
C# İle OpenFileDialog ile yaptığımızın aynısı; yani dosyadan resim,müzik,video vb. seçip programda çalıştırmaya yarar.
Programda dosyadan resim seçip göstermek için yapmamız gerekenler ilk önce boş bir “Blank Page” yani xaml sayfası açalım.Sayfada dosyadan resim seçiceğimiz için Toolbox’dan “MediaElement” seçip projeye atalım.Sonrada dosyadan seçtiğimiz resmi göstermek için “İmage” ve “Button” kontrolü ekleyelim.
Projenin Xaml kodları aşağıdaki gibi olmalıdır..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Page x:Class="App16.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App16" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <MediaElement Name="mediaControl" Height="400" /> <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="50,316,0,0" VerticalAlignment="Top" Click="button_Click"/> <Image x:Name="image1" HorizontalAlignment="Left" Height="74" Margin="50,242,0,0" VerticalAlignment="Top" Width="54"/> </Grid> </Page> |
MainPage.xaml kımını hallettikten sonra MainPage.xaml.cs kısmının kodlarını yazalım.Bunun için
“SetLocalMedia” fonksiyonuyla dosyadan resim seçip image kontrolüne atayalım.
1 |
opener.FileTypeFilter.Add(".jpg"); |
ile seçiceğimiz resmin formatlarını belirleyebiliriz.
“SetLocalMedia” fonksiyonunun içeriğini aşağıdaki gibi yazalım..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
async private void SetLocalMedia() { var openPicker = new Windows.Storage.Pickers.FileOpenPicker(); FileOpenPicker opener = new FileOpenPicker(); opener.ViewMode = PickerViewMode.Thumbnail; opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary; opener.FileTypeFilter.Add(".jpg"); opener.FileTypeFilter.Add(".jpeg"); opener.FileTypeFilter.Add(".png"); StorageFile file = await opener.PickSingleFileAsync(); if (file != null) { // We've now got the file. Do something with it. var stream1 = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream1); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream1); image1.Source = bitmapImage; } else { //OutputTextBlock.Text = "The operation may have been cancelled."; } } |
Button’a tıklandığında dosyadan resim seçmesini istediğimiz için Button’un Click eventinde Yazdığımız fonksiyonu çağıralım.
1 2 3 4 |
private void button_Click(object sender, RoutedEventArgs e) { SetLocalMedia(); } |
Buttona tıkladığımızda Dosyadan resim seçip programda göstericektir.
Leave a reply