我正在尝试创建一个简单的 MS Word 插件(主要是为了探索功能)。该插件在功能区中添加了自定义任务窗格和组。功能区控件包括一个用于控制自定义任务窗格可见性的复选框,以及一个用于打开文档的按钮。当我在 MS Word 中测试插件时,任务窗格正确显示,并且复选框正常工作。问题是,只要我单击按钮并打开一个新文档,任务窗格就会隐藏,并且复选框不再控制任务窗格的可见性。出了什么问题?如何保持自定义任务窗格显示?
这是插件的简单版本:
public partial class ThisAddIn
{
private MyUserControl _myUserControl;
private CustomTaskPane _myCustomTastPane;
private OpenFileDialog _dialog;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
_dialog = new OpenFileDialog { Filter = "Doc File (*.rtf)|*.rtf", RestoreDirectory = true };
_myUserControl = new MyUserControl();
_myCustomTastPane = this.CustomTaskPanes.Add(_myUserControl, "My Task Pane");
_myCustomTastPane.Visible = true;
Globals.Ribbons.MyRibbon.ShowPane.Click += ShowClicked;
Globals.Ribbons.MyRibbon.LoadDoc.Click += LoadFile;
}
private void ShowClicked(object sender, RibbonControlEventArgs ribbonControlEventArgs)
{
_myCustomTastPane.Visible = Globals.Ribbons.MyRibbon.ShowPane.Checked;
}
void LoadFile(object sender, RibbonControlEventArgs e)
{
if (_dialog.ShowDialog() != DialogResult.OK) return;
object missing = Missing.Value;
object myFalse = false;
object myTrue = true;
object format = WdSaveFormat.wdFormatRTF;
object fileToOpen = _dialog.FileName;
Application.Documents.Open(ref fileToOpen, ref myFalse, ref myFalse, ref myFalse, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref myTrue, ref myFalse, ref missing, ref missing, ref missing);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
//....
#endregion
}
为了简单起见,我省略了功能区的定义,因为它实际上只是一个按钮和一个复选框。我也省略了 MyUserControl 的定义,因为类的内容并不重要(在我的演示版本中,我只有一个带有标签的简单类)。