C# 无法正确实现WPF MVVM
使用ArcGIS Runtime.NETSDK10.2.7和MVVM模式,我得到了C# 无法正确实现WPF MVVM,c#,wpf,mvvm,arcgis-runtime,arcgis-runtime-net,C#,Wpf,Mvvm,Arcgis Runtime,Arcgis Runtime Net,使用ArcGIS Runtime.NETSDK10.2.7和MVVM模式,我得到了“System.NullReferenceException”。在ViewModel构造函数中: this.mapView.Map.Layers.Add(localTiledLayer); 我做错了什么 我得到的是: 1-ViewModel.cs public class ViewModel : INotifyPropertyChanged { private Model myModel = null;
“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;
}
}
public class Model
{
private string tilePackage = "";
public Model() { }
public string TilePackage
{
get { return this.tilePackage; }
set
{
if (value != this.tilePackage)
{
this.tilePackage = value;
}
}
}
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));
}
}
2-Model.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;
}
}
public class Model
{
private string tilePackage = "";
public Model() { }
public string TilePackage
{
get { return this.tilePackage; }
set
{
if (value != this.tilePackage)
{
this.tilePackage = value;
}
}
}
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));
}
}
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>
问题似乎在于
mapView
未设置为对象的实例。你需要说些什么吗
this.mapView = new Mapview();
this.mapView.Map.Layers.Add(localTiledLayer);
问题似乎在于
mapView
尚未设置为对象的实例。你需要说些什么吗
this.mapView = new Mapview();
this.mapView.Map.Layers.Add(localTiledLayer);
看起来您没有连接到绑定,但一般来说,您不应该从ViewModel中引用MapView,而应该绑定MapView.Map<代码>地图被视为定义地图视图外观的模型 在您的视图中,确保您具有绑定设置
<Window.Resources>
<vm:MainViewModel x:Key="vm"></vm:MainViewModel>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<esri:MapView x:Name="MyMapView"
Map="{Binding Map}"
LayerLoaded="MyMapView_LayerLoaded">
</esri:MapView>
</Grid>
看起来您没有连接到绑定,但一般来说,您不应该从ViewModel中引用MapView,而应该绑定MapView.Map<代码>地图被视为定义地图视图外观的模型 在您的视图中,确保您具有绑定设置
<Window.Resources>
<vm:MainViewModel x:Key="vm"></vm:MainViewModel>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<esri:MapView x:Name="MyMapView"
Map="{Binding Map}"
LayerLoaded="MyMapView_LayerLoaded">
</esri:MapView>
</Grid>
视图模型不应包含对mapView(或任何其他视觉元素)的任何引用。这打破了MVVM模式。取而代之的是,您的viewmodel应该只包含贴图,并将贴图绑定到XAML中viewmodel的map属性。更新:刚刚意识到这正是安蒂在下面写的。这是mvvm的“正确”方法。视图模型不应包含对mapView(或任何其他可视元素)的任何引用。这打破了MVVM模式。取而代之的是,您的viewmodel应该只包含贴图,并将贴图绑定到XAML中viewmodel的map属性。更新:刚刚意识到这正是安蒂在下面写的。这是mvvm的“正确”方法