Visual Studio/WPF > Form > 닫힌 창을 다시 ShowModal() 했을 때 > Error:System.InvalidOperationException: 'Window가 닫힌 후 Visibility 설정, Show, ShowDialog 및 WindowInteropHelper.EnsureHandl 호출 수 없습니다. > 대처

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
  • 다른 양식을 새로 만들기
  • ShowModal에서 열기
  • 그 양식을 닫습니다
  • new이 끝났으므로, 다시 ShowModal에서 열린다

  • 다음 오류가 발생합니다.

    System.InvalidOperationException: 'Window가 닫힌 후에는 Visibility를 설정하거나 Show, ShowDialog 및 WindowInteropHelper.EnsureHandl을 호출할 수 없습니다.

    참고 : h tps : // s t c ゔ ぇ rf ぉ w. 이 m / 쿠에 s 치온 s / 3568233 / wpf

    요약: 다음과 같이 하면 좋다.
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                e.Cancel = true;
                this.Visibility = Visibility.Collapsed;
            }
    
    

    code



    윈도우(WPF)를 SubWindow라는 이름으로 추가한다.

    MainWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace _170612_t1720_twoForms
    {
        /// <summary>
        /// MainWindow.xaml の相互作用ロジック
        /// </summary>
        public partial class MainWindow : Window
        {
            private SubWindow subWin;
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void uxOpen_Click(object sender, RoutedEventArgs e)
            {
                if (subWin == null)
                {
                    subWin = new SubWindow();
                    subWin.Owner = this;
                }
                subWin.ShowDialog();
            }
    
            private void uxCheck_Click(object sender, RoutedEventArgs e)
            {
                if(subWin == null)
                {
                    return;
                }
                string txt = subWin.uxName.Text;
                MessageBox.Show(txt);
            }
        }
    }
    

    MainWindow.xaml
    <Window x:Class="_170612_t1720_twoForms.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:local="clr-namespace:_170612_t1720_twoForms"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <StackPanel VerticalAlignment="Center">
                <Button Content="Open" x:Name="uxOpen"
                    Height="28" Width="100"
                    Click="uxOpen_Click"/>
                <Button Content="Check" x:Name="uxCheck"
                    Height="28" Width="100"
                    Click="uxCheck_Click"/>
            </StackPanel>
        </Grid>
    </Window>
    

    SubWindow.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace _170612_t1720_twoForms
    {
        /// <summary>
        /// SubWindow.xaml の相互作用ロジック
        /// </summary>
        public partial class SubWindow : Window
        {
            public SubWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                e.Cancel = true;
                this.Visibility = Visibility.Collapsed;
            }
        }
    }
    

    SubWindo.xaml
    <Window x:Class="_170612_t1720_twoForms.SubWindow"
            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:local="clr-namespace:_170612_t1720_twoForms"
            mc:Ignorable="d"
            Title="SubWindow" Height="300" Width="300" Closing="Window_Closing">
        <Grid>
            <StackPanel>
                <TextBlock Text="Your Name"/>
                <TextBox x:Name="uxName" Width="200" Height="28"/>
            </StackPanel>
        </Grid>
    </Window>
    

    두 번째 이후의 Open에서도 문제없이 열린다.


    Visibility.Collapsed가 아닌 Visibility.Hidden이 더 좋을 수 있습니다.

    좋은 웹페이지 즐겨찾기