只显示消息一次

问题描述:

我正在为我们的CRM系统构建自定义添加项。当最终用户符合某些条件时,我只想弹出一个消息框。我不知道我是否在后续的实施做正确:只显示消息一次

我宣布在全球范围内的触发变量:

public class MyWorkspaceAddIn : Panel, IWorkspaceComponent2 
    { 

     private bool _readOnly; 
     private IRecordContext _recordContext; 
     private IGlobalContext _globalContext; 
     private bool _triggerPopup = true; 

的弹出代码在下面的类:

void _incident_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     {//check property and/or i.Prod and then show the popup 

      // MessageBox.Show(e.PropertyName); //Check what property name is 

      if (_incident.ProductID == 182 && _triggerPopup) //If product ID is 183 in incident 
      { 
       MessageBox.Show("The GO Classic will soon be end of life, make sure you propose the customer to buy a new device and offer maximum 20% discount to reward his/her loyalty (NOTE: refurbished devices are not included in this offer)."); 
       _triggerPopup = false; //Do not pop up 
      } 

我希望消息框只会弹出一次。

全码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.AddIn; 
using RightNow.AddIns.AddInViews; 
using RightNow.AddIns.Common; 
using System.Windows.Forms; 

//////////////////////////////////////////////////////////////////////////////// 
// 
// File: MyWorkspaceAddIn.cs 
// 
// Comments: 
// 
// Notes: 
// 
// Pre-Conditions: 
// 
//////////////////////////////////////////////////////////////////////////////// 
namespace TriggerAddIn 
{ 
    [AddIn("My VAS AddIn", Version = "1.1.0.2")] 
    public class MyWorkspaceAddIn : Panel, IWorkspaceComponent2 
    { 

     private bool _readOnly; 
     private IRecordContext _recordContext; 
     private IGlobalContext _globalContext; 
     private bool _triggerPopup = true; 

     IIncident _incident; //Define IIncident outside dataLoaded event; 
     //Get reference when the incident open in workspace. 
     public MyWorkspaceAddIn(bool inDesignMode, IRecordContext recContext, IGlobalContext globalContext) 
     { 
      /*MessageBox.Show("AddIns Load My workspace plugin");*/ 

      _recordContext = recContext; 
      _globalContext = globalContext; 
      //Check wheather users 
      if (!inDesignMode &&_recordContext != null) 
      { //Add our custom event 
       _recordContext.DataLoaded += new EventHandler(_recordContext_DataLoaded); 
      } 
     } 
     //Custom Event handler 
     void _recordContext_DataLoaded(object sender, EventArgs e) 
     { 
      _incident = _recordContext.GetWorkspaceRecord(WorkspaceRecordType.Incident) as IIncident; 

      if (_incident != null) 
      { 
       _incident.PropertyChanged -= _incident_PropertyChanged; 
       _incident.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_incident_PropertyChanged); 
      } 
     } 
     void _incident_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
     {//check property and/or i.Prod and then show the popup 

      // MessageBox.Show(e.PropertyName); //Check what property name is 

      if (_incident.ProductID == 182 && _triggerPopup) //If product ID is 183 in incident 
      { 
       MessageBox.Show("The GO Classic will soon be end of life, make sure you propose the customer to buy a new device and offer maximum 20% discount to reward his/her loyalty (NOTE: refurbished devices are not included in this offer)."); 
       _triggerPopup = false; //Do not pop up 
      } 
      /* 
      if (_incident.CategoryID == 2353) 
      { 
       Form1 myForm = new Form1(); 
       myForm.ShowDialog(); 
      }*/ 
     } 
     #region IAddInControl Members 

     public Control GetControl() 
     { 
      return this; 
     } 

     #endregion 

     #region IWorkspaceComponent2 Members 

     public bool ReadOnly 
     { 
      get 
      { 
       return _readOnly; 
      } 
      set 
      { 
       _readOnly = value; 
      } 
     } 

     public void RuleActionInvoked(string actionName) 
     { 

     } 

     public string RuleConditionInvoked(string conditionName) 
     { 
      return ""; 
     } 

     #endregion 
    } 

    [AddIn("My VAS Factory AddIn", Version = "1.1.0.2")] 
    public class MyWorkspaceAddInFactory : IWorkspaceComponentFactory2 
    { 

     private IGlobalContext _globalContext; 

     #region IWorkspaceComponentFactory2 Members 

     IWorkspaceComponent2 IWorkspaceComponentFactory2.CreateControl(bool inDesignMode, IRecordContext context) 
     { 
      return new MyWorkspaceAddIn(inDesignMode, context, _globalContext); 
     } 

     #endregion 

     #region IFactoryBase Members 

     public System.Drawing.Image Image16 
     { 
      get { return Properties.Resources.AddIn16; } 
     } 

     public string Text 
     { 
      get { return "Trigger add in for VAS"; } 
     } 

     public string Tooltip 
     { 
      get { return "Trigger add in for VAS Tooltip"; } 
     } 

     #endregion 

     #region IAddInBase Members 

     public bool Initialize(IGlobalContext context) 
     { 
      _globalContext = context; 

      return true; 
     } 

     #endregion 

    } 
} 
+2

所以,我猜你已经试过了。发生了什么? – 2011-03-25 10:21:56

+1

这是什么问题?你有没有尝试过这些代码?它工作吗? – 2011-03-25 10:22:15

这看起来很不错,但你也可以做的只是实施你自己的事件PropertyChanged,它会触发一次,例如。 PropertyFirstChanged然后您将避免额外的代码调用。

+0

这是如何避免额外的代码调用?当您确定要举办哪个活动时,您仍然需要进行检查。 – 2011-03-25 10:38:49

+0

您将不会有方法调用,因为事件只会触发一次。当然,你需要确定事件发生的原因,但是因为它会被移到责任级别上,所以事情不会像现在这样被解雇。 – 2011-03-25 10:47:03

+0

嗨帕维尔和科迪,感谢您的快速回复。我认为很难自定义PropertyChanged,因为事件是加载一次。我将在我的原始文章中分享所有代码。 – QLiu 2011-03-25 10:54:44

您的代码似乎是正确的对我来说,有没有一个具体的问题在这里的其他然后如果你的代码是正确的吗?

请注意,它会显示MyWorkspaceAddIn的每个实例的弹出窗口。如果你希望弹出窗口只显示一次应用程序的生命,你应该在不同的类中调用一个静态变量。