您的位置:首页 > 其它

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文件代码如下:

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的时候,如果目标文件不存在,接口会自动创建目标文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: