none
Adding Custom TileSource throw System.NullReferenceException

    Question

  • I'm new to bingmaps wpf control and I'm referring to a topic from http://social.msdn.microsoft.com/Forums/en-US/43bf9c89-1a2b-4bea-bb2e-22fdc151cdbd/bing-map-display-problem-with-wms?forum=bingmapssilverlightwpfcontrols and try to create similar one

    I host the bingmap control on a usercontrol as follow:

    <UserControl x:Class="Kio.Views.Maps"
    
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Kio.Data"
                 xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
                
        <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
    
            <m:Map Grid.Column="0" x:Name="MyMaps" Mode="AerialWithLabels" CredentialsProvider="AvCwvKRuOi0_71zNutgIfDl2K9CAi2pF-uGfxS2dyONvsXwNDNuf9rb0rCTh7sRz" ScaleVisibility="Visible" Center="{Binding CenterMap, Mode=TwoWay}" ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}" Margin="50,50,50,50">
                <m:Map.Children>
                    <m:MapTileLayer x:Name="test">
                        <m:MapTileLayer.TileSource>
                            <local:WMSTileSource></local:WMSTileSource>
                        </m:MapTileLayer.TileSource>
                    </m:MapTileLayer>
                    <m:MapItemsControl ItemTemplate="{StaticResource PushpinTemplate}" ItemsSource="{Binding MapLocations}" />
                </m:Map.Children>
            </m:Map>
            <StackPanel Grid.Column="0" VerticalAlignment="Center" Margin="50" HorizontalAlignment="Right">
                <Button Content="+" Width="80" Height="80" Margin="5,5,5,5"  Command="{Binding ZoomIn}"></Button>
                <Button Content="-" Width="80" Height="80" Margin="5,5,5,5" Command="{Binding ZoomOut}"></Button>
                <Button Content="Home" Width="80" Height="80" Margin="5,5,5,5" Command="{Binding MapHome}"></Button>
            </StackPanel>
        </Grid>
    </UserControl>

     

    and the WMSTileSource as

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Maps.MapControl.WPF;
    
    namespace Kio.Data
    {
        public class WMSTileSource : TileSource
        {
            public WMSTileSource()
                : base("http://xx.xx.xx.xx:8080/geoserver/wms?LAYERS=map:layer&TRANSPARENT=TRUE&FORMAT=image/png&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG:4326&BBOX={0}&WIDTH=256&HEIGHT=256")
            {
    
            }
            public override Uri GetUri(int x, int y, int zoomLevel)
            {
                return new Uri(String.Format(this.UriFormat, XYZoomToBBox(x, y, zoomLevel)));
            }
    
            public string XYZoomToBBox(int x, int y, int zoom)
            {
                int TILE_HEIGHT = 256, TILE_WIDTH = 256;
                // From the grid position and zoom, work out the min and max Latitude / Longitude values of this tile
                double W = (float)(x * TILE_WIDTH) * 360 / (float)(TILE_WIDTH * Math.Pow(2, zoom)) - 180;
                double N = (float)Math.Asin((Math.Exp((0.5 - (y * TILE_HEIGHT) / (TILE_HEIGHT) / Math.Pow(2, zoom)) * 4 * Math.PI) - 1) / (Math.Exp((0.5 - (y * TILE_HEIGHT) / 256 / Math.Pow(2, zoom)) * 4 * Math.PI) + 1)) * 180 / (float)Math.PI;
                double E = (float)((x + 1) * TILE_WIDTH) * 360 / (float)(TILE_WIDTH * Math.Pow(2, zoom)) - 180;
                double S = (float)Math.Asin((Math.Exp((0.5 - ((y + 1) * TILE_HEIGHT) / (TILE_HEIGHT) / Math.Pow(2, zoom)) * 4 * Math.PI) - 1) / (Math.Exp((0.5 - ((y + 1) * TILE_HEIGHT) / 256 / Math.Pow(2, zoom)) * 4 * Math.PI) + 1)) * 180 / (float)Math.PI;
                string[] bounds = new string[] { W.ToString(), S.ToString(), E.ToString(), N.ToString() };
                // Return a comma-separated string of the bounding coordinates
                return string.Join(",", bounds);
            }
        }
    }

    However, it throw an System.NullReferenceException exception.The errorstack is:

       at Microsoft.Maps.MapControl.WPF.MapTileLayer.Microsoft.Maps.MapControl.WPF.IProjectable.SetView(Size viewportSize, Matrix3D normalizedMercatorToViewport, Matrix3D viewportToNormalizedMercator)
       at Microsoft.Maps.MapControl.WPF.MapCore.UpdateView()
       at Microsoft.Maps.MapControl.WPF.MapCore.AnimateViewUsingSprings(Point centerNormalizedMercator, Double zoomLevel, Double heading, Boolean setViewImmediately)
       at Microsoft.Maps.MapControl.WPF.MapCore.SetViewInternal(Point centerNormalizedMercator, Double zoomLevel, Double heading)
       at Microsoft.Maps.MapControl.WPF.MapCore.SetView(Location center, Double zoomLevel, Double heading)
       at Microsoft.Maps.MapControl.WPF.MapCore.SetView(Location center, Double zoomLevel)
       at Microsoft.Maps.MapControl.WPF.MapCore.OnCenterChanged(DependencyPropertyChangedEventArgs e)
       at Microsoft.Maps.MapControl.WPF.MapCore.OnCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
       at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
       at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
       at System.Windows.Data.BindingExpression.Activate(Object item)
       at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
       at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
       at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
       at MS.Internal.Data.DataBindEngine.Run(Object arg)
       at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
       at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
       at System.Windows.ContextLayoutManager.UpdateLayout()
       at System.Windows.UIElement.UpdateLayout()
       at System.Windows.Interop.HwndSource.SetLayoutSize()
       at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
       at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
       at System.Windows.Window.SetRootVisual()
       at System.Windows.Window.SetRootVisualAndUpdateSTC()
       at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
       at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
       at System.Windows.Window.CreateSourceWindowDuringShow()
       at System.Windows.Window.SafeCreateWindowDuringShow()
       at System.Windows.Window.ShowHelper(Object booleanBox)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at KiosK.App.Main() in C:\MyProject\WPFCourse\KiosK\KiosK\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()


    Any idea to solve this error? Thank in advance.

    Saturday, February 15, 2014 10:07 AM

Answers

All replies