How to add a Marker to ArcGIS map (Silverlight) - silverlight

I am teaching my self arcGIS maps via the Silverlight API for work, and i was trying to do a simple, place marker symbol at XY example, but darn it if I can't get it to work! I am using the following sample page as a reference, but when i go to debug the thing, my marker always ends up dead center of the map, regardless of what the MapPoint's X and Y values:
http://blogs.esri.com/dev/blogs/silverlightwpf/archive/2010/12/14/adding-gps-location-to-the-map-in-the-arcgis-api-for-windows-phone.aspx
my xaml looks as follows:
<UserControl x:Class="CustomGeometry.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="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<esri:Map x:Name="mapWells" Loaded="mapWells_Loaded">
<esri:ArcGISTiledMapServiceLayer x:Name="BaseLayer" ID="Base Map" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
</esri:Map>
</Grid>
</UserControl>
my back end code looks like the following:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
/// <summary>
/// creating Wells Layer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mapWells_Loaded(object sender, RoutedEventArgs e)
{
GraphicsLayer wellsLayer = mapWells.Layers["WellsLayer"] as GraphicsLayer;
if (wellsLayer == null)
{
wellsLayer = new GraphicsLayer()
{
ID = "wellsLayer"
};
mapWells.Layers.Add(wellsLayer);
Graphic marker = new Graphic();
marker.Symbol = new SimpleMarkerSymbol();
wellsLayer.Graphics.Add(marker);
}
// map point not being set correctly.
MapPoint location = new MapPoint(-122.466903686523, 48.7440490722656, mapWells.SpatialReference);
wellsLayer.Graphics[0].Geometry = location;
}
}
What am I doing wrong? I'm assuming it has to do with the spatialReference, but the maps spatial reference is null. Help!

Found the issue. While the walkthough example stated that the GeographicToWebMercator was optional, it clearly is not. When I replaced the line
wellsLayer.Graphics[0].Geometry = location;
with
wellsLayer.Graphics[0].Geometry = ESRI.ArcGIS.Client.Bing.Transform.GeographicToWebMercator(location);
the map marker moves to a location based upon my map point, because while the latitude and longitude might be correct to the given Spacial reference system, the bing maps system has to convert those values to something it knows how to display on the current projection.
Hope this helps others!

Related

Microsoft WPF Designer Value For Property Not Updating When Changed By Another Property

Similar questions have been asked for WinForms projects, but not for WPF projects.
This VERY simple sample below shows the issue. The real user control I have is much more complicated but this sample shows the basic issue I am having.
The UserControl1 xaml (nothing there as it doesn't need anything to show the issue)
<UserControl x:Class="TestProperty.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</UserControl>
The Window xaml (just contains a UserControl1):
<Window
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:TestProperty="clr-namespace:TestProperty;assembly=TestProperty" x:Class="TestApp.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TestProperty:UserControl1 HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="100"/>
</Grid>
</Window>
The actual Window code-behind is really nothing
using System.Windows;
namespace TestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Here is code-behind for UserControl1. Has two properties where the first property simply modifies the second to be the same.
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace TestProperty
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
[Description("Sets a test property value that sets another property.")]
[Category("UserControl1")]
[RefreshProperties(RefreshProperties.All)]
public bool TestValue1
{
get => (bool) GetValue(TestValue1Property);
set => SetValue(TestValue1Property, value);
}
public static readonly DependencyProperty TestValue1Property =
DependencyProperty.Register(
"TestValue1", typeof(bool), typeof(UserControl1),
new FrameworkPropertyMetadata(false,
new PropertyChangedCallback(TestValue1Changed)));
private static void TestValue1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((UserControl1) d)?.TestValue1Changed(e);
private void TestValue1Changed(DependencyPropertyChangedEventArgs e)
{
TestValue2 = (bool) e.NewValue;
InvalidateProperty(TestValue2Property);
}
[Description("Value should change automatically when TestValue1 changes.")]
[Category("UserControl1")]
[RefreshProperties(RefreshProperties.Repaint)]
public bool TestValue2
{
get => (bool) GetValue(TestValue2Property);
set => SetValue(TestValue2Property, value);
}
public static readonly DependencyProperty TestValue2Property =
DependencyProperty.Register(
"TestValue2", typeof(bool), typeof(UserControl1),
new FrameworkPropertyMetadata(false,
new PropertyChangedCallback(TestValue2Changed)));
private static void TestValue2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((UserControl1) d)?.TestValue2Changed(e);
private void TestValue2Changed(DependencyPropertyChangedEventArgs e)
{
InvalidateProperty(TestValue2Property);
}
}
}
The attribute RefreshProperties and the call to InvalidateProperty appear to do nothing.
When editing the MainWindow.xaml and selecting UserControl1, the UserControl1 section of the Properties window shows TestValue1 and TestValue2. If TestValue1 is changed, TestValue2 does NOT change. However, if you change to another Visual Studio (or Blend) editor window and then back to the MainWindow.xaml, then TestValue2 will be the same as TestValue1.
I cannot seem to find any method to automatically have TestValue2 update when TestValue1 forces the update. The real case is obviously more complex with quite a few controls and when adding our UserControl, we want the designer to be able to specify several properties that can result in forcing (or coercing) other properties. However, the designer needs to be able to see the property value changes without having to switch to another file and back again to redraw the designer's properties window.
Is this something that simply can't be done - or is there something I'm missing. Note that this is a UserControl and is not designed to operate itself MVVM, but allow another window to be designed using the control with (or without) MVVM design.
Also, in a 'kind of' related issue. We have one property that needs to have an entry in the properties the same as Width and Height with the auto button. When set to Auto display "Auto (nnn)" as the automatically set value changes nnn changes. When not set, the value is simply nnn and is defined during the design of the window using the UserControl. Is this possible?
Thanks in advance to anyone who can shed light on this.

