VSTO Visio 加载项 SolutionXmlElement 错误

VSTO Visio Add-In SolutionXmlElement error

我正在为 Visio 开发 VSTO 加载项,需要使用 Document.SolutionXmlElement 属性 保存文档属性。以下代码演示了在保存之前为对象序列化 XML 的处理程序。根据阅读,我将 C# class 实例包装在 SolutionXml 包装器中,并将 Name 属性设置为已公开序列化 class 实例的元素名称:

        private void Document_Saved(Visio.Document document)
        {
        IDictionary<string, ModelGenMVCModel> documentModel = GetRibbon().documentModel;
        if (documentModel.TryGetValue(document.Name, out ModelGenMVCModel modelGenMVCModel))
        {
            try
            {
                string serializedModel = new SolutionXml(modelGenMVCModel,ModelGenVisioAddIn.Properties.Resources.SolutionXmlElementName).Serialize();
                LOGGER.Debug(String.Format("Saving model gen properties:\n{0}", serializedModel));
                document.SolutionXMLElement[ModelGenVisioAddIn.Properties.Resources.SolutionXmlElementName] = serializedModel;
            } catch ( Exception e)
            {
                LOGGER.Error(String.Format("Serialization error {0}\n{1}", e.Message, e.StackTrace));
            }
        }
    }

序列化后XML如下:

<?xml version="1.0" encoding="utf-16"?>
<SolutionXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="ModelGenMVCModel">
    <ModelGenMVCModel>
        <Author>Andrew Tyson</Author>
        <UniqueID>Object_ID</UniqueID>
        <Name>Name</Name>
        <Description>Note</Description>
        <LineColor/>
        <FillColor/>
        <SearchMatchLineColor/>
        <SearchMatchFillColor/>
        <SearchMatchFontColor/>
        <SearchNoMatchLineColor/>
        <SearchNoMatchFillColor/>
        <LineWidth>1.5</LineWidth>
        <PopupHeight>250</PopupHeight>
        <PopupWidth>750</PopupWidth>
        <InitialZoom>2</InitialZoom>
        <MinZoom>0.05</MinZoom>
        <MaxZoom>2</MaxZoom>
        <ZoomSteps>0.05</ZoomSteps>
        <EnablePan>true</EnablePan>
        <EnableSearch>true</EnableSearch>
        <DragPan>true</DragPan>
        <MouseWheelZoom>true</MouseWheelZoom>
        <DoubleClickZoom>true</DoubleClickZoom>
        <PanDuration>300</PanDuration>
        <PanAmount>100</PanAmount>
        <OpenInBrowser>true</OpenInBrowser>
        <SaveToLocation/>
    </ModelGenMVCModel>
</SolutionXml>

但是我收到 "Invalid Parameter" 异常抛出。如有任何帮助,我们将不胜感激。

感谢和问候 安德鲁

我这里有一个代码示例,演示如何管理 SolutionXmlElement。你可以检查一下,如果它适合你。它定义了一个简单的 class 到 save/load 设置:

https://unmanagedvisio.com/using-solutionxml-with-c/

我相信,问题可能是,您实际上需要将 XML 放在那里,而不是序列化的字符串。但是如果不看完整的例子很难说。