【问题标题】:Why does exception in DocData.LoadDocData does not prevent the view from opening?为什么 DocData.LoadDocData 中的异常不会阻止视图打开?
【发布时间】:2016-11-08 21:45:08
【问题描述】:

我正在努力解决这个问题。

我有一个 Visual Studio 包,它注册了一个自定义编辑器工厂来创建自定义文档数据和自定义文档视图。

在 DocData 中,在 LoadDocData 方法中,当我创建文档时,如果正在打开的文件已损坏,则会引发 InvalidOperationException。

这里的问题是我不希望打开相应的视图,但 Visual Studio 显示错误消息但它仍然打开视图。

这里有什么问题?

protected override int LoadDocData(string fileName, bool isReload)
{
    // Clear errors

    this.DocumentData.ClearErrorListItems();

    // Catch errors

    try
    {
        base.LoadDocData(fileName, isReload); // InvalidOperationException raised here
    }
    catch (Exception)
    {
        if (this.DocumentData.ErrorListProvider != null)
        {
            this.DocumentData.ErrorListProvider.ShowErrorOnIdle();
        }

        throw;
    }

    return VSConstants.S_OK;
}

感谢您的帮助!

【问题讨论】:

  • 能否通过 OneDrive 提供一个完整的演示。我们会在我们这边重现您的问题。
  • 我发现了问题。问题是在 DocData.LoadDocData 中有一个循环关闭输入到 ModelingDocView 的文档视图的框架,但我的视图不是 ModelingDocView 的子类。
  • 您可以发布答案并将其标记为答案。

标签: visual-studio-extensions vsix vsx vs-extensibility


【解决方案1】:

这里的问题是我的自定义文档视图不是 ModelingDocView 的子类。

通过使用 ILSpy 分析 DocData.LoadDocData 中的源代码,我发现它只关闭了从 ModelingDocView 派生的视图。

public int LoadDocData(string fileName)
{
    this.codeMarkers.CodeMarker(8206);
    this.OnDocumentLoading(EventArgs.Empty);
    try
    {
        this.LoadDocData(fileName, false);
    }
    catch (Exception ex)
    {
        if (CriticalException.IsCriticalException(ex))
        {
            throw;
        }
        this.HandleLoadDocDataException(fileName, ex, false);
        foreach (ModelingDocView current in new List<ModelingDocView>(this.DocViews))
        {
            if (current != null)
            {
                ErrorHandler.ThrowOnFailure(current.Frame.CloseFrame(65792u));
            }
        }
        return 0;
    }
    this.loaded = true;
    this.OnDocumentLoaded(EventArgs.Empty);
    this.codeMarkers.CodeMarker(8207);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多