MapControl using XAML Island in WPF: default map layer is shown over custom tile layer

I've added a MapControl using this instruction to my WPF application. I wanted to add custom map layer to it, so I've added a OpenStreetMap tile layer using this instruction. I want to remove the default map at all, but it doesn't work.
I've tested the original MapControl in a UWP app and it works.
My code in WPF:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls"
Title="MainWindow" Height="450" Width="800">
<Grid>
<controls:MapControl x:Name="mapControl" />
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
HttpMapTileDataSource dataSource = new HttpMapTileDataSource("http://c.tile.openstreetmap.org/{zoomlevel}/{x}/{y}.png");
MapTileSource tileSource = new MapTileSource(dataSource);
tileSource.Visible = true;
tileSource.Layer = MapTileLayer.BackgroundReplacement;
tileSource.IsFadingEnabled = false;
mapControl.Style = Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.MapStyle.None;
mapControl.TileSources.Add(tileSource);
}
}
This is the WPF application. It shows OpenStreetMap layer and also default Bing map layer on top:
This is the UWP app and shows only OpenStreetMap perfectly:
Is there any solution to fix it in WPF?
Update
I find out that this line doesn't apply correctly and it caused the error:
mapControl.Style = Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.MapStyle.None;
Finally I found the solution. Add these lines to the initialization code:
Windows.UI.Xaml.Controls.Maps.MapControl originalMapControl =
mapControl.GetUwpInternalObject() as Windows.UI.Xaml.Controls.Maps.MapControl;
if (originalMapControl != null)
{
originalMapControl.Style = MapStyle.None;
}
It fixes the defect.

Impossible to embed WPFToolkit into my executable?

I made a post about this a while back and got no solutions that worked.
No one said it wasn't possible but some people suggested that the error was coming from something else.
So I created a new solution, simple, just added a toolkit control which calls the DLL via the XAML (namespace import) - which I think is the problem.
I added some code to load the DLL's which I have embedded as resources.
my project can be downloaded from here:
https://onedrive.live.com/redir?resid=B293BA834310C42A%21108
for those who don't have time, here's the code:
MainWindow.xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" x:Class="WpfAppTest1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<chartingToolkit:ScatterSeries HorizontalAlignment="Left" Margin="63,85,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
MainWindow.xaml.cs
namespace WpfAppTest1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
and my App.xaml.cs
namespace WpfAppTest1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var execAssembly = Assembly.GetExecutingAssembly();
string resourceName = execAssembly.FullName.Split(',').First() + ".Resources." + new AssemblyName(args.Name).Name + ".dll";
Console.WriteLine(resourceName);
using (var stream = execAssembly.GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
}
}
The 2 DLL's are:
system.windows.controls.datavisualization.toolkit.dll
WPFToolkit.dll
Both are added as Resources and build action is set to Embedded Resource.
The 2 references are copy local to False.
Looking at the EXE, both files are well built into it.
I added a console writeline to print out the path of the DLL being loaded...
But I still get an error. If I do set the copy local to true, it works.
I am really stuck there and have to use this toolkit and I need the DLL to be part of the EXE. If it's not possible then I'd like to read it from one of you pro's :)
thanks
Steve
edit: Is there no solution to achieve what I need? :(
edit: I still haven't found a solution as of today :/
edit: I wonder if this is even possible.
After quite a bit of effort I seem to have this working.
As your resource for system.windows.controls.datavisualization.toolkit is all in lower case, ensure all of the XAML references to it are also, e.g.:
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=system.windows.controls.datavisualization.toolkit"
This ensures Assembly.GetManifestResourceStream is able to find the resource without any casing issues.
Then change your assembly resolve method to be the following:
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var requestedAssemblyName = new AssemblyName(args.Name).Name;
const string resourcesExtension = ".resources";
if (requestedAssemblyName.EndsWith(resourcesExtension, StringComparison.Ordinal))
{
requestedAssemblyName = requestedAssemblyName.Remove(requestedAssemblyName.Length - resourcesExtension.Length);
}
var resourceName = "WpfAppTest1.Resources." + requestedAssemblyName + ".dll";
var appAssembly = typeof(App).Assembly;
using (var stream = appAssembly.GetManifestResourceStream(resourceName))
{
// Check the resource was found
if (stream == null)
return null;
var assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
var loadedAssembly = Assembly.Load(assemblyData);
return loadedAssembly;
}
}
The key point to note is the removal of the .resources extension which sometimes comes through when resolving assemblies
This is enough to get the assembly loading from the resources, however there will be no implicit styling applied to the charting controls (I am assuming this is to do with how WPF discovers its themes at runtime).
To work around this problem, I put the following in App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/system.windows.controls.datavisualization.toolkit;component/Themes/generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This merges the default chart styles which should then get implicitly applied.
To test it, I used the following content in MainWindow.xaml:
<chartingToolkit:Chart Title="Some Chart" Background="Blue">
<chartingToolkit:Chart.Series>
<chartingToolkit:ScatterSeries Title="Series 1" />
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>

