学习010-05-03 How to: Implement a Custom Messaging Class(如何:实现自定义消息类)

How to: Implement a Custom Messaging Class(如何:实现自定义消息类)

Message boxes that contain text and several buttons are be displayed in Windows Forms XAF applications in many situations. For instance, a message box is shown when an end-user closes a Detail View with modified data or cancels changes via the ModificationsController.CancelAction Action. These message boxes are provided by the Messaging class.
在许多情况下,Windows窗体XAF应用程序中会显示包含文本和多个按钮的消息框。例如,当最终用户关闭带有修改数据的详细信息视图或通过ModificationsController取消更改时,会显示一个消息框。取消操作操作。这些消息框由Messaging类提供。
在这里插入图片描述

This class utilizes the XtraMessageBox functionality. In certain scenarios it can be required to customize the Messaging class. In this instance you should inherit this class. This topic details a way of implementing such a descendant.
此类使用XtraMessageBox功能。在某些情况下,可能需要自定义Messaging类。在这种情况下,您应该继承此类。本主题详细介绍了实现此类后代的方法。

Note
You can handle the Messaging.ConfirmationDialogClosed event instead of implementing a custom Messaging class, if executing a custom code after upon the closing of a message box is required.
如果需要在关闭消息框后执行自定义代码,则可以处理Messaging.ConfirmationDialogClosed事件,而不是实现自定义Messaging类。

Change the Default Focused Button(更改默认焦点按钮)

The Messaging class exposes the Messaging.Show virtual method, which displays a message box via the XtraMessageBox.Show method. The XtraMessageBox.Show overload that takes owner, text, caption, buttons and icon parameters is used by default. This overload displays a message box with the first (“OK”) button initially focused. In this example, we will override the Messaging.Show method and use another XtraMessageBox.Show method’s overload that takes the defaultButton extra parameter. Thus, it will be possible to specify which button to focus.
Messaging类公开了Messaging.Show的虚拟方法,该方法通过XtraMessageBox.Show方法显示消息框。默认使用XtraMessageBox.Show重载,接受所有者、文本、标题、按钮和图标参数。此重载显示最初聚焦的第一个(“ OK”)按钮的消息框。在本例中,我们将覆盖Messaging.Show方法,并使用另一个XtraMessageBox.Show方法的重载,该方法接受defaultButton额外参数。因此,可以指定聚焦哪个按钮。

Add a new CustomXtraMessageBoxMessaging class to the WinForms module project (MySolution.Module.Win). If your solution does not contain this project, add this class to the WinForms application project (MySolution.Win). This class should inherit the Messaging class. Add the default public constructor and override the ShowCore method as it is shown in the snippet below.
将新的CustomXtraMessageBoxMessaging类添加到WinForms模块项目(MySolution.Module.Win)。如果您的解决方案不包含此项目,请将此类添加到WinForms应用程序项目(MySolution.Win)。此类应继承Messaging类。添加默认的公共构造函数并覆盖ShowCore方法,如下面的代码片段所示。

C#
using System.Windows.Forms;
using DevExpress.ExpressApp;
using DevExpress.XtraEditors;
using DevExpress.ExpressApp.Win.Core;
// ...
public class CustomXtraMessageBoxMessaging : Messaging {
    public CustomXtraMessageBoxMessaging(XafApplication application)
        : base(application) { }
    protected override DialogResult ShowCore(
        string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) {
        return XtraMessageBox.Show(
           message, caption, buttons, icon, MessageBoxDefaultButton.Button2);
    }
}

Invoke the Model Editor for the Windows forms application project and navigate to the Options node. The IModelOptionsWin.Messaging property specifies the type of messaging used in Windows Forms application. Set this property value to the fully qualified name of your custom class (e.g. “CustomMessaging.Win.CustomXtraMessageBoxMessaging”).
调用Windows窗体应用程序项目的模型编辑器并导航到选项节点。IModelOptionsWin. Messaging属性指定Windows窗体应用程序中使用的消息传递类型。将此属性值设置为自定义类的完全限定名称(例如CustomMessaging.Win.CustomXtraMessageBoxMessaging)。

在这里插入图片描述

Run the Windows Forms application. Open any persistent object’s Detail View and make some changes. Click Close. The message box with “No” button focused will be displayed.
运行Windows窗体应用程序。打开任何持久对象的详细信息视图并进行一些更改。单击关闭。将显示带有“No”按钮的消息框。
在这里插入图片描述

Use a custom Message Box instead of XtraMessageBox(使用自定义消息框而不是XtraMessageBox)

You can use a fully custom message box in your Messaging implementation. In the snippet below, a use of System.Windows.Forms.MessageBox class is demonstrated.
可以在Messaging实现中使用完全自定义的消息框。

C#
using System.Windows.Forms;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Win.Core;
// ...
public class CustomWinFormsMessageBoxMessaging : Messaging {
    public CustomWinFormsMessageBoxMessaging(XafApplication application)
        : base(application) { }
    protected override DialogResult ShowCore(
        string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) {
        return MessageBox.Show(message, caption, buttons, icon);
    }
}

Invoke the Model Editor for the Windows forms application project. Navigate to the Options node. Set the IModelOptionsWin.Messaging property to the fully qualified name of your custom class (e.g. “CustomMessaging.Win.CustomWinFormsMessageBoxMessaging”).
为Windows窗体应用程序项目调用模型编辑器。导航到选项节点。将IModelOptionsWin. Messaging属性设置为自定义类的完全限定名称(例如CustomMessaging.WinCustomWinFormsMessageBoxMessaging)。

Run the Windows Forms application. Open any persistent object’s Detail View and make some changes. Click Close. The following message box will be displayed.
运行Windows窗体应用程序。打开任何持久对象的详细信息视图并进行一些更改。单击关闭。将显示以下消息框。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值