【问题标题】:Error "Invalid at the top level of the document" creating a Visual Studio 2012 Wizard创建 Visual Studio 2012 向导时出现“文档顶层无效”错误
【发布时间】:2014-06-12 16:01:48
【问题描述】:

我正在尝试为 Visual Studio 2012 创建一个新项目向导。这将是一个创建多个 C++ 文件并将它们添加到项目中的向导。我已经为 VS 2005 创建了一个类似的向导。我正在尝试使用 IDTWizard 界面创建它。我按照这个演练:http://msdn.microsoft.com/en-us/library/7k3w6w59(v=vs.110).aspx

当我尝试使用该向导时,我收到一条错误消息,显示“在文档的顶层无效”。当我查看 oleview.exe 显示的数据时,我的 dll 显示为已注册的 COM 对象。我究竟做错了什么?我是否应该像使用 html/javascript 创建 VS2005 向导一样执行此操作?我的代码如下:

Wizard.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SystemsModuleWizard
{
    [ComVisible(true)]
    [Guid("FE7DC545-30DC-445B-8130-897C5F3114EC"), ProgId("SystemsModuleWizard.WizardClass")]

    public class WizardClass : IDTWizard
    {
        public void Execute(object Application,
            int hwndOwner, ref object[] contextParams,
            ref object[] customParams,
            ref EnvDTE.wizardResult retval) 
        {
            MessageBox.Show("The wizard is running");

        }
    }
}

AssemblyInfo.cs:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SystemsModuleWizard")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SystemsModuleWizard")]
[assembly: AssemblyCopyright("Copyright ©  2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ab29b7df-32c4-4dca-ade4-a31f687b9331")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

【问题讨论】:

  • 这是由 XML 解析器生成的错误消息。远离您的代码 sn-ps。

标签: c# visual-studio-2012 wizard


【解决方案1】:

我注意到您的Execute 实现是:

public void Execute(object Application,
        int hwndOwner, ref object[] contextParams,
        ref object[] customParams,
        ref EnvDTE.wizardResult retval) 
    {
        MessageBox.Show("The wizard is running");

    }

但是根据this MSDN page,retval是out参数,不是ref参数:

void Execute(
    Object Application,
    int hwndOwner,
    ref Object[] ContextParams,
    ref Object[] CustomParams,
    out wizardResult retval
)

您链接到的演练肯定将其命名为ref,但也许这是一个错误。我看不出两者是如何工作的——内存分配和编组会有所不同。试试看。希望这会有所帮助。

【讨论】:

  • 不,这只对 C# 编译器很重要,它的明确赋值检查 功能取决于它。它与 COM 无关。
【解决方案2】:

我对此很感兴趣,所以我按照您链接的演练中的步骤启动了一个快速向导。但效果很好:

(事实证明ref 或out 参数的问题不会出现,因为它甚至不会与out 一起编译。所以that other MSDN page 将其作为out 是完全错误的.)

所以我开始研究如何破解它。

方法如下:将错误引入.vsz 文件,.例如,如果您的文件包含以下行

VSWIZARD 7.0
Wizard=MyNewWizard.Class1
Param=First Item
Param=Second Item

第二行的类名拼错了:

Wizard=ThyNewWizard.Class1

当我这样做时,它很乐意显示您的错误消息:

由于您已检查您的课程是否已向 COM 注册,因此它可能是您的 .vsz 文件中的拼写错误等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2015-06-28
    • 1970-01-01
    • 2017-04-28
    • 2015-03-24
    相关资源
    最近更新 更多