where to put the "Code Behind C#" code?

287
2
08-14-2012 06:08 AM
Labels (1)
MichaelStiso
New Contributor
Hi. I'm using Microsoft Expression Blend to work with the ArcGIS SDK for WPF, and I'm a bit new to both. I have been trying to integrate some of the interactive samples in the "ArcGIS API for Silverlight" section into my code, and I am a bit confused about where to put the code in the "Code Behind C#" tab of those samples.

More specifically, I know how to find the code-behind files for MainWindow and App (and my various user controls), but I'm not sure in which of those I should put the code-behind from the sample.

For example, this is the code-behind for the ElementLayer sample (http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ElementLayer):


[INDENT]using System.Windows;
using System.Windows.Controls;

namespace ArcGISSilverlightSDK
{
[INDENT] public partial class ElementLayer : UserControl
{
[INDENT]  public ElementLayer()
  {
   [INDENT]InitializeComponent();[/INDENT]
  }

  private void RedlandsButton_Click(object sender, RoutedEventArgs e)
  {
   [INDENT]MessageBox.Show("You found Redlands");[/INDENT]
  }
[/INDENT] }
[/INDENT]}
[/INDENT]


I've found through trial and error that I don't put all of that in MainWindow.xaml.cs. Rather, it seems that I need to include just the RedlandsButton_Click method to get things to work. But does that mean I can ignore the rest of the C# code, or do I need to put it elsewhere?

Any tips, or a point toward some relevant documentation, would be appreciated. Thanks much.
0 Kudos
2 Replies
AnttiKajanus1
Occasional Contributor III
Hi there!

MainWindow.xaml.cs is most likely correct palace but some things to note:

In your case, you can just copy needed code to code-behind after the MainWindow's Constructor
private void RedlandsButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("You found Redlands");
}

After that you codebehind shoud look more or less like this (just writing out from my head)
public partial class MainWindow : Window
{
   public MainWindow()
     {
        InitializeComponent();
     }

     private void RedlandsButton_Click(object sender, RoutedEventArgs e)
     {
MessageBox.Show("You found Redlands");
     }     
}


In Wpf, you are using Window control instead of the UserControl so in the code behind

public partial class ElementLayer : UserControl

Here ElementLayer defines the name of the class that should be in your case MainWindow. If you are using Window as your control base, then UserControl should be Window.

I would say that follow more ArcGIS Runtime for WPF 1.0 samples that you can find from your computer after you have installed Runtime development tools:
Start -> All Programs -> ArcGIS ->
Runtime SDK 1.0 for WPF ->ArcGIS Runtime WPF Samples 1.0


0 Kudos
MichaelStiso
New Contributor
Great, that clears up a bit. Thanks much for the help.
0 Kudos