動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
- 他のフォームをnewする
- ShowModalで開く
- そのフォームを閉じる
- new済みなので、再度ShowModalで開く
以下のエラーとなる。
System.InvalidOperationException: 'Window が閉じた後で、Visibility の設定や、Show、ShowDialog、およびWindowInteropHelper.EnsureHandl の呼び出しを行うことはできません。
参考: https://stackoverflow.com/questions/3568233/wpf-cannot-reuse-window-after-it-has-been-closed
要約: 以下のようにするといい。
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>
Visibility.CollapsedでなくVisibility.Hiddenのほうがいいかもしれない。