C# 只显示一次消息
我正在为我们的CRM系统构建一个定制的外接程序。当最终用户满足某些条件时,我只想弹出一次消息框。我不确定我在以下实施中是否正确: 我在全局范围内声明触发器变量:C# 只显示一次消息,c#,C#,我正在为我们的CRM系统构建一个定制的外接程序。当最终用户满足某些条件时,我只想弹出一次消息框。我不确定我在以下实施中是否正确: 我在全局范围内声明触发器变量: public class MyWorkspaceAddIn : Panel, IWorkspaceComponent2 { private bool _readOnly; private IRecordContext _recordContext; private IGlobal
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
}
}
你的代码对我来说似乎是正确的,如果你的代码是正确的,这里还有一个特别的问题吗
请注意,它将显示MyWorkspaceAddIn的每个实例的弹出窗口。如果您希望弹出窗口在应用程序生命周期内仅显示一次,则应在不同的类中调用静态变量。这看起来不错,但您也可以在
PropertyChanged
上实现您自己的事件,该事件将触发一次,例如PropertyFirstChanged
,然后您将避免额外的代码调用。,我想你已经试过了。发生什么事了?问题是什么?你试过密码了吗?它能工作吗?这如何避免额外的代码调用?当您确定要引发哪个事件时,仍然必须进行检查。您将不会进行方法调用,因为事件将只触发一次。当然,您需要确定事件是否会上升,但因为它将被移动到更高的责任级别,所以事件不会在不需要时触发,就像现在一样。嗨,Pawel和Cody,感谢您的快速回复。我认为很难自定义PropertyChanged,因为事件只加载一次。我将在我的原始帖子中分享所有代码。你的代码很好,我只是分享我的想法如何实现这一点。你介意为我把你的想法写进一些代码片段吗?永远好向大师学习:)