You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ArcGIS C# Silverlight应用地图显示问题及代码修正咨询

解决ArcGIS Silverlight地图无法显示的问题

核心问题

你把Map控件放在Grid.Resources里,这只是定义了资源对象,没有将它加入可视化渲染树,所以地图不会被绘制出来——其他Silverlight组件正常显示,是因为它们直接放在布局容器的子元素列表里。

具体修复步骤

1. 先调整XAML布局(可选,更规范)

如果要保留Resources里的Map定义,在主布局Grid中加一个承载容器,比如ContentControl:

<Grid x:Name="LayoutRoot">
    <!-- 你的其他Silverlight组件 -->
    <ContentControl x:Name="MapContainer" />
</Grid>

2. 后台代码绑定并初始化地图

在MainPage.xaml.cs的构造函数或Loaded事件中,把Resources里的Map实例取出,添加到可视化树,同时确保GraphicsLayer正确配置:

public MainPage()
{
    InitializeComponent();
    Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    // 替换成你在Resources中给Map设置的x:Key
    var map = LayoutRoot.Resources["MyMap"] as ESRI.ArcGIS.Client.Map;
    if (map == null) return;

    // 创建并添加GraphicsLayer
    var graphicsLayer = new GraphicsLayer();
    
    // 添加测试图形验证显示(可选,确认图层生效)
    var testPoint = new MapPoint(-118.15, 33.80, new SpatialReference(4326));
    var markerSymbol = new SimpleMarkerSymbol() 
    { 
        Color = Colors.Red, 
        Size = 12, 
        Style = SimpleMarkerSymbolStyle.Circle 
    };
    var graphic = new Graphic() { Geometry = testPoint, Symbol = markerSymbol };
    graphicsLayer.Graphics.Add(graphic);

    map.Layers.Add(graphicsLayer);
    // 将Map挂载到布局容器
    MapContainer.Content = map;
    // 也可以直接添加到Grid子元素:LayoutRoot.Children.Add(map);
}

3. 关键注意事项

  • 确保Resources里的Map有明确的x:Key,比如<esri:Map x:Key="MyMap" .../>,代码中要对应这个Key
  • 检查ArcGIS Runtime程序集版本与Silverlight兼容,虽然官方停止支持,但要保证引用的dll版本匹配
  • 确认Map的InitialExtent设置合理,范围不对可能导致地图空白
  • 如果GraphicsLayer没有显示,检查符号是否明显——默认符号可能看不清,建议手动指定样式

替代方案:直接后台创建Map

如果不想用Resources定义,直接在后台生成Map并加入布局:

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var map = new ESRI.ArcGIS.Client.Map()
    {
        Extent = new Envelope(-118.2, 33.7, -118.1, 33.9, new SpatialReference(4326))
    };

    var graphicsLayer = new GraphicsLayer();
    // 添加测试图形
    var testPoint = new MapPoint(-118.15, 33.80, new SpatialReference(4326));
    var markerSymbol = new SimpleMarkerSymbol() { Color = Colors.Red, Size = 12 };
    var graphic = new Graphic() { Geometry = testPoint, Symbol = markerSymbol };
    graphicsLayer.Graphics.Add(graphic);

    map.Layers.Add(graphicsLayer);
    LayoutRoot.Children.Add(map);
}

内容的提问来源于stack exchange,提问作者abramhum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:35:01