c# - 无法正确实现WPF MVVM

标签 c# wpf mvvm arcgis-runtime arcgis-runtime-net

使用带有MVVM模式的ArcGIS Runtime .Net SDK 10.2.7,我正在获取'System.NullReferenceException'。在ViewModel构造函数中的位置:

this.mapView.Map.Layers.Add(localTiledLayer);

我究竟做错了什么?

我所拥有的是:

1- ViewModel.cs
public class ViewModel : INotifyPropertyChanged
{ 
    private Model myModel = null;
    private MapView mapView = null;
    public event PropertyChangedEventHandler PropertyChanged;
    public ViewModel()
    {
        var URI = "D:/GIS/Runtime/Tutorial/VanSchools.tpk";
        ArcGISLocalTiledLayer localTiledLayer = new ArcGISLocalTiledLayer(URI);
        localTiledLayer.ID = "SF Basemap";
        localTiledLayer.InitializeAsync();

        this.mapView.Map.Layers.Add(localTiledLayer);

    }
    public string TilePackage
    {
        get { return this.myModel.TilePackage; }
        set
        {
            this.myModel.TilePackage = value;

        }
    }

2- Model.cs
public class Model
{
    private string tilePackage = "";

    public Model() { }

    public string TilePackage
    {
        get { return this.tilePackage; }
        set
        {
            if (value != this.tilePackage)
            {
                this.tilePackage = value;
            }
        }
    }

3- MainWindow.xaml
<Window x:Class="SimpleMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
        xmlns:local="clr-namespace:SimpleMVVM"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

        <Window.Resources>
            <local:ViewModel x:Key="VM"/>
        </Window.Resources>

        <Grid DataContext="{StaticResource VM}">
            <Grid.RowDefinitions>
                <RowDefinition Height="400" />
                <RowDefinition Height="200" />
            </Grid.RowDefinitions>

            <esri:MapView x:Name="MyMapView" Grid.Row="0"
                      LayerLoaded="MyMapView_LayerLoaded" >
                <esri:Map>  </esri:Map>
            </esri:MapView>

        </Grid>
</Window>

4- MainWindow.xaml.cs
  public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }

            private void MyMapView_LayerLoaded(object sender, LayerLoadedEventArgs e)
            {
                if (e.LoadError == null)
                    return;

                Debug.WriteLine(string.Format("Error while loading layer : {0} - {1}", e.Layer.ID, e.LoadError.Message));
            }

        }

enter image description here

最佳答案

似乎问题在于mapView尚未设置为对象的实例。您是否需要说些类似的话:

this.mapView = new Mapview();
this.mapView.Map.Layers.Add(localTiledLayer);

关于c# - 无法正确实现WPF MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480544/

相关文章:

c# - Geolocation.CivicAddress.City 返回空的 win8 metro 应用程序

c# - 在 C# Windows 应用程序中查找打开的表单

c# - 连接到 Azure 云 Blob 存储失败,证书无效

C# 资源文件 - 如何从 XAML 访问内部资源?

ContextMenu : direct vs. 资源中的 WPF 数据绑定(bind)

c# - 并非所有代码路径都返回一个值 - 枚举实践

wpf - XAML 替代品

c# - Awesomium 网页控制

c# - 来自SQL数据库的DateTime,其值为null

c# - AvalonDock(2.0) + MVVM + VSPackage = 文档选择麻烦?