简体   繁体   English

覆盖MessageBox.Show

[英]Override MessageBox.Show

I am seeking to override the System.Windows.Forms.MessageBox.Show() method to save modifying a monolithic codebase to use a new messagebox function. 我正在尝试重写System.Windows.Forms.MessageBox.Show()方法,以保存对整体代码库的修改,以使用新的消息框功能。

I am using the Citrix Mobility SDK to instrument a legacy C# Windows application to facilitate more intuitive functionality on a mobile device such as iPad or Nexus tablet. 我正在使用Citrix Mobility SDK来检测旧版C#Windows应用程序,以在iPad或Nexus平板电脑等移动设备上实现更直观的功能。

I wish to display a local messagebox on the device, and to do so, I need to replace all calls to MessageBox.Show in the application with a CustomMessageBox.Show function. 我希望在设备上显示本地消息框,为此,我需要使用CustomMessageBox.Show函数替换应用程序中对MessageBox.Show所有调用。 The codebase is enormous, and I would rather not modify every file to use CustomMessageBox.Show as it would be a large and dangerous job, as well as resulting in tight coupling between the SDK and the application. 代码库非常庞大,我不希望每个文件都修改为使用CustomMessageBox.Show因为这将是一项庞大而危险的工作,并且会导致SDK与应用程序之间的紧密耦合。

Thus, is there a way to override MessageBox.Show so that I can reimplement the function for use with this third party SDK, as it would be the most elegant option? 因此,有没有一种方法可以重写MessageBox.Show以便我可以重新实现该函数以供此第三方SDK使用,因为这将是最优雅的选择?

不,您不能覆盖MessageBox.Show因为它是static方法。

Given that you cannot override MessageBox.Show , there is still a solution that would allow it to work for mobile and non-mobile cases. 鉴于您无法覆盖MessageBox.Show ,仍然存在一种解决方案,可以使它适用于移动和非移动案例。 The easiest way is to create a class that has members that mimic what MessageBox.Show can allow. 最简单的方法是创建一个类,该类的成员模仿MessageBox.Show可以允许的内容。 In this class, it is aware of whether or not mobile is active. 在此类中,它知道移动设备是否处于活动状态。 If it is not active, then it uses MessageBox.Show . 如果未激活,则使用MessageBox.Show Otherwise it uses CustomMessageBox.Show . 否则,它将使用CustomMessageBox.Show Here is an example member function I have used for error messages in a recent sample. 这是我在最近的示例中用于错误消息的示例成员函数。

/// <summary>
/// Show a error dialog box
/// </summary>
/// <param name="errorDialogText"></param>
public void ShowErrorDialog(String errorDialogText)
{
    if (!Visible || !IsMobile())
    {
        ErrorDialog.Show(errorDialogText);
    }
    else
    {
        ShowStatus(errorDialogText);
    }
}

ErrorDialog is a wrapper class for MessageBox. ErrorDialog是MessageBox的包装器类。 Note that IsMobile() knows that the mobile device is present or not. 请注意, IsMobile()知道移动设备是否存在。

public class ErrorDialog
{
    //
    // When an exception happens, we show the message here
    //
    public static void Show(String text)
    {
        MessageBox.Show(text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    //
    // When an exception happens, we show the message here
    //
    public static void Show(Exception ex)
    {
        MessageBox.Show(ex.GetType().ToString() + ":" + ex.Message + ex.Source, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        MessageBox.Show(ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

}

This is just abstraction. 这仅仅是抽象。 By creating a new function, you can wrap the two other functions. 通过创建一个新函数,您可以包装另外两个函数。 Inside the wrapper function, you can decide which one will be called. 在包装函数内部,您可以决定将调用哪一个。 You will still have the pain of updating all the MessageBox.Show references but at least it will be under your control and you could change the abstracted function at any time in the future for whatever reason. 您仍然会很痛苦地更新所有MessageBox.Show引用,但至少它将在您的控制之下,并且您可以出于任何原因在将来的任何时候更改抽象函数。

My example does not exactly match what you describe. 我的示例与您描述的不完全匹配。 Either you could create the function in the derived Form class or you could create an object and call the function from that object. 您可以在派生的Form类中创建函数,也可以创建一个对象并从该对象中调用该函数。

I work on the SDK at Citrix and am happy to help. 我在Citrix上使用SDK,很高兴能为您提供帮助。 There are a number of new posts on http://citrixblogger.org that might help out on other issues. http://citrixblogger.org上有许多新帖子可能会对其他问题有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM