2010-05-10 10 views
5

Sono riuscito a rendere la mia mappa Bing Silverlight accetta Mousclicks e li converte in simboli in C#. Ora voglio mostrare un testo accanto al PushPin come una descrizione che appare quando il mouse supera il pin, non ho idea di come farlo. Quali sono i metodi che mi consentono di fare questa cosa?Silverlight - Aggiunta di testo a simboli in Bing Maps tramite C#

Questo è il codice C#:

public partial class MainPage : UserControl 

{ privato maplayer m_PushpinLayer;

public MainPage() 
{ 
    InitializeComponent(); 
    base.Loaded += OnLoaded; 
} 

private void OnLoaded(object sender, RoutedEventArgs e) 
{ 
    base.Loaded -= OnLoaded; 

m_PushpinLayer = new MapLayer(); 
x_Map.Children.Add(m_PushpinLayer); 
    x_Map.MouseClick += OnMouseClick; 
} 

private void AddPushpin(double latitude, double longitude) 
{ 
    Pushpin pushpin = new Pushpin(); 
    pushpin.MouseEnter += OnMouseEnter; 
    pushpin.MouseLeave += OnMouseLeave; 
    m_PushpinLayer.AddChild(pushpin, new Location(latitude, longitude), PositionOrigin.BottomCenter); 
} 

private void OnMouseClick(object sender, MapMouseEventArgs e) 
{ 
    Point clickLocation = e.ViewportPoint; 
    Location location = x_Map.ViewportPointToLocation(clickLocation); 
    AddPushpin(location.Latitude, location.Longitude); 
} 

private void OnMouseLeave(object sender, MouseEventArgs e) 
{ 
    Pushpin pushpin = sender as Pushpin; 

    // remove the pushpin transform when mouse leaves 
    pushpin.RenderTransform = null; 
} 

private void OnMouseEnter(object sender, MouseEventArgs e) 
{ 
    Pushpin pushpin = sender as Pushpin; 

    // scaling will shrink (less than 1) or enlarge (greater than 1) source element 
    ScaleTransform st = new ScaleTransform(); 
    st.ScaleX = 1.4; 
    st.ScaleY = 1.4; 

    // set center of scaling to center of pushpin 
    st.CenterX = (pushpin as FrameworkElement).Height/2; 
    st.CenterY = (pushpin as FrameworkElement).Height/2; 

    pushpin.RenderTransform = st; 
} 

}

risposta

7

Hai 2 modi per andare:

(1) creare qualsiasi UIElement a passare in PushPinLayer.AddChild. Il metodo AddChild accetterà e qualsiasi UIElement, come questa griglia contenente un'immagine e un TextBlock:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer); 
Image image = new Image(); 
image.Source = ResourceFile.GetBitmap("Images/Pushpin.png", From.This); 
TextBlock textBlock = new TextBlock(); 
textBlock.Text = "My Pushpin"; 
Grid grid = new Grid(); 
grid.Children.Add(image); 
grid.Children.Add(textBlock); 

m_PushpinLayer.AddChild(grid, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137), 
     PositionOrigin.Center); 

(2) Creare un oggetto puntina da disegno nativo di passare in PushpinLayer.AddChild, ma in primo luogo impostare è di proprietà del modello. Si noti che l'a pressione di sono ContentControls, e avere una proprietà modello che può essere impostata da una risorsa definita in XAML:

MapLayer m_PushpinLayer = new MapLayer(); 
Your_Map.Children.Add(m_PushpinLayer); 
Pushpin pushpin = new Pushpin(); 
pushpin.Template = Application.Current.Resources["PushPinTemplate"] 
    as (ControlTemplate); 
m_PushpinLayer.AddChild(pushpin, 
    new Microsoft.Maps.MapControl.Location(42.658, -71.137), 
     PositionOrigin.Center); 

...

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
> 
    <ControlTemplate x:Key="PushPinTemplate"> 
     <Grid> 
      <Image Source="Images/Pushpin.png" /> 
      <TextBlock Text="My Pushpin" /> 
     </Grid> 
    </ControlTemplate> 
</ResourceDictionary> 

Buona fortuna, Jim McCurdy

Viso To Face Software, e YinYangMoney

+0

Grazie Jim .. Questo è stato molto utile :) All'inizio non ha funzionato .. Fino a quando non ho cambiato la griglia. Aggiungi a grid.Childre n.Aggiungere ... Grazie ancora! – Morano88

Problemi correlati