Skip to content

Instantly share code, notes, and snippets.

@hatsunea

hatsunea/TItem.cs Secret

Created Nov 27, 2020
Embed
What would you like to do?
using ImageFilePickerSample.Common;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
namespace ImageFilePickerSample.Models
{
public class TItem : INotifyPropertyChanged
{
private Guid _ID;
public Guid ID
{
get { return this._ID; }
set
{
if (this._ID != value)
{
this._ID = value;
OnPropertyChanged();
}
}
}
private ImageSource _ImageItem;
public ImageSource ImageItem
{
get { return _ImageItem; }
set
{
if (_ImageItem != value)
{
_ImageItem = value;
OnPropertyChanged();
}
}
}
private async Task SetImageFromFileAsync()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
var file = await picker.PickSingleFileAsync();
var imageFile = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(file.Name);
using (var stream = await imageFile.OpenReadAsync())
{
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.SetSource(stream);
this.ImageItem = bitmapImage;
}
}
private RelayCommand _GetImageCommand;
public RelayCommand GetImageCommand
{
get
{
if (_GetImageCommand == null)
{
_GetImageCommand = new RelayCommand(async () =>
{
await this.SetImageFromFileAsync();
});
}
return _GetImageCommand;
}
set
{
_GetImageCommand = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment