Saturday, September 24, 2011

How to set up ArcGIS for WPF with Prism.

Got a question on the Testing ArcGIS for WPF + Funbeat + Prism. blog post if I could share the code. Sure thing! I've added a project for the sample code on google projects hosting https://code.google.com/p/arcgis-samples/ . I've removed the Funbeat APIs from the solution because it only adds to the complexity and probably only make sense for people in Sweden. 

Here is a step by step description on how I set up the project:
1. Shell app
Create a Wpf Application

1.1 Install Prism using NuGet
Use NuGet  to install microsoft Prism dependencies into the project:
Install-Package prism

1.1.1 Install Extensions using NuGet
Do the same for the extension to be used to load modules:

Install-Package Prism.UnityExtensions
Or
Install-Package Prism.MefExtensions

1.2 Rename MainWindow
Rename MainWindow to Shell both filename and class name, use refactor to rename class so that the partial classes get renamed as well.


1.3 Add region in Shell.xaml
xmlns:prism="http://www.codeplex.com/prism"
 
           
               
                   
               

           

       

1.4 Add Bootstrapper
Add a class Bootstrapper.cs to the project:


Add using:


using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
The bootstrapper should inherit from the UnitBootstrapper if it is the Unity extension you are using:
: UnityBootstrapper
 
 
 
Override methods as shown below:
 
protected override DependencyObject CreateShell()
{
    return new Shell();
}
protected override void InitializeShell()
{
    base.InitializeShell();
    App.Current.MainWindow = (Window)this.Shell;
    App.Current.MainWindow.Show();
}
protected override void ConfigureModuleCatalog()
{
    base.ConfigureModuleCatalog();
}

1.5 App.cs
Change the App.cs so that the Bootstrapper gets called:
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
1.6 App.xaml
The bootstrapper is now doing the job remove StartupUri in App.xaml:
StartupUri="MainWindow.xaml"

2. Module
Create a new project for the MapModule.
2.1 Install Prism using NuGet
Install-Package prism
2.1.1 Extensions
Install-Package Prism.UnityExtensions
Or
Install-Package Prism.MefExtensions
2.2 Add references
PresentationCore.dll
PresentationFramework.dll
WindowsBase.dll
System.Xaml.dll
2.3 Rename Class1

Class1 to MapModule
2.4 Add code to MapModule
Add using:

using Microsoft.Practices.Prism.Modularity;
 
Let MapModule inherit IModule:
: IModule
public void Initialize()
{
}
2.4.1 Solutions folders
Add Solution folders:
  • Model
  • Services. In this folder, you store service implementations and service interfaces.
  • ViewModels. In this folder, you store view models.
  • Views. In this folder you add your views.
2.5 Connect the Module with Shell
2.5.1 Add reference
In Shell project add areference to the Module project
2.5.2 Change code in Bootstrapper
Change the code in the bootstrapper so the Module get loaded:

ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(Module.Module));


3. Add View

3.1 Add xaml

3.1.1 View
Before we can add our map we need to add some references to the ArcGIS for WPF:
Add some esri elements to the view so we can see if it get's loaded correctly:

       
           
                   Url="
http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
           
       
   



When running the application the end result looks like this:


No comments: