ToB企服应用市场:ToB评测及商务社交产业平台

标题: DevExpress开辟WPF应用实现对话框总结 [打印本页]

作者: 我可以不吃啊    时间: 2024-6-13 20:07
标题: DevExpress开辟WPF应用实现对话框总结
说明:


简单标准弹窗

使用标准的.NET的MessageBox:
  1. public void BaseMsg()
  2. {
  3.     MessageBox.Show("This is a standard .NET MessageBox", "Title", MessageBoxButton.OK, MessageBoxImage.Information);
  4. }
复制代码

DevExpress的DXMessageBox

  1. public void DxBaseMsg()
  2. {
  3.     // 显示一个简单的消息框
  4.     DXMessageBox.Show("This is a DevExpress DXMessageBox", "Title", MessageBoxButton.OK, MessageBoxImage.Information);
  5. }
复制代码

DevExpress的IMessageBoxService

在DevExpress WPF中使用IMessageBoxService来显示一个消息框,并根据用户的选择更新按钮的文本,符合MVVM模式的设计原则,保持了视图和视图模型的分离
  1. <Window ...
  2.         xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
  3.         xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
  4.         DataContext="{dxmvvm:ViewModelSource Type=local:ViewModel}">
  5.     <dxmvvm:Interaction.Behaviors>
  6.         <dx:DXMessageBoxService/>
  7.     </dxmvvm:Interaction.Behaviors>
  8.     <dx:SimpleButton Content="{Binding ButtonText}"
  9.                      Command="{Binding SaveConfirmationCommand}"/>
  10. </Window>
复制代码
  1. public class ViewModel : ViewModelBase {
  2.     public virtual string ButtonText { get; set; } = "Save Changes Button";
  3.     IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }
  4.     [Command]
  5.     public void SaveConfirmation() {
  6.         MessageResult result;
  7.         result = MessageBoxService.ShowMessage(
  8.             messageBoxText: "Save Changes?",
  9.             caption: "DXApplication",
  10.             icon: MessageIcon.Question,
  11.             button: MessageButton.YesNoCancel,
  12.             defaultResult: MessageResult.Cancel
  13.         );
  14.         switch (result) {
  15.             case MessageResult.Yes:
  16.                 ButtonText = "Changes Saved";
  17.                 break;
  18.             case MessageResult.No:
  19.                 ButtonText = "Changes Discarded";
  20.                 break;
  21.             case MessageResult.Cancel:
  22.                 ButtonText = "Continue Editing";
  23.                 break;
  24.         }
  25.     }
  26. }
复制代码

点击“Yes”按钮,修改按钮显示的文字为“Changes Saved”

DevExpress的DXDialog

DXDialog提供了比标准WPF对话框更强大的功能和更高的机动性。它在定制、样式支持、MVVM集成、功能丰富性和一致的用户体验方面体现出色
DXDialog提供了丰富的功能,比如:

简单控件

  1. public void BaseDXDialog()
  2. {
  3.     var dialog = new DXDialog
  4.     {
  5.         WindowStartupLocation = WindowStartupLocation.CenterScreen,
  6.         Width = 500,
  7.         Height = 300,
  8.         Title = "Custom Dialog",
  9.         Content = new TextBlock { Text = "This is a custom DXDialog", Margin = new Thickness(10) }
  10.     };
  11.     var result = dialog.ShowDialog();
  12.     if (result == true)
  13.     {
  14.         MessageBox.Show("OK clicked");
  15.     }
  16.     else
  17.     {
  18.         MessageBox.Show("Cancel clicked");
  19.     }
  20. }
复制代码

