Visual Studio 2017 Community (以下VS)
Windows 7 Pro (32bit)
10:28頃
win.ShowDialog();の戻り値ではなく、
win.Result;を後から取得している。
実際には.Resultではなく、.DialogResultというプロパティを取得するようだ。
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 _170526_t1910_winResult
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
InputWindow inpWin = new InputWindow();
inpWin.ShowDialog();
Nullable<bool> res = inpWin.DialogResult;
if (res == null)
{
MessageBox.Show("null");
} else if (res == true)
{
MessageBox.Show("true");
} else
{
MessageBox.Show("false");
}
}
}
}
InputWindow.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 _170526_t1910_winResult
{
/// <summary>
/// InputWindow.xaml の相互作用ロジック
/// </summary>
public partial class InputWindow : Window
{
public InputWindow()
{
InitializeComponent();
}
private void Window_Closed(object sender, EventArgs e)
{
// DialogResult = true;
}
private void OkButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
}
}
補足
Window_Closed()においてDialogResultに値を代入しようとするとエラーが出た。
System.InvalidOperationException: 'Window を作成し、ダイアログとして表示した後でのみ DialogResult を設定できます。'