dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.04k stars 1.17k forks source link

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

Closed DotNetAppCompatFeiWang closed 4 years ago

DotNetAppCompatFeiWang commented 4 years ago

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 commented 4 years ago

image

vatsan-madhavan commented 4 years ago
---------------------------

---------------------------
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 commented 4 years ago

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

rladuca commented 4 years ago

Looks related to https://github.com/dotnet/runtime/issues/1724#issuecomment-574728941

vatsan-madhavan commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

Tracking with https://github.com/dotnet/runtime/issues/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 commented 4 years ago

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.