locked
FileOpenPicker inside a ContentDialog RRS feed

  • Question

  • Hello,

    I'm trying to develop an app for Windows Phone and running into a small problem with the FileOpenPicker inside my ContentDialog.

    I have a list with items and a command bar button that opens a dialog to quickly add new items. This all works fine and I'm happy with it. Except now I would love to allow users to select an image in my dialog. But as soon as the open file dialog opens, the content dialog is closed. Is there a good way to make sure the content dialog remains open while picking an image file?

    My quick solution for now is to reopen the dialog if it isn't closed properly, but that doesn't look well (keeps flickering).

    The other solution perhaps is to use a normal page, but is that recommended?

    My current code:

    MainPage.xaml.cs (Where I open the content dialog)

    	/// <summary>
            /// Opens the add sheet dialog
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private async void AddSheet_Click(object sender, RoutedEventArgs e)
            {
                AddSheet addSheetDialog = new AddSheet();
                if (await addSheetDialog.ShowAsync() == ContentDialogResult.Primary)
                {
                    // Sheet was added, refresh content
                    LoadSheets();
                }
            }

    The code in the AddSheet.xaml content dialog

    <ContentDialog
        x:Class="Sheets.WinPhone.AddSheet"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Sheets.WinPhone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="ADD SHEET"
        PrimaryButtonText="add"  
        SecondaryButtonText="cancel"
        PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
        SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
    
        <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <TextBox x:Name="sheetName" Header="Name"/>
            <Grid>
                <Image Source="Assets\SmallLogo.scale-240.png" Name="img" Width="48" Height="48" Tapped="OpenImage_Click"  />
            </Grid>
        </StackPanel>
    </ContentDialog>
    

    	CoreApplicationView view = CoreApplication.GetCurrentView();
            String ImagePath;
    
            private void OpenImage_Click(object sender, RoutedEventArgs e)
            {
                ImagePath = string.Empty;
                FileOpenPicker filePicker = new FileOpenPicker();
                filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                filePicker.ViewMode = PickerViewMode.Thumbnail;
                filePicker.PickSingleFileAndContinue();
                view.Activated += viewActivated;
            }
    
            private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
            {
                FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
                if (args != null && args.Files.Count > 0)
                {
                    view.Activated -= viewActivated;
                    StorageFile storageFile = args.Files[0];
                    var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                    var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                    await bitmapImage.SetSourceAsync(stream);
    
                    var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                    img.Source = bitmapImage;
                }
            }

    Wednesday, December 7, 2016 5:36 PM

Answers

  • In WP8.1, this scenario is not supported, sadly. The system can choose to suspend your app when you do the file picking, and your content dialog may not be present there any longer.

    I would suggest you use a normal page. This is the recommended approach.

    • Marked as answer by Martijn1985 Thursday, December 8, 2016 9:18 AM
    Thursday, December 8, 2016 8:38 AM

All replies

  • Hi Martijn1985,

    How about PickSingleFileAsync instead of obsolete PickSingleFileAndContinue?

    private void OpenImage_Click(object sender, RoutedEventArgs e)
    {
            //ImagePath = string.Empty;
            FileOpenPicker filePicker = new FileOpenPicker();
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.ViewMode = PickerViewMode.Thumbnail;
           
            var storageFile = await filePicker.PickSingleFileAsync();
            if (storageFile != null) 
            {
                var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                await bitmapImage.SetSourceAsync(stream);
    
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                img.Source = bitmapImage;   
            }
    }

    Thursday, December 8, 2016 2:27 AM
  • Hello FEC-4RP,

    Thank you for your reply. I tried using PickSingleFileAsync first, as it seemed a much cleaner solution, unfortunately, it says this function is obsolete. And when I try to run it I get an exception telling me it isn't supported on Windows Phone 8.1


    • Edited by Martijn1985 Thursday, December 8, 2016 6:50 AM
    Thursday, December 8, 2016 6:50 AM
  • In WP8.1, this scenario is not supported, sadly. The system can choose to suspend your app when you do the file picking, and your content dialog may not be present there any longer.

    I would suggest you use a normal page. This is the recommended approach.

    • Marked as answer by Martijn1985 Thursday, December 8, 2016 9:18 AM
    Thursday, December 8, 2016 8:38 AM
  • Hello mcosmin,

    Thank you for your reply. Too bad this isn't supported in WP8.1, but if using a normal page is recommended I'll just use that. I already tested it and it works great.

    Thanks everybody for their help!

    Thursday, December 8, 2016 9:18 AM
  • Oh great. I also think using ordinary Page in standard frame navigation system is the best solution as long as it's possible.
    Though.. I think it's also possible to make dialog like stuff by using Popup or AppBar, if you don't have to put so many controls on it.

    Thursday, December 8, 2016 1:40 PM