Hi,I'm building a "zoomtoscale" application to silverlight, and i'm having quite some trouble.here's a sample of my work so far:<UserControl x:Class="ScaleZoom.MainPage"
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:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
xmlns:userControls="clr-namespace:ESRI.ArcGIS.SilverlightMapApp"
xmlns:actions="clr-namespace:ESRI.ArcGIS.SilverlightMapApp.Actions"
mc:Ignorable="d" d:DesignWidth="780" d:DesignHeight="480">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Border Margin="0,-35,0,0" Background="Black">
<esri:Map x:Name="mapa">
<esri:ArcGISTiledMapServiceLayer ID="Streets World 2D"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
</esri:Map>
</Border>
<esri:ScaleBar x:Name="scala" Height="23" Width="200" Map="{Binding ElementName=mapa}" DisplayUnit="Kilometers" Margin="10,0,0,10" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
</Grid>
<Grid Grid.Row="0" Background="#AAAAAAAA" Opacity="0.6">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Scale: " TextAlignment="Center" VerticalAlignment="Center" FontFamily="Buxton Sketch" FontSize="18"/>
<TextBox x:Name="txtScale" Height="27" Width="100" VerticalAlignment="Center" Margin="5,0,5,0" Opacity="0.6"/>
<Button x:Name="btnCalc" Content="Calc" FontFamily="Buxton Sketch" FontSize="18" Height="25"/>
<Button x:Name="btnDo" Content="Do!" FontFamily="Buxton Sketch" FontSize="18" Height="25" Margin="5"/>
</StackPanel>
</Grid>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Shapes;
using System.ComponentModel;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Actions;
using ESRI.ArcGIS.Client.Behaviors;
using System.Windows.Controls.Primitives;
namespace ScaleZoom
{
public partial class MainPage : UserControl
{
double actualScale;
public MainPage()
{
InitializeComponent();
btnCalc.Click += new RoutedEventHandler(BtnCalc_Click);
btnDo.Click += new RoutedEventHandler(BtnDo_Click);
txtScale.KeyDown += new KeyEventHandler(TxtScale_KeyDown);
}
private void BtnCalc_Click(object sender, RoutedEventArgs e)
{
var mapWidth = Application.Current.Host.Content.ActualWidth;
var unitsPerPixel = this.mapa.Extent.Width / mapWidth;
actualScale = unitsPerPixel;
}
private void BtnDo_Click(object sender, RoutedEventArgs e)
{
mapa.ZoomToResolution(escalaAtual);
}
private void TxtScale_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BtnCalc_Click(sender, e);
}
}
}
}
With this i can get the "units per pixel" of a resolution shown and return to it anytime i click the "Do!" button.But what i need is the opposite, a function where the user types the desired scale and then go for it. Anyone has any idea of how to do it?I added this to my calculation algorithm:
private void BtnCalc_Click(object sender, RoutedEventArgs e)
{
var mapWidth = Application.Current.Host.Content.ActualWidth;
//4711 is the number that the ScaleBar shows.
var unitsPerPixel = ((double.Parse(txtScale.Text) * 359.99998) / 4711) / mapWidth;
escalaAtual = unitsPerPixel;
}
//4711 is the number that the ScaleBar shows.
var unitsPerPixel = ((double.Parse(txtScale.Text) * 359.99998) / 4711) / mapWidth;
Works if i keep the browser maximized and the same screen resolution, but it's obviously flawed.Does anyone know what i'm doing wrong?