为什么字典计数在单词addin中返回0

为什么字典计数在单词addin中返回0

本文介绍了为什么字典计数在单词addin中返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从单词Addin 之外传递了一些值,并将其保存到字典中,以便以后在用户想要保存单词文档时使用。

I am passing some values from outside the word Addin and save it to the dictionary for later use when the user wants to save the word document.

以下是代码

[ComVisible(true)]
    [Guid("229CCF26-CECF-4708-8F44-561FB1655641")]
    [ClassInterface(ClassInterfaceType.None)]
    public class AddinUtilities : StandardOleMarshalObject , IAddinUtilities
    {


        private int clipCount = 0;
        private int maxClipCount = 0;


        public void SetClipData(Temp dataManager)
        {
            addClipElements(dataManager);

        }

        private void addClipElements(Temp dataManager)
        {
            Temp.Clip.Add(clipCount++,dataManager);
        }

        public void ClearClipData()
        {
            Temp.Clip.Clear();
        }

        public void SetClipCount(int count)
        {
            maxClipCount = count;
        }
    }

以下是临时类

  [Serializable]
    public class Temp
    {
        private string copiedText;

        [DisplayName("CopiedText")]
        public string CopiedText
        {
            get
            {
                return copiedText;
            }
            set
            {
                copiedText = value;
            }
        }

        private string time;

        [DisplayName("Time")]
        public string Time
        {
            get
            {
                return time;
            }
            set
            {
                time = value;
            }
        }

        public Temp( string _copiedText, string _copiedTime)
        {
            this.copiedText = _copiedText;
            this.time = DateTime.Now.ToString("T");
        }

        private static Dictionary<int, Temp> clip = new Dictionary<int, Temp>();

        public static Dictionary<int, Temp> Clip
        {
            get
            {
                return clip;
            }
            set
            {
                clip = value;
            }
        }

        public Dictionary<int, Temp> ReturnData()
        {
            return Clip;
        }
    }

这是我从Addin这个词外面传递数据的方法

This is how I pass data from outside the word Addin

word = new Microsoft.Office.Interop.Word.Application();
object addinName = "WordAddInClip";
COMAddIns comAddins = word.COMAddIns;

COMAddIn comAddin = comAddins.Item(addinName);
WordAddInClip.IAddinUtilities comObj = (WordAddInClip.IAddinUtilities)comAddin.Object;


while (comObj == null)
    {
       comObj = (WordAddInClip.IAddinUtilities)comAddin.Object;
       System.Threading.Thread.Sleep(1000);
    }


AddinDataMgr = new WordAddInClip.Temp(dataMgr.CopiedText, dataMgr.Time);
comObj.SetClipData(AddinDataMgr);

它正确添加数据,因为它增加计数器的字典。

It adds data correctly because it increments the counter ow the Dictionary.

但当我在ThisAddin.cs类中调用Dictionary时,如下所示,它将计数显示为0

But when I call the Dictionary inside ThisAddin.cs class like below it shows the count as 0

void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
{

MessageBox.Show("clip count " + Temp.Clip.Count.ToString());

}

可能是什么问题?

推荐答案

尝试传递一个简单的变量,如整数而不是自定义类。在这种情况下它是否正常工作?

Try to pass a simple variable like integer instead of custom classes. Does it work correctly in that case?


这篇关于为什么字典计数在单词addin中返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:02