2010-05-10 14 views
5

Udało mi się uczynić moją mapę Binga silverlight akceptuje Mousclicks i konwertuje je do Pushpins w C#. Teraz chcę pokazać tekst obok PushPin jako opis, który pojawia się, gdy mysz przejdzie przez pinezkę, nie mam pojęcia, jak to zrobić. Jakie są metody, które pozwalają mi to zrobić?Silverlight - Dodawanie tekstu do pinezki w Bing Maps przez C#

Jest to kod C#:

public partial class MainPage : UserControl 

{ prywatny 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; 
} 

}

Odpowiedz

7

Masz 2 sposoby, aby przejść:

(1), aby utworzyć dowolną UIElement wprost pod PushPinLayer.AddChild. Metoda addChild zaakceptuje i żadnej UIElement, takich jak siatka zawierającego obraz i 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) Utwórz natywną obiekt pinezki na wprost pod PushpinLayer.AddChild, ale najpierw ustaw jego właściwość szablonu. Zauważ, że pinezka są ContentControls i mają właściwości szablonu, który można ustawić od zasobu określonego w 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> 

Powodzenia Jim McCurdy

twarzy To Face Software i YinYangMoney

+0

Dzięki Jim .. To było bardzo pomocne :) To nie działało na początku ... Aż zmieniłem siatkę.Dodaj do grid.Childre n.Add ... Thanks Again! – Morano88

Powiązane problemy