1

I want to use SharpMap in a WPF application, however, I can’t figure out how to use this control. This is what I have tried so far, and the dead-ends I have faced.

Using the WPFExample available on Github: SharpMap on GitHub.

First I tried opening the SharpMap.Sln in VS Community 2017. I am unable to build the application due to the shear number of errors. See screenshot below. I have tried reinstalling all the nuget packages with no change, clean, built etc with no joy. Too many errors to list that mainly revolve around missing namespaces etc.

I then tried opening just the WPFSamples project, again, reinstalling the Nuget packages, and finally adding any other projects that were referenced, however this just made the error list even longer.

I then tried starting a new WPF Application and adding the following Nuget packages:

  1. SharpMaps

  2. SharpMaps.UI

And then manually adding the SharpMaps.UI.Wpf.dll from the samples debug folder on GitHub. The XAML I used is below. There is no code behind. The solution will build with no errors, however, when I try to run it it gives the following error:

System.Windows.Markup.XamlParseException: ''The invocation of the constructor on type 'SharpMap.UI.WPF.SharpMapHost' that matches the specified binding constraints threw an exception.' Line number '30' and line position '10'.' TypeLoadException: Could not load type 'SharpMap.Layers.ILayer' from assembly 'SharpMap, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

So my questions are:

1. What step(s) am I missing that prevents the sample WPF SharpMap project in GitHub from working (I get no errors when I open VS)? Now Solved. Despite using VS2017, it only had .NET 4.6.1 installed, adding .NET 4.7.2 solved the issue. There were no warnings that it was pointing to the wrong framework version so resolving this was just luck.

  1. What am I missing in my code below to get the barebones SharpMap working? Unfortunately, the documentation lacks any WPF support and I can’t find any other examples, so from here on it will be trial and error.

XAML:

<Window x:Class="SharpMap.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:local="clr-namespace:SharpMap"
    xmlns:smh="clr-namespace:SharpMap.UI.WPF;assembly=SharpMap.UI.WPF"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

<Grid Name="grid1">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0" Name="Menu">
        <MenuItem Header="_File">
            <MenuItem Header="_Exit"/>
        </MenuItem>
        <MenuItem Header="_Add Layer">
            <MenuItem Header="_ShapeFile" Name="AddShapeLayer"/>
        </MenuItem>
        <MenuItem Header="_BackgroundMap">
            <MenuItem Header="_OpenStreetMap" Name="BgOsm" />
            <MenuItem Header="_MapQuest" Name="BgMapQuest" />
        </MenuItem>
    </Menu>

    <smh:SharpMapHost Name="WpfMap" Grid.Row="1"></smh:SharpMapHost>
    <StatusBar Grid.Row="2">
        <StatusBarItem Name="Coordinates" HorizontalAlignment="Right">
            <Label Content="{Binding CurrentMouseCoordinateString, ElementName=WpfMap, UpdateSourceTrigger=PropertyChanged}"></Label>
        </StatusBarItem>
    </StatusBar>
</Grid>
</Window>

Just for completeness, the code behind (no changes really)

using System;
using System.Windows;
using System.Windows.Forms;
using SharpMap;
using SharpMap.Forms;
using GeoAPI.Geometries;
using Application = System.Windows.Application;
using MenuItem = System.Windows.Controls.MenuItem;

namespace SharpMap
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


        }

    }
}

EDIT:

I've also tried using MapSui which uses SharpMap and supports WPF however I'm still finding that I can't run the examples on GitHub, nor is the documentation available to build a bare bones example.

EDIT 2:

Ok, so I simply can't get SharpMap to work in my WPF application. Since updating my .NET framework, I have revisited MapSui and founf the examples worked as expected. I then applied MapSui to my project using this simple guide: MapSui Startup Guide and it just worked perfectly. I still think my question deserves an answer for others who want to use WPF and SHarpMap, however, I will continue using MapSui.

4

1 回答 1

0

关于问题 1,我也升级到了 .NET 4.7.2 但它没有改变,我WinformHost的仍然是不可见的。我终于明白为什么会这样了:我的主窗口有一个AllowsTransparency=True参数。这只是阻止WinformHost显示...将其更改为 False 它将起作用。

于 2020-03-11T13:49:05.537 回答