Select to view content in your preferred language

MyMap_MouseOver issue..

1007
7
08-04-2010 04:03 AM
JoshV
by
Regular Contributor
When my Silverlight App is loading in the browser, if I have my mouse hovering over the map area I will get a "Object reference not set to an instance of an object".  The error states this occured in MyMap_MouseOver event.  The App will load fine as long as the mouse cursor is not on the map when the App is loading but I can't take the chance a user will do that.  Does someone have some suggestions?

many thanks.
0 Kudos
7 Replies
jonataspovoas
Regular Contributor
Hi,

I made a Small Application to demonstrate a possible solution to your problem.

the Xaml has a Map and a Border with a TextBox that Show the mouse position on the screen or OOB (out of border) if the mouse is out of the map. here's the code:

XAML:

<UserControl x:Class="MouseOverTest.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" 
    mc:Ignorable="d" d:DesignWidth="780" d:DesignHeight="480"
 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">
    <Grid x:Name="LayoutRoot">
        <!-- Map Control -->
        <esri:Map x:Name="MyMap" Background="White">
            <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" 
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
        </esri:Map>
        
        <Border BorderThickness="2" BorderBrush="Black" Height="50" Width="100" Margin="12,418,668,12">
            <TextBlock Name="mousePosition"/>
        </Border>
        
    </Grid>
</UserControl>



CS:

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 System.Windows.Controls.Primitives;

namespace MouseOverTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            MyMap.Loaded += new RoutedEventHandler(MyMap_Loaded);
        }

        private void MyMap_Loaded(object sender, RoutedEventArgs e)
        {
            //choose what's best for your case
            MyMap.MouseEnter += new MouseEventHandler(MyMap_MouseEnter);
            MyMap.MouseMove += new MouseEventHandler(MyMap_MouseMove);
            MyMap.MouseLeave += new MouseEventHandler(MyMap_MouseLeave);
        }

        private void MyMap_MouseEnter(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("OOB");
        }

        private void MyMap_MouseMove(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(LayoutRoot).X.ToString(), e.GetPosition(LayoutRoot).Y);
        }

        private void MyMap_MouseLeave(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("OOB");
        }
    }
}




I Hope this is useful XD
0 Kudos
JoshV
by
Regular Contributor
Hi,

I made a Small Application to demonstrate a possible solution to your problem.

the Xaml has a Map and a Border with a TextBox that Show the mouse position on the screen or OOB (out of border) if the mouse is out of the map. here's the code:

XAML:

<UserControl x:Class="MouseOverTest.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" 
    mc:Ignorable="d" d:DesignWidth="780" d:DesignHeight="480"
 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">
    <Grid x:Name="LayoutRoot">
        <!-- Map Control -->
        <esri:Map x:Name="MyMap" Background="White">
            <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" 
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
        </esri:Map>
        
        <Border BorderThickness="2" BorderBrush="Black" Height="50" Width="100" Margin="12,418,668,12">
            <TextBlock Name="mousePosition"/>
        </Border>
        
    </Grid>
</UserControl>



CS:

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 System.Windows.Controls.Primitives;

namespace MouseOverTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            MyMap.Loaded += new RoutedEventHandler(MyMap_Loaded);
        }

        private void MyMap_Loaded(object sender, RoutedEventArgs e)
        {
            //choose what's best for your case
            MyMap.MouseEnter += new MouseEventHandler(MyMap_MouseEnter);
            MyMap.MouseMove += new MouseEventHandler(MyMap_MouseMove);
            MyMap.MouseLeave += new MouseEventHandler(MyMap_MouseLeave);
        }

        private void MyMap_MouseEnter(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("OOB");
        }

        private void MyMap_MouseMove(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(LayoutRoot).X.ToString(), e.GetPosition(LayoutRoot).Y);
        }

        private void MyMap_MouseLeave(object sender, MouseEventArgs e)
        {
            mousePosition.Text = string.Format("OOB");
        }
    }
}




I Hope this is useful XD


Hi I have a question on your code.  at  mousePosition.Text I'm getting "mouseposition" does not exist.  Can you offer me some advice on that?

Thanks
0 Kudos
JoshV
by
Regular Contributor
Hi I have a question on your code.  at  mousePosition.Text I'm getting "mouseposition" does not exist.  Can you offer me some advice on that?

Thanks


Ok I see that I left that out of my XAML.  So when user loads the web application and they hover over the map before the page loads your code will let the page load without an error occuring??

Regards,
0 Kudos
jonataspovoas
Regular Contributor
So when user loads the web application and they hover over the map before the page loads your code will let the page load without an error occuring??


well, I'm still new to silverlight, but in theory this code prevents the error, because the call that overrides the event MouseEnter, MouseMove and MouseLeave is only triggered after the map finishes loading, and, as I understood from your first post, the error ocurred because the map still didn't have an object instance.
0 Kudos
JoshV
by
Regular Contributor
well, I'm still new to silverlight, but in theory this code prevents the error, because the call that overrides the event MouseEnter, MouseMove and MouseLeave is only triggered after the map finishes loading, and, as I understood from your first post, the error ocurred because the map still didn't have an object instance.


Hi jonatas-

That did work, thanks.  In your MyMap_MouseMove, it doesn't seem to return the actual XY's from the Map.  I'm guessing they are just the page XY's??  do you know how I can fix that?

-Thanks
0 Kudos
jonataspovoas
Regular Contributor
In your MyMap_MouseMove, it doesn't seem to return the actual XY's from the Map.  I'm guessing they are just the page XY's??  do you know how I can fix that?


That actualy isn't hard.

see this:

 private void MyMap_MouseMove(object sender, MouseEventArgs e)
        {
            //If you change LayoutRoot to any object that appears on the screen, the spacial 
            //reference to the coordinates change, so, if you want the map coordinates, 
            //you can change it to MyMap
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(LayoutRoot).X.ToString(), e.GetPosition(LayoutRoot).Y);
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(MyMap).X.ToString(), e.GetPosition(MyMap).Y);
        }



This should fix it!

Just to be sure, if your map is inside an ScrollViwer or a ChildWindow, this code Won't work well (i tried once, and i'm still trying to fix code for the childwindow)
0 Kudos
JoshV
by
Regular Contributor
That actualy isn't hard.

see this:

 private void MyMap_MouseMove(object sender, MouseEventArgs e)
        {
            //If you change LayoutRoot to any object that appears on the screen, the spacial 
            //reference to the coordinates change, so, if you want the map coordinates, 
            //you can change it to MyMap
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(LayoutRoot).X.ToString(), e.GetPosition(LayoutRoot).Y);
            mousePosition.Text = string.Format("X: {0}, Y:{1}", e.GetPosition(MyMap).X.ToString(), e.GetPosition(MyMap).Y);
        }



This should fix it!

Just to be sure, if your map is inside an ScrollViwer or a ChildWindow, this code Won't work well (i tried once, and i'm still trying to fix code for the childwindow)


Ok, thanks
0 Kudos