Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot show "open file dialog" window issue in WPF Apps #2663

Closed
DotNetAppCompatFeiWang opened this issue Feb 27, 2020 · 8 comments
Closed
Assignees
Labels
Bug Product bug (most likely)
Milestone

Comments

@DotNetAppCompatFeiWang
Copy link

  • .NET Core Version: 5.0.100-preview.1.20125.9
  • Windows version: Windows 10 1809
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No
  • Security issues and bugs should be reported privately, learn more via our responsible disclosure guidelines.

Problem description: Click Open File dialog button on the app.

Actual behavior: Messagebox says "Cannot show a file dialog unless application is running in UserInterActive mode "

Expected behavior: Open file dialog window show.

**Minimal repro:**using Microsoft.Win32;

private void button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
try
{
openFileDialog.ShowDialog(); // Exception on this line
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

More detail and repro machine could be found at https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1074622

We are testing this app https://github.com/NuGetPackageExplorer/NuGetPackageExplorer and most WPF apps affect by this issue.
@dotnet-actwx-bot FYI

@vatsan-madhavan
Copy link
Member

image

@vatsan-madhavan
Copy link
Member

---------------------------

---------------------------
System.InvalidOperationException: Cannot show a file dialog unless the application is running in UserInteractive mode.

   at Microsoft.Win32.CommonDialog.ShowDialog()

   at test003.MainWindow.OnOpenFileDialogButtonClicked(Object _, RoutedEventArgs __) in C:\Temp\wpf\test003\MainWindow.xaml.cs:line 34
---------------------------
OK   
---------------------------

image

@rladuca
Copy link
Member

rladuca commented Feb 27, 2020

We pivot on Environment.UserInteractive, is that not returning the correct value in 5.0?

@rladuca
Copy link
Member

rladuca commented Feb 27, 2020

Looks related to dotnet/runtime#1724 (comment)

@vatsan-madhavan
Copy link
Member

vatsan-madhavan commented Feb 27, 2020

Yeah, doesn't look like a WPF bug. Here is a self-contained repro.

Environment.UserInteractive is coming back as False, so we are failing. This must be fixed in runtime.

image

<Window x:Class="test003.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:test003"
        mc:Ignorable="d"
        Title="MainWindow" Height="80" Width="250">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="0" Grid.Column="0" Click="OnOpenFileDialogButtonClicked" Content="Open File Dialog">
            <x:Code>
                <![CDATA[
        void OnOpenFileDialogButtonClicked(object _, RoutedEventArgs __)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();
            try
            {
                dlg.ShowDialog();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }                
                ]]>
            </x:Code>
        </Button>
        <Label Grid.Row="1" Grid.Column="0" Content="Environment.UserInteractive: " FontWeight="Bold"/>
        <Label Grid.Row="1" Grid.Column="1" Content="{x:Static sys:Environment.UserInteractive}"/>
    </Grid>
</Window>

/cc @danmosemsft, @merriemcgaw

This has the potential to break WinForms as well.

@merriemcgaw
Copy link
Member

Thanks for the heads up @vatsan-madhavan . @Vino-Wang you might run into this testing WinForms. Can you see if we're impacted? File a WinForms issue to track if you do hit it?

@danmoseley
Copy link
Member

Tracking with dotnet/runtime#32929

Is this a preview 1 blocking issue?

If so, I suggest a temporary workaround in WPF code. Previous to my change, Environment.UserInteractive was hard coded to true. My change copied the .NET Framework implementation over (at least, attempted to.). The quickest workaround is for you to replace the 3 uses of Environment.UserInteractive in dotnet/wpf with hard coded "true". This will give the exact same behavior as in 3.1.

I don't see use of this in Winforms - I guess it uses the CommonDialog in WPF.

@vatsan-madhavan
Copy link
Member

vatsan-madhavan commented Feb 27, 2020

It doesn't seem to affect WinForms. The checks come from WPF itself.

Perhaps winforms should add these checks 😄 - no point in trying to show ux when Environment.UserInteractive is (genuinely) false.

@ghost ghost locked as resolved and limited conversation to collaborators Apr 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Product bug (most likely)
Projects
None yet
Development

No branches or pull requests

6 participants