Interactive samples / addin question

2795
6
Jump to solution
04-30-2012 07:25 AM
KarenEllett
Occasional Contributor
I'd like to take the functionality shown in the interactive samples and create an addin for the silverlight viewer.  For example, the bookmark sample.  Is it as simple as using the code shown in the samples, building, and importing the .xap as an addin?  Or is there a lot more involved?  I'm a total newbie with this, so forgive my ignorance!

I should add that I've attempted to do this, but when I upload the .xap file, there are no tools shown; hence my question.  Thanks!
0 Kudos
1 Solution

Accepted Solutions
KatherineDalton
Esri Regular Contributor
Hi Karen,

You are correct, you can't take a sample directly from the ArcGIS API for Silverlight and add it to the Viewer. But since the Viewer is built on top of the ArcGIS API for Silverlight, you can still look to those samples for direction and ideas. One (of many) of the fundamental differences is the Map. When creating your own SL Application, you often declare the Map in xaml, but in the Viewer, the Map already exists.

Technically I suppose you could use the Bookmark (from the Toolkit) as they do in the SLAPI, but the problem is that any bookmarks the end user of your Viewer application creates would be gone as soon as they close the browser. None of the bookmarks they create would persist from session to session. The only way to set bookmarks that persist is to do it from the Builder side of things.

Hope that helps,
Katy
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™

View solution in original post

0 Kudos
6 Replies
KatherineDalton
Esri Regular Contributor
Hi Karen,

It sounds like you have the steps right. Once you have uploaded the Add-In in Builder, click Add Tools on the Tools tab and I believe you should see the Bookmarks sample under the Map category.

Most of our interactive samples are meant to demonstrate various coding practices, so not all of them can be downloaded directly and added as an add-in. But as you've found, Bookmarks is one that is essentially ready to be plugged in to the Builder as-is. If you simply want the xap and don't want to have to worry about downloading and compiling the code, I've placed it on ArcGIS Online [url=http://www.arcgis.com/home/item.html?id=3a6423933da3417886e39f37a2832ae9]here[/url].

Let me know how that goes.

Katy
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™
0 Kudos
KarenEllett
Occasional Contributor
Katy,

I did attempt it, but when I uploaded it, there are no tools shown within the addin, and nothing shows up in the "Add Tool" dialog.  I did try the bookmark sample in the code gallery, but with that one I have to configure the bookmarks when I add the tool to the viewer.  I was hoping for something more like the interactive sample, which allows the user to add and clear their own bookmarks, according to their preferences.
0 Kudos
KarenEllett
Occasional Contributor
I have an app.xaml and app.xaml.cs, as well as MainPage.xaml and MainPage.xaml.cs. 

App.xaml:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="Bkmk.App"
             >
    <Application.Resources>
       
    </Application.Resources>
</Application>


App.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Bkmk
{
    public partial class App : Application
    {

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new ArcGISSilverlightSDK.Bookmarks();
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled.
                // For production applications this error handling should be replaced with something that will
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }

        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}


MainPage.xaml
<UserControl x:Class="ArcGISSilverlightSDK.Bookmarks"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009">
    <Grid x:Name="LayoutRoot" Background="White">

        <esri:Map x:Name="MyMap" WrapAround="True" Extent="-15000000,2000000,-7000000,8000000">
            <esri:ArcGISTiledMapServiceLayer Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
        </esri:Map>

        <esri:Bookmark x:Name="MyBookmarks" Width="125" HorizontalAlignment="Right" VerticalAlignment="Top"
             Margin="0,10,10,0" Background="#CC919191" BorderBrush="#FF92a8b3" Foreground="Black"
             Map="{Binding ElementName=MyMap}" />

    </Grid>
</UserControl>


MainPage.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace ArcGISSilverlightSDK
{
    public partial class Bookmarks : UserControl
    {
        public Bookmarks()
        {
            InitializeComponent();
        }

    }
}
0 Kudos
KatherineDalton
Esri Regular Contributor
Oh, I see. You've actually grabbed the code from the ArcGIS API for Silverlight interactive samples, and those can't be used directly in the ArcGIS Viewer for Silverlight. We have our own set of interactive samples you can look at [url=http://help.arcgis.com/en/webapps/silverlightviewer/samples/start.html]here[/url]. The samples for the ArcGIS API for Silverlight are for use if you were creating your own Silverlight Application from scratch. The Silverlight Viewer/Builder is designed to alleviate the need to create a custom Silverlight application from scratch unless you need/want to.

Hope that helps,
Katy
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™
0 Kudos
KarenEllett
Occasional Contributor
So I can't take the SDK sample code and use it to create an addin?  Gotcha.  Would altering the viewer bookmark example to behave the same way as the sdk example be fairly simple, or a good bit more involved? (ie, to allow the user to click and create a bookmark on the fly)
0 Kudos
KatherineDalton
Esri Regular Contributor
Hi Karen,

You are correct, you can't take a sample directly from the ArcGIS API for Silverlight and add it to the Viewer. But since the Viewer is built on top of the ArcGIS API for Silverlight, you can still look to those samples for direction and ideas. One (of many) of the fundamental differences is the Map. When creating your own SL Application, you often declare the Map in xaml, but in the Viewer, the Map already exists.

Technically I suppose you could use the Bookmark (from the Toolkit) as they do in the SLAPI, but the problem is that any bookmarks the end user of your Viewer application creates would be gone as soon as they close the browser. None of the bookmarks they create would persist from session to session. The only way to set bookmarks that persist is to do it from the Builder side of things.

Hope that helps,
Katy
Katy Dalton | Technical Consultant
THE SCIENCE OF WHERE™
0 Kudos