public async Task Expand(Contractor contractor)
        {
            if (contractor.HasLazyPhoto && (contractor.PhotoRaw == null))
            {
                await ContractorsRepository.Current.Expand(contractor);

                contractor.Photo = await UIFunctions.RawToImage(contractor.PhotoRaw);
            }
        }
Example #2
0
        private async void ChoosePhoto()
        {
            var fileOpenPicker = new FileOpenPicker();

            fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
            fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            fileOpenPicker.FileTypeFilter.Add(".png");
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            fileOpenPicker.FileTypeFilter.Add(".jpeg");
            fileOpenPicker.FileTypeFilter.Add(".bmp");
            fileOpenPicker.FileTypeFilter.Add(".tiff");
            fileOpenPicker.FileTypeFilter.Add(".ico");

            var storageFile = await fileOpenPicker.PickSingleFileAsync();

            if (storageFile == null)
            {
                return;
            }

            var pointer = Window.Current.CoreWindow.PointerCursor;

            Window.Current.CoreWindow.PointerCursor =
                new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Wait, 1);

            using (IRandomAccessStream stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                byte[] fileBytes = new byte[stream.Size];

                using (DataReader reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);

                    reader.ReadBytes(fileBytes);

                    try
                    {
                        this.iContractor.Photo = await UIFunctions.RawToImage(fileBytes);
                    }
                    catch (Exception e)
                    {
                        if (e.HResult == -2003292336)
                        {
                            Window.Current.CoreWindow.PointerCursor = pointer;

                            var dialog = new MessageDialog("То что вы пытаетесь присвоить - технически не совсем изображение");

                            dialog.Commands.Add(new UICommand("Закрыть"));
                            dialog.DefaultCommandIndex = 0;
                            dialog.CancelCommandIndex  = 0;

                            await dialog.ShowAsync();

                            return;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    this.iContractor.PhotoRaw = fileBytes;

                    this.ImageHint = ZoomImageHint;
                }
            }

            Window.Current.CoreWindow.PointerCursor = pointer;
        }