IStyleGallery.AddItem、UpdateItem、RemoveItem用法
2012-08-21 21:53
453 查看
今天做arcengine的符号库管理,做好了符号文件symbol.StyleServer。用到方法AddItem、UpdateItem、RemoveItem。怎么都报错。网上同样问题的人也有一些,就是不见人回答,只有自己琢磨。最终搞定了。写下来,备用。
参考了一篇这样的文章IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口理解.
注意:用AddItem、UpdateItem、RemoveItem之前,必须用到IStyleGalleryStorage接口的TargetFile属性。如果没有设置这个属性,就会报错,错误信息如下:
“对 COM 组件的调用返回了错误 HRESULT E_FAIL。” 错误代码:“-2147467259”
读取ServerStyle文件代码如下:
要想更新,必须用到TargetFile属性。具体为什么必须用这个,原因不明,可能是文件独占。所以只能走一个弯。思路如下:
(1)先把"3D Basic.ServerStyle"备份一个临时文件,备份的文件名称"Temp.ServerStyle"
(2)设置TargetFile,使其指向"Temp.ServerStyle"
(3)开始更新,更新完毕后,拷贝"Temp.ServerStyle"覆盖原来的"3D Basic.ServerStyle"
代码如下(只写数据跟新,不写文件拷贝和覆盖)
参考了一篇这样的文章IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口理解.
注意:用AddItem、UpdateItem、RemoveItem之前,必须用到IStyleGalleryStorage接口的TargetFile属性。如果没有设置这个属性,就会报错,错误信息如下:
“对 COM 组件的调用返回了错误 HRESULT E_FAIL。” 错误代码:“-2147467259”
读取ServerStyle文件代码如下:
private string styleName = "3D Basic.ServerStyle"; public void ReadStyleServer() { IStyleGallery tStyleGallery = new ServerStyleGalleryClass(); IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage; tStyleGalleryStorage.AddFile(styleName); // tStyleGalleryStorage.TargetFile = styleName_TargetFile; IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, ""); tStyleGalleryItems.Reset(); IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next(); int tIndex = 0; try { while (tStyleGalleryItem != null) { string tName = tStyleGalleryItem.Name; tStyleGalleryItem.Name = tName + tIndex; tIndex++; tStyleGallery.UpdateItem(tStyleGalleryItem);//这个地方报错
tStyleGalleryItem = tStyleGalleryItems.Next(); } } catch (System.Runtime.InteropServices.COMException ex) { string tErrorMessage = ex.Message + ex.ErrorCode; } finally { //释放这个接口,不然再次读取时会报错 ReleaseCom(tStyleGalleryItems); ReleaseCom(tStyleGallery); } } private void ReleaseCom(object o) { while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) { } }
要想更新,必须用到TargetFile属性。具体为什么必须用这个,原因不明,可能是文件独占。所以只能走一个弯。思路如下:
(1)先把"3D Basic.ServerStyle"备份一个临时文件,备份的文件名称"Temp.ServerStyle"
(2)设置TargetFile,使其指向"Temp.ServerStyle"
(3)开始更新,更新完毕后,拷贝"Temp.ServerStyle"覆盖原来的"3D Basic.ServerStyle"
代码如下(只写数据跟新,不写文件拷贝和覆盖)
private string styleName_TargetFile = "Temp.ServerStyle"; private string styleName = "3D Basic.ServerStyle"; public void ReadStyleServer() { IStyleGallery tStyleGallery = new ServerStyleGalleryClass(); IStyleGalleryStorage tStyleGalleryStorage = tStyleGallery as IStyleGalleryStorage; tStyleGalleryStorage.AddFile(styleName); tStyleGalleryStorage.TargetFile = styleName_TargetFile; IEnumStyleGalleryItem tStyleGalleryItems = tStyleGallery.get_Items("Marker Symbols", styleName, ""); tStyleGalleryItems.Reset(); IStyleGalleryItem tStyleGalleryItem = tStyleGalleryItems.Next(); int tIndex = 0; try { while (tStyleGalleryItem != null) { string tName = tStyleGalleryItem.Name; tStyleGalleryItem.Name = tName + tIndex; tIndex++; tStyleGallery.UpdateItem(tStyleGalleryItem); tStyleGalleryItem = tStyleGalleryItems.Next(); } } catch (System.Runtime.InteropServices.COMException ex) { string tErrorMessage = ex.Message + ex.ErrorCode; } finally { //释放这个接口,不然再次读取时会报错 ReleaseCom(tStyleGalleryItems); ReleaseCom(tStyleGallery); } } private void ReleaseCom(object o) { while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) { } }
不再报错,顺利运行。AddItem、RemoveItem方法类似。AddItem的时候,如果目标文件不存在,接口会自动创建目标文件。
相关文章推荐
- IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口理解
- IEnumStyleGalleryItem get_Items
- IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口的详解
- IStyleGallery 和IstyleGalleryItem以及IStyleGalleryStorage接口理解
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- MySQL_Style - INSERT ... ON DUPLICATE KEY UPDATE
- ddns-update-style statement error
- WPF ListBox ItemContainerStyle 设置BackGround 和 BorderBrush 无效
- QcomboBox item style
- ?android:attr/spinnerDropDownItemStyle意思
- WPF—Dev的GalleryControl的Item的样式
- flex 3 list控件添加Item的方法【1061: 调用可能未定义的方法 addItem (通过 static 类型 spark.components:List 引用)】
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
- storyboard plain style unsupported in a navigation item
- ListBox Grouping的研究(二):ContainerStyle 与 GroupItem的绑定对象
- MOSS: SPListItem.Update() throws error Operation is not valid due to the current state of the object.
- You can't specify target table 'item' for update in FROM clause
- ItemsControl的ItemContainerStyle属性
- Gallery滑动与Item中Layout点击冲突解决
- 答网友疑问:如何动态设置ItemControl的GroupStyle