复杂控件

  1. public void CompDXDialog()
  2. {
  3.     // 创建一个 StackPanel 作为对话框的内容
  4.     var stackPanel = new StackPanel
  5.     {
  6.         Margin = new Thickness(10)
  7.     };
  8.     // 在 StackPanel 中添加控件
  9.     stackPanel.Children.Add(new TextBlock { Text = "Enter your name:", Margin = new Thickness(0, 0, 0, 10) });
  10.     var nameTextBox = new TextBox { Width = 200, Margin = new Thickness(0, 0, 0, 10) };
  11.     stackPanel.Children.Add(nameTextBox);
  12.     stackPanel.Children.Add(new TextBlock { Text = "Select your age:", Margin = new Thickness(0, 0, 0, 10) });
  13.     var ageComboBox = new ComboBox { Width = 200, Margin = new Thickness(0, 0, 0, 10) };
  14.     ageComboBox.ItemsSource = new int[] { 18, 19, 20, 21, 22, 23, 24, 25 };
  15.     stackPanel.Children.Add(ageComboBox);
  16.     // 设置 StackPanel 作为对话框的内容
  17.     var dialog = new DXDialog
  18.     {
  19.         WindowStartupLocation = WindowStartupLocation.CenterScreen,
  20.         Title = "Custom Dialog with Controls",
  21.         Content = stackPanel
  22. };
  23.     // 显示对话框并获取结果
  24.     var result = dialog.ShowDialog();
  25.     // 根据对话框结果进行处理
  26.     if (result == true)
  27.     {
  28.         string name = nameTextBox.Text;
  29.         int? age = ageComboBox.SelectedItem as int?;
  30.         MessageBox.Show($"Name: {name}, Age: {age}");
  31.     }
  32.     else
  33.     {
  34.         MessageBox.Show("Dialog was cancelled.");
  35.     }
  36. }
复制代码

自定义控件

可以将一个View页面(例如一个用户控件)放置在DXDialog的Content属性中。这样可以使对话框的内容更加复杂和模块化,从而更好地构造和管理代码
以下是一个示例,展示如安在DXDialog中放置一个用户控件:
  1. public void UserControlDXDialog()
  2. {
  3.     // 创建并设置用户控件作为对话框的内容
  4.     var myUserControl = new MyUserControl();         
  5.     var dialog = new DXDialog
  6.     {
  7.         WindowStartupLocation = WindowStartupLocation.CenterScreen,
  8.         Title = "Custom Dialog with UserControl",
  9.         Content = myUserControl
  10.     };         
  11.     // 显示对话框并获取结果
  12.     var result = dialog.ShowDialog();
  13.     // 根据对话框结果进行处理
  14.     if (result == true)
  15.     {
  16.         string name = myUserControl.UserName;
  17.         int? age = myUserControl.UserAge;
  18.         MessageBox.Show($"Name: {name}, Age: {age}");
  19.     }
  20.     else
  21.     {
  22.         MessageBox.Show("Dialog was cancelled.");
  23.     }
  24. }
复制代码

DevExpress的IDialogService

自定义对话框按钮

SimpleDialogView.xaml
  1. <UserControl x:Class="DXMessageBoxDemos.View.SimpleDialogView"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.              xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
  7.              xmlns:local="clr-namespace:DXMessageBoxDemos.View"
  8.              xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
  9.              mc:Ignorable="d"
  10.              d:DesignHeight="450" d:DesignWidth="800">
  11.     <dxmvvm:Interaction.Behaviors>
  12.         <dx:CurrentDialogService />
  13.     </dxmvvm:Interaction.Behaviors>
  14.     <StackPanel>
  15.         <ComboBox SelectedItem="{Binding DialogResult}">
  16.             <ComboBox.Items>
  17.                 <dxmvvm:MessageResult>Yes</dxmvvm:MessageResult>
  18.                 <dxmvvm:MessageResult>No</dxmvvm:MessageResult>
  19.                 <dxmvvm:MessageResult>Cancel</dxmvvm:MessageResult>
  20.             </ComboBox.Items>
  21.         </ComboBox>
  22.         <Button Command="{Binding CloseCommand}" Content="Close the dialog from the dialog view model" />
  23.     </StackPanel>
  24. </UserControl>
复制代码
SimpleDialogViewModel.cs
  1. public class SimpleDialogViewModel : ViewModelBase
  2. {
  3.     public MessageResult DialogResult
  4.     {
  5.         get { return GetProperty(() => DialogResult); }
  6.         set { SetProperty(() => DialogResult, value); }
  7.     }
  8.     protected ICurrentDialogService CurrentDialogService { get { return GetService<ICurrentDialogService>(); } }
  9.     [Command]
  10.     public void Close()
  11.     {
  12.         CurrentDialogService.Close(DialogResult);
  13.     }
  14. }
复制代码
MainViewModel.cs
  1. public MessageResult Result
  2. {
  3.     get { return GetProperty(() => Result); }
  4.     set { SetProperty(() => Result, value); }
  5. }
  6. protected IDialogService DialogService { get { return GetService<IDialogService>(); } }
  7. [Command]
  8. public void ShowDialog()
  9. {
  10.     Result = DialogService.ShowDialog(dialogButtons: MessageButton.YesNoCancel, title: "Simple Dialog", viewModel: new SimpleDialogViewModel());
  11. }
复制代码
MainView.xaml
  1. <StackPanel>
  2.     <TextBlock Text="DialogService:"/>
  3.     <Button Command="{Binding ShowDialogCommand}" Content="Show Dialog" />
  4.     <TextBlock Text="{Binding Result, StringFormat='The last result is {0}'}" />
  5. </StackPanel>
复制代码

异步按钮

DialogService可以显示异步按钮。这些按钮指示关联的异步操作是否正在举行
  1. public void ShowRegistrationForm()
  2. {
  3.     UICommand registerAsyncCommand = new UICommand(
  4.        id: null,
  5.        caption: "Register Async",
  6.        command: new AsyncCommand<CancelEventArgs>(
  7.            async cancelArgs => {
  8.                try
  9.                {
  10.                    await MyAsyncExecuteMethod();
  11.                }
  12.                catch (Exception e)
  13.                {
  14.                    AsyncMessageBoxService.ShowMessage(e.Message, "Error", MessageButton.OK);
  15.                    cancelArgs.Cancel = true;
  16.                }
  17.            },
  18.            cancelArgs => !string.IsNullOrEmpty(registrationViewModel.UserName)
  19.        ),
  20.        asyncDisplayMode: AsyncDisplayMode.Wait,
  21.        isDefault: false,
  22.        isCancel: false
  23.    );
  24.     UICommand registerCommand = new UICommand(
  25.         id: null,
  26.         caption: "Register",
  27.         command: new DelegateCommand<CancelEventArgs>(
  28.             cancelArgs => {
  29.                 try
  30.                 {
  31.                     MyExecuteMethod();
  32.                 }
  33.                 catch (Exception e)
  34.                 {
  35.                     AsyncMessageBoxService.ShowMessage(e.Message, "Error", MessageButton.OK);
  36.                     cancelArgs.Cancel = true;
  37.                 }
  38.             },
  39.             cancelArgs => !string.IsNullOrEmpty(registrationViewModel.UserName) && !((IAsyncCommand)registerAsyncCommand.Command).IsExecuting
  40.         ),
  41.         isDefault: true,
  42.         isCancel: false
  43.     );
  44.     UICommand cancelCommand = new UICommand(
  45.         id: MessageBoxResult.Cancel,
  46.         caption: "Cancel",
  47.         command: null,
  48.         isDefault: false,
  49.         isCancel: true
  50.     );
  51.     UICommand result = AsyncDialogService.ShowDialog(
  52.         dialogCommands: new List<UICommand>() { registerAsyncCommand, registerCommand, cancelCommand },
  53.         title: "Registration Dialog",
  54.         viewModel: registrationViewModel
  55.     );
  56.     if (result == registerCommand)
  57.     {
  58.         // ...
  59.     }
  60. }
  61. void MyExecuteMethod()
  62. {
  63.     // ...
  64. }
  65. async Task MyAsyncExecuteMethod()
  66. {
  67.     await Task.Delay(2000);
  68.     // ...
  69. }
复制代码

点击“Register Async”,实行异步方法

参考



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4