仅显示一次消息

时间:2011-03-25 10:19:45

标签: c#

我正在为我们的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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你的代码对我来说似乎是正确的,如果你的代码是正确的,那么除此之外还有一个特定的问题吗?

请注意,它将显示MyWorkspaceAddIn的每个实例的弹出窗口。如果您希望弹出窗口仅显示应用程序生命周期一次,则应在另一个类中调用静态变量。