ArcGIS API for Silverlight (1): Getting Started

Source: Internet
Author: User
This section provides a general understanding of the development of Silverlight APIs (ArcGIS API for Silverlight, the same below.
First, let us take advantage of its provisioning. The first step is to build an environment for development. Because it is developed on the basis of Siverlight, you must first sort out the development environment of Siverlight. Silverlight is not built in VS2008, but attached as an add-on. Here you can find detailed installation steps:
Download(97.87 KB)

Note: Step 1 has Silverlight add-on installed (requires the SP1 Patch package of IDE); Step 2 has ExpressionBlend installed in ExpressionStudio, which is equivalent to a visual xaml editor, it can be used to easily create the Silverlight program user interface. in step 3, Silverlight is installed with a very gorgeous image processing effect. For details, refer to the example here; step 4 includes some available Silverlight controls and examples. Next, let's take a look at the requirements of the Silverlight API. It can be seen that for the development of ArcGIS Silverlight program, only step 1 is required, and others are optional. Download the Silverlight API from the ESRI website (you need to register an ESRI Global account for free) for future use.
Summarize the most common installation steps: 1. Install VS2008; 2. Install VS2008 SP1; 3. Install Silverlight Tools for Visual Studio 2008 SP1. At this point, you can develop the Silverlight program. For more information about how to build the development environment, see the post of the yyilyzbc moderator. (For Silverlight API development, you do not need to install ArcGIS Server on your machine. You can directly use data on ArcGIS Online. However, if you want to add your own data, of course, ArcGIS Server is required)
Let's take a look at "Hello World". for GIS, It is a map of beautiful World. The procedure is as follows:
1. In VS2008, create a project and select Silverlight Application;
2. In the prompt box that appears, select Add a new ASP. NET Web project to the solution to hostSilverlight; (the Silverlight program is the same as flash, which is equivalent to a plug-in the Web page. The first option is to embed Silverlight into an ASP. NET Website, and the second option is to embed Silverlight into a temporary html page)
3. Add a reference to the Silverlight API.. NET program development, add reference (note that it is in the Silverlight project rather than ASP.. NET project), find the API downloaded from ESRI, and select Add ESRI. arcGIS. dll;
4. Open Page. xaml, add a reference to the UserControl label, and add some code between the Grid labels. After the code is complete, it looks like this:

  1. <UserControl x: Class = "SilverlightApplication1.Page"
  2. Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. Xmlns: esri = "clr-namespace: ESRI. ArcGIS; assembly = ESRI. ArcGIS"
  5. Width = "400" Height = "300">
  6. <Grid x: Name = "LayoutRoot" Background = "White">
  7. <Esri: Map x: Name = "mymap">
  8. <Esri: Map. Layers>
  9. <Esri: ArcGISTiledMapServiceLayer ID = "layerworldmap"
  10. Url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"/>
  11. </Esri: Map. Layers>
  12. </Esri: Map>
  13. </Grid>
  14. </UserControl>

Copy code

5. Press F5 and run the program to complete our hello world in.
GIS can see the following picture in the browser:
Download(40.67 KB)

After you see the effect, you can understand it.
Let's talk about the basic background of the Silverlight program. Page. xaml is actually a control, which is equivalent to default in asp.net. aspx. Most of the work is done here (app. xaml is equivalent to global. asax); The above is the xaml (read: [ig 'zeml]) code, which is Microsoft's markup language for wpf/silverlight, similar to mxml in flex. All the layout work in the Silverlight program is done by xaml. In Silverlight2, VS2008 can preview the xaml effect in real time, but the preview effect is read-only, the control in the preview is not optional. To make up for this defect, you can use the ExpressionBlend mentioned earlier to visually design the program interface, and the corresponding xaml code is automatically generated, it is used for Complex layout and beautification work (see the clock example in Silverlight. xaml. The usercontrol tag (the root element of the page) proves the page. xaml is actually a control class. The following statements are equivalent to introducing a specific namespace of xml, which includes our ESRI. arcGIS; width and height indicate the size of the Silverlight control. Generally, we remove the width and height attributes here to achieve full screen effect (you can also try it ); grid labels are layout controls, which are equivalent to tables in html and can be flexibly laid out on pages. Commonly Used layout controls in xaml include Canvas and StackPanel. Each xaml Control can have an x: name attribute to reference it on the code-behind page.
Then we are the main character. Map labels (Control inherited from xaml) are equivalent to a Map Control, where layers can be added; here we have added an ArcGISTiledMapServiceLayer layer (the layer type in SilverlightAPI will be discussed in later articles). It corresponds to the cache service released by ArcGIS Server and serves as the client's API, like JavaScript and FlexAPI, resources and operations are referenced through REST. An ID attribute is assigned to this layer, because layers in SilverlightAPI inherit from DependencyObject in xaml, therefore, the property of x: Name is not used. To facilitate finding this layer in code-behind (similar to the managed code of asp.net), the ID attribute is used; the URL content is a world map resource released by ArcGIS Online.
At this point, we should understand this example almost. What if I want to add another layer? Yes, you just need to add another layer to the Map label. However, note that the first layer is displayed at the bottom and determines the space reference information of the entire Map control.
You will naturally think of adding a data layer to view the effect, so you made changes to the Map TAG content (china is a Chinese Map released by the Local Machine ):

  1. <Esri: ArcGISDynamicMapServiceLayer ID = "chinamaplayer"
  2. Url = "http: // localhost/ArcGIS/rest/services/china/MapServer"/>

Copy code

After the operation, there is only one layer of the world map (the spelling and case are correct). What is the problem? To use the event to help find errors.
Silverlight can use some core library content of. net, including events. To add an event to the layer: InitializationFailed. This event is triggered when the layer fails to be added. The process for adding this event is also very simple: adding the InitializationFailed attribute to the above layer will prompt you to generate a new eventhandler. By default, press enter, which looks like this:

  1. <Esri: ArcGISDynamicMapServiceLayerID = "chinamaplayer" InitializationFailed = "ArcGISDynamicMapServiceLayer_InitializationFailed"
  2. Url = "http: // localhost/ArcGIS/rest/services/china/MapServer"/>

Copy code

Right-click the Event and Navigate to Event Handler. The code-behind page (C # in this example) is displayed. Add the following code:

  1. Private void ArcGISDynamicMapServiceLayer_InitializationFailed (object sender, EventArgs e)
  2. {
  3. ESRI. ArcGIS. Layer layer = sender as ESRI. ArcGIS. Layer;
  4. MessageBox. Show (layer. InitializationFailure. Message );
  5. }

Copy code

Then run the program to get the reason for the initialization layer failure:
Download(21.89 KB)

Originally, for security reasons, like flash, Silverlight imposes strict restrictions on cross-origin access. To solve this problem, you can save two xml files in the root directory of the website, for example, C: \ Inetpub \ wwwroot. (In fact, you can save one of them, arcGISOnline has put both xml files in the root directory of the website, so we can reference the above service ).
Let's take a look at the final effect.
Download(120.4 KB)

To better understand xaml and Silverlight, we recommend that you complete two workthrough: hello world and clock in the Silverlight help independently.
Address: http://bbs.esrichina-bj.cn/ESRI/thread-44042-1-1.html

Original Author: diligentpig

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.