1. 程式人生 > >Winform與WPF對話方塊(MessageBox, Dialog)之比較

Winform與WPF對話方塊(MessageBox, Dialog)之比較

Winform:使用System.Windows.Forms名稱空間中相應控制元件; WPF則呼叫Microsoft.Win32。

MessageBox:

// WinForm
        private void WFMessageBoxClicked(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Hello from Windows Forms!", "Hello World!");
        }

// WPF
        private void WPFMessageBoxClicked(object sender, RoutedEventArgs e)
        {
            System.Windows.MessageBox.Show("Hello from WPF!", "Hello world!");
        } 

OpenFileDialog / SaveFileDialog:
// WinForm
        private void WinFormsDialogClicked(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dialogOpenFile = new System.Windows.Forms.OpenFileDialog();
            dialogOpenFile.ShowDialog();

            //System.Windows.Forms.SaveFileDialog dialogSaveFile = new System.Windows.Forms.SaveFileDialog();
            //dialogSaveFile.ShowDialog();
        }

// WPF
        private void WPFDialogClicked(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialogOpenFile = new Microsoft.Win32.OpenFileDialog();
            dialogOpenFile.ShowDialog();

            //Microsoft.Win32.SaveFileDialog dialogSaveFile = new Microsoft.Win32.SaveFileDialog();
            //dialogSaveFile.ShowDialog();
        }