2011-03-25 160 views
1

我正在爲我們的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

回答

2

這看起來很不錯,但你也可以做的只是實施你自己的事件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

0

您的代碼似乎是正確的對我來說,有沒有一個具體的問題在這裏的其他然後如果你的代碼是正確的嗎?

請注意,它會顯示MyWorkspaceAddIn的每個實例的彈出窗口。如果你希望彈出窗口只顯示一次應用程序的生命,你應該在不同的類中調用一個靜態變量。