WindowsFormsHost steals focus on activating application even activating by application's other wpf form

To Reproduce my case (.net 4.0)
Create a WPF Application (MainWindow.xaml)
Add a Winform user control that contains a textbox (UserConrol1.cs - Winform)
Put UserControl1 into MainWindow.xaml with windowsformshost
Add another WPF Window that contains a textbox(wpf) to project (Window1.xaml)
Create and Show Window1 after MainWindow InitializeComponent
Your project is ready,
Run Project and set textbox focused in MainWindow.xaml (that in WindowsFormsHost)
Deactivate your application by opening a window (Windows file explorer ,notepad, winamp etc.)
Try to write in textbox that in Window1 window by clicking textbox with mouse
And you will see that you can't set focus on textbox in Window1 because MainWindow Texbox( in winformshost will steal your focus on you application got activating)
Any idea?
MainWindow.xaml
<Window x:Class="WinFormsHostFocusProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WinFormsHostFocusProblem"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="MainWindow" Height="350" Width="525">
<Grid>
<my:WindowsFormsHost Focusable="False" >
<local:UserControl1>
</local:UserControl1>
</my:WindowsFormsHost>
</Grid>
</Window>
MainWindow.xaml.cs
namespace WinFormsHostFocusProblem
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Window1 window1 = new Window1();
window1.Show();
}
}
}
Window1.xaml
<Window x:Class="WinFormsHostFocusProblem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WinFormsHostFocusProblem"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
SizeToContent="WidthAndHeight"
ResizeMode="NoResize"
Topmost="True"
Title="Window1" Height="300" Width="300" Background="Red">
<Grid>
<TextBox Height="25">asd</TextBox>
</Grid>
</Window>
Window1.xaml.cs
namespace WinFormsHostFocusProblem
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
I used my MSDN support contract to get an answer to this problem. The engineer was able to repro from yunusayd's sample and confirmed it is almost certainly a bug in WindowsFormsHost.
Thanks to yunus for the minimal repro sample and Keith at Microsoft for tackling the issue and providing a workaround in less than one day.
Workaround code follows. It works by using .NET reflection to change a private variable used in WindowsFormsHost and disable the trigger for the bug. According to the engineer I worked with, this relies on WPF internals, but he spoke with product team members and it should be safe to use. There's no guarantee of lack of side effects, of course, but so far I haven't found any problems in my testing with multiple WindowsFormsHosts in multiple WPF windows (maybe nesting would be trickier). I modified the original workaround to work generically with multiple windows. You can just as easily hardcode references to specific windows and named WindowsFormsHost controls in the Application_Deactivated event and skip the whole "LastActive" scheme and extension methods.
// App.xaml.cs: you must hook up to Application.Deactivated
void Application_Deactivated(object sender, EventArgs e)
{
foreach (Window w in windows)
{
foreach (var host in UI.DependencyObjectExtension.AllLogicalChildren(w).
Where(c => c is WindowsFormsHost))
{
FIELD_FOCUSED_CHILD.SetValue(host, null);
}
}
}
public readonly static FieldInfo FIELD_FOCUSED_CHILD = typeof(System.Windows.Forms.Integration.WindowsFormsHost).
GetField("_focusedChild", BindingFlags.NonPublic | BindingFlags.Instance);
public static class DependencyObjectExtension
{
/// <summary>
/// Returns a collection of o's logical children, recursively.
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static IEnumerable<DependencyObject> AllLogicalChildren(this DependencyObject o)
{
foreach (var child in LogicalTreeHelper.GetChildren(o))
{
if (child is DependencyObject)
{
yield return (DependencyObject)child;
if (child is DependencyObject)
{
foreach (var innerChild in AllLogicalChildren((DependencyObject)child))
{
yield return innerChild;
}
}
}
}
}
}
We had a similar problem in one of our applications and found that upgrading to .net 4.5 seems to have fixed a good portion of our application's WPF/WinForms focus issues including a similar one to this.
In addition, the _focusedChild field no longer exists in the .net 4.5 version of WindowsFormsHost

Named Controls inside content property of UserControl always appear null at run time

I have created a user control that I reuse for different purposes. I have defined a Dependency Property that holds a UIElement, which I present as the content of one of the areas in the user control.
I noticed that when I use this control. and give names to the elements inside the content property, they always appear as null at run time.
MyContainer.xaml:
<UserControl x:Class="BSSApp.UI.Tests.MyContainer" x:Name="userControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Border x:Name="LayoutRoot" Background="Green" CornerRadius="20" BorderBrush="#005500" BorderThickness="10">
<ContentPresenter Content="{Binding ElementName=userControl, Path=MyContent}" Margin="20"/>
</Border>
</UserControl>
MyContainer.xaml.cs
namespace BSSApp.UI.Tests
{
public partial class MyContainer : UserControl
{
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent",
typeof(UIElement),
typeof(MyContainer),
new PropertyMetadata(new Grid()));
public UIElement MyContent
{
get
{
return (UIElement)GetValue(MyContentProperty);
}
set
{
SetValue(MyContentProperty, value);
}
}
public MyContainer()
{
InitializeComponent();
}
}
}
And the using class:
UserControlContentBug.xaml:
<UserControl x:Class="BSSApp.UI.Tests.UserControlContentBug"
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:t="clr-namespace:BSSApp.UI.Tests"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<t:MyContainer>
<t:MyContainer.MyContent>
<Button Name="btnWithName" Click="btnWithName_Click">Click Me</Button>
</t:MyContainer.MyContent>
</t:MyContainer>
</Grid>
</UserControl>
and the code behind:
UserControlContentBug.xaml.cs
namespace BSSApp.UI.Tests
{
public partial class UserControlContentBug : UserControl
{
public UserControlContentBug()
{
InitializeComponent();
}
private void btnWithName_Click(object sender, RoutedEventArgs e)
{
// this throws an exception
btnWithName.Tag=2;
}
}
}
So there is a declaration of a button, named "btnWithName". the variable is indeed declared in the code behind, but it holds null... It only happens when the button is declared inside a property of another user control.
Does anybody know how to solve this?
Thanks
I just ran into this problem myself. The generated partial class (e.g., obj\UserControlContentBug.xaml.g.cs) which defines the code-accessible named elements in your XAML uses FrameworkElement.FindName() to make those assignments.
According to MSDN:
A run-time API such as FindName is working against an object tree. These objects are loaded into the content area and the CLR runtime engine of the overall Silverlight plug-in. When part of the object tree is created from templates or run-time loaded XAML, a XAML namescope is typically not contiguous with the entirety of that object tree. The result is that there might be a named object in the object tree that a given FindName call cannot find.
Since MyContainer establishes its own namescope boundary as a UserControl, calling FindName() from UserControlContentBug will not explore that part of the object tree (where
btnWithName is defined).
There are probably a few different ways to work around this, but the simplest one that works for me is to:
Give MyContainer a name (x:Name="myContainer").
Remove the name from the Button (btnWithName) so that the superfluous, always-null variable of the same name isn't reserved in the generated C# code.
Create a reference to the Button in the codebehind.
Like so:
namespace BSSApp.UI.Tests
{
public partial class UserControlContentBug : UserControl
{
private Button btnWithName;
public UserControlContentBug()
{
Loaded += UserControlContentBugLoaded;
InitializeComponent();
}
private void UserControlContentBugLoaded(object sender, System.Windows.RoutedEventArgs e)
{
btnWithName = myContainer.MyContent as Button;
}
private void btnWithName_Click(object sender, RoutedEventArgs e)
{
// this throws an exception, not so much
btnWithName.Tag=2;
}
}
}
Hope this helps.
Try using x:Name instead of Name on the button.

Resources