【问题标题】:AlertControl doesn't show警报控件不显示
【发布时间】:2011-11-13 16:45:18
【问题描述】:

我对 devexpress AlertControl 有一个奇怪的问题。我使用此代码创建警报

 AlertInfo alertInfo = new AlertInfo(caption, text);
    AlertControl control = new AlertControl();
    control.FormLocation = AlertFormLocation.BottomRight;
    control.Show(null,alertInfo);

此代码放置在 backgroundWorker_DoWork 函数中,它应该不时显示警报。问题是没有显示警报。我可以看到调用了 show 方法,但是没有显示警报。 根据文档,我将 null 作为 Show function 的参数传递,通知应显示在主监视器上。 我该怎么做才能让它发挥作用?

【问题讨论】:

    标签: c# winforms devexpress


    【解决方案1】:

    考虑到您使用的是工人,我想这是一个线程问题。尝试将您的代码包装在 Action 对象中:

    Action action = () =>
    {
        AlertControl control = new AlertControl();
        control.FormLocation = AlertFormLocation.BottomRight;
        control.Show(this, alertInfo); // "this" being a Form
    };
    
    this.Invoke(action);
    

    我在表单中使用了类似的代码,效果很好,曾经也使用AlertControl 做了类似的代码。

    【讨论】:

      【解决方案2】:

      您的 AlertControl 需要一个父控件。

      AlertControl control = new AlertControl();
      control.FormLocation = AlertFormLocation.BottomRight;
      control.Show(MyForm,alertInfo); //replace null with a Form/Control instance
      

      您使用 null 参数调用 Show 方法 - 您应该在其中使用 Form/Control 的实例

      【讨论】:

        【解决方案3】:

        对 devexpress 控件一无所知,但也许您必须通过调用方法从主线程显示警报?

        【讨论】:

        • 我不能使用调用方法,因为我没有从表单中显示这个 - 我调用它的类不是控件或表单
        • 您不能从后台线程使用视觉控制。这行不通。
        • @platon 那么我该如何实现我的目标?有什么想法吗?
        • 应该有一个表格。如果您的应用程序是 WindowsForms 应用程序,并且它是,那么其中应该有主窗体。
        【解决方案4】:
        using DevExpress.XtraBars.Alerter;
        
        // Create a regular custom button.
        AlertButton btn1 = new AlertButton(Image.FromFile(@"c:\folder-16x16.png"));
        btn1.Hint = "Open file";
        btn1.Name = "buttonOpen";
        // Create a check custom button.
        AlertButton btn2 = new AlertButton(Image.FromFile(@"c:\clock-16x16.png"));
        btn2.Style = AlertButtonStyle.CheckButton;
        btn2.Down = true;
        btn2.Hint = "Alert On";
        btn2.Name = "buttonAlert";
        // Add buttons to the AlertControl and subscribe to the events to process button clicks
        alertControl1.Buttons.Add(btn1);
        alertControl1.Buttons.Add(btn2);
        alertControl1.ButtonClick += new AlertButtonClickEventHandler(alertControl1_ButtonClick);
        alertControl1.ButtonDownChanged += 
            new AlertButtonDownChangedEventHandler(alertControl1_ButtonDownChanged);
        
        // Show a sample alert window.
        AlertInfo info = new AlertInfo("New Window", "Text");
        alertControl1.Show(this, info);
        
        void alertControl1_ButtonDownChanged(object sender, 
        AlertButtonDownChangedEventArgs e) {
            if (e.ButtonName == "buttonOpen") {
                //...
            }
        }
        
        void alertControl1_ButtonClick(object sender, AlertButtonClickEventArgs e) {
            if (e.ButtonName == "buttonAlert") {
                //...
            }
        }
        

        参考:https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraBarsAlerterAlertControltopic

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-17
          • 1970-01-01
          • 2019-10-18
          • 1970-01-01
          • 2015-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多