<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: MapView Events handled in ViewModel in .NET Maps SDK Questions</title>
    <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345062#M12271</link>
    <description>&lt;P&gt;So if you are using Prism, then you should be using the Prism container.&amp;nbsp; I think Prism 7 had an Autofac version, but they depreciated that. starting at Prism 8.&amp;nbsp; I would suggest Prism.DryIoc as that is the direction they are moving with Prism 9.&amp;nbsp; You want to add one of the Prism packages with a container either Prism.Unity or Prism.DryIoc and then let Prism do the rest&lt;/P&gt;&lt;P&gt;Prism provides a method in App.Xaml.cs to override with registrations.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
	containerRegistry.Register&amp;lt;IComboBoxItem, ComboBoxItem&amp;gt;();
	containerRegistry.Register&amp;lt;IToolbarCommand, ToolbarCommand&amp;gt;();
	containerRegistry.RegisterSingleton&amp;lt;IConnectionCheck, PortalConnectionCheck&amp;gt;();
	containerRegistry.RegisterSingleton&amp;lt;ILog4NetFacade, Log4NetLogger&amp;gt;();
       
    //If you did not have Modules or Regions you could register a view for Navigation here
   containerRegistry.RegisterForNavigation&amp;lt;MobileAppMapView, MobileAppMapViewModel&amp;gt;();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If you have Modules the initialization is done in the IModule implementation and would be along these lines&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// &amp;lt;summary&amp;gt;
/// Map Module is primary application module responsible for inital loading
/// of the map and all interations with the MapView
/// &amp;lt;/summary&amp;gt;

public class MapModule : ModuleBase
// I have a ModuleBase class that implements IModule so that common initialization code is put there
{
	public override void RegisterTypes(IContainerRegistry containerRegistry)
	{
		base.RegisterTypes(containerRegistry);
		containerRegistry.Register&amp;lt;IMapLoadService, MapLoadService&amp;gt;();
	}

	public override void OnInitialized(IContainerProvider containerProvider)
	{
		base.OnInitialized(containerProvider);

		//RegionManager comes from base class this is is how one gets RegionManager in Module
		// RegionManager = containerProvider.Resolve&amp;lt;IRegionManager&amp;gt;();
		RegionManager.RegisterViewWithRegion(RegionNames.MapRegion, typeof(MobileAppMapView));
	}
}&lt;/LI-CODE&gt;&lt;P&gt;There is no need to be setting a DataContext, Prism does that for you&lt;/P&gt;&lt;P&gt;As an aside:&amp;nbsp; I would avoid naming my View MapView, I find this confuses things sometimes because it is the same name as the esri class&lt;/P&gt;</description>
    <pubDate>Thu, 02 Nov 2023 16:17:04 GMT</pubDate>
    <dc:creator>JoeHershman</dc:creator>
    <dc:date>2023-11-02T16:17:04Z</dc:date>
    <item>
      <title>MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344681#M12258</link>
      <description>&lt;P&gt;Does ESRI have a single example of a MapView event being handled from a ViewModel using the best MVVM approach possible?&lt;/P&gt;&lt;P&gt;I am using the basic Display a Map example from ESRI and added Prism for commands and the event aggregator. How would you handle the GeoViewTapped event in the MapViewModel?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit&lt;/P&gt;&lt;P&gt;Thank you for the responses already. I should have included this in the original post.&lt;/P&gt;&lt;P&gt;Using WPF and .NET 6 and starting with this tutorial. &amp;nbsp; &lt;A href="https://developers.arcgis.com/net/maps-2d/tutorials/display-a-map/" target="_blank"&gt;https://developers.arcgis.com/net/maps-2d/tutorials/display-a-map/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 11:46:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344681#M12258</guid>
      <dc:creator>HS_PolkGIS</dc:creator>
      <dc:date>2023-11-02T11:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344708#M12260</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;This post covered some of this, but unfortunately it appears the links are broken.&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-maps-sdks-native-blog/blogpost-navigating-the-mapview-from-a-viewmodel/ba-p/886181" target="_blank"&gt;https://community.esri.com/t5/arcgis-maps-sdks-native-blog/blogpost-navigating-the-mapview-from-a-viewmodel/ba-p/886181&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I don't have the opportunity to search or access my version of this code, but look for MapViewController in some of Morten's git repositories. While not a full answer, at least it is something to point you in the right direction.&lt;/P&gt;&lt;P&gt;I would implement the GeoViewTapped through custom WeakEventManager implementations to avoid memory leaks.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 21:27:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344708#M12260</guid>
      <dc:creator>BjørnarSundsbø1</dc:creator>
      <dc:date>2023-11-01T21:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344709#M12261</link>
      <description>&lt;P&gt;The approach is the same you use with any view events - the Maps SDK isn't special in this regard. Depending on which UI framework you use, it's slightly different though. For instance MAUI using the &lt;A href="https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/event-to-command-behavior" target="_self"&gt;MAUI Community Toolkit&lt;/A&gt; you can execute a command from an event:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;esri:MapView Map="{Binding Map, Source={StaticResource VM}}"&amp;gt;
    &amp;lt;esri:MapView.Behaviors&amp;gt;
        &amp;lt;mauitoolkit:EventToCommandBehavior EventName="GeoViewTapped"
            x:TypeArguments="esri:GeoViewInputEventArgs"
            Command="{Binding GeoViewTappedCommand, Source={StaticResource VM}}" /&amp;gt;
     &amp;lt;/esri:MapView.Behaviors&amp;gt;
&amp;lt;/esri:MapView&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In WPF you can use the &lt;A href="https://github.com/microsoft/XamlBehaviorsWpf" target="_self"&gt;Behaviors SDK&lt;/A&gt; to do the same thing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;esri:MapView Map="{Binding Map, Source={StaticResource VM}}"&amp;gt;
	&amp;lt;Behaviors:Interaction.Triggers&amp;gt;
		&amp;lt;Behaviors:EventTrigger EventName="GeoViewTapped" &amp;gt;
			&amp;lt;Behaviors:InvokeCommandAction Command="{Binding GeoViewTappedCommand, Source={StaticResource VM}}" PassEventArgsToCommand="True" /&amp;gt;
		&amp;lt;/Behaviors:EventTrigger&amp;gt;
	&amp;lt;/Behaviors:Interaction.Triggers&amp;gt;
&amp;lt;/esri:MapView&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In WinUI and UWP, you can bind straight to a method in your VM using x:Bind:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt; GeoViewTapped="{x:Bind VM.OnGeoViewTapped}"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Prism also offers some documentation on this:&lt;BR /&gt;&amp;nbsp;-&amp;nbsp;&lt;A href="https://prismlibrary.com/docs/wpf/interactivity/event-to-command.htm" target="_blank" rel="noopener"&gt;https://prismlibrary.com/docs/wpf/interactivity/event-to-command.htm&lt;/A&gt;l&lt;BR /&gt;&amp;nbsp;-&amp;nbsp;&lt;A href="https://prismlibrary.com/docs/maui/behaviors/eventtocommandbehavior.html" target="_blank" rel="noopener"&gt;https://prismlibrary.com/docs/maui/behaviors/eventtocommandbehavior.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 21:44:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344709#M12261</guid>
      <dc:creator>dotMorten_esri</dc:creator>
      <dc:date>2023-11-01T21:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344737#M12263</link>
      <description>&lt;P&gt;You don't really need a controller for reacting to events (see my other response). For performing operations on the view, this PR in the works might be of interest:&amp;nbsp;&lt;A href="https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/pull/528" target="_blank"&gt;https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/pull/528&lt;/A&gt;&amp;nbsp;(you could just pull &lt;A href="https://github.com/Esri/arcgis-maps-sdk-dotnet-toolkit/blob/GeoViewController/src/Toolkit/Toolkit/GeoViewController.cs" target="_self"&gt;the controller code&lt;/A&gt;&amp;nbsp;and have a play with it until this is in the toolkit)&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 22:00:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344737#M12263</guid>
      <dc:creator>dotMorten_esri</dc:creator>
      <dc:date>2023-11-01T22:00:57Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344758#M12265</link>
      <description>&lt;P&gt;You don't actually say what you are developing in.&amp;nbsp; If you have Prism setup, I would use the CommandToEvent from Prism instead of the MAUI toolkit like Morten shows below.&amp;nbsp; But basically same principle.&amp;nbsp; &amp;nbsp;As a rule if using Prism stick with all of their implementations, don't mix and match MAUI toolkit and Prism implementations&lt;/P&gt;&lt;P&gt;Or you could just fire your own events, which is what I have done.&amp;nbsp; You can inject IEventAggregator into the code behind that has the MapView .&amp;nbsp; I create an Event for each of the MapView interactions and wire them up&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private void WireUpMapViewEventHandlers(IEventAggregator eventAggregator)
{
	MapView.GeoViewTapped += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;GeoViewTappedEvent&amp;gt;().Publish(e); };
	MapView.GeoViewDoubleTapped += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;GeoViewDoubleTappedEvent&amp;gt;().Publish(e); };
	MapView.GeoViewHolding += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;GeoViewHoldingEvent&amp;gt;().Publish(e); };
	MapView.ViewpointChanged += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;ViewpointChangedEvent&amp;gt;().Publish(MapView.VisibleArea.Extent); };
	MapView.NavigationCompleted += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;NavigationCompletedEvent&amp;gt;().Publish(MapView.VisibleArea.Extent); };
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This way everything if just published and I can hook into in any Module&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 12:51:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344758#M12265</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2023-11-02T12:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344903#M12268</link>
      <description>&lt;P&gt;Joe, thank you for the response on this and your snippet is exactly what I am looking for. However, I am a bit confused on how to achieve this.&lt;/P&gt;&lt;P&gt;How are you injecting the IEventAggregator into the View?&lt;/P&gt;&lt;P&gt;For ViewModels, I would pass the IEventAggregator as a parameter in the constructor.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 12:38:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344903#M12268</guid>
      <dc:creator>HS_PolkGIS</dc:creator>
      <dc:date>2023-11-02T12:38:10Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344932#M12269</link>
      <description>&lt;P&gt;You still haven't mentioned what framework you are using.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You should be able to inject the IEventAggregator into the View code behind.&amp;nbsp; Prism will handle this.&amp;nbsp; I have WPF and Xamarin Forms code that use this approach for events associated to View updates or actions&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public MobileAppMapView(IEventAggregator eventAggregator)
{
	_eventAggregator = eventAggregator;
	InitializeComponent();
	
	WireUpMapViewEventHandlers(eventAggregator);
	SetupEventListeners(eventAggregator);
}&lt;/LI-CODE&gt;&lt;P&gt;I have not done this with a View using regions in Xamarin, because when I originally built out my Xamarin architecture Prism hadn't introduced Regions in Xamarin yet.&amp;nbsp; But it should still work&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 13:11:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344932#M12269</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2023-11-02T13:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344952#M12270</link>
      <description>&lt;P&gt;My framework is WPF and I have tried what you suggested but I run into an error when adding a parameter to the View Constructor.&lt;/P&gt;&lt;P&gt;This is where I register (autofac) the View in the Application_Startup,&amp;nbsp;the XAML where the view is used, and the View's code behind. The XAML shows a warning and throws a runtime error due to lacking a parameter-less constructor.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;builder.RegisterType&amp;lt;MapView&amp;gt;().AsSelf();&lt;/LI-CODE&gt;&lt;LI-CODE lang="csharp"&gt;&amp;lt;!--This has a warning for no accessible constructors--&amp;gt;
&amp;lt;mViews:MapView DataContext="{Binding MapViewModel}" /&amp;gt;&lt;/LI-CODE&gt;&lt;LI-CODE lang="csharp"&gt;private IEventAggregator _eventAggregator;
public MapView(IEventAggregator eventAggregator)
{
    InitializeComponent();

    _eventAggregator = eventAggregator;
    RegisterMapViewEvents(_eventAggregator);

}

private void RegisterMapViewEvents(IEventAggregator eventAggregator)
{
    MyMapView.GeoViewTapped += (s, e) =&amp;gt; { eventAggregator.GetEvent&amp;lt;OnClickMapEvent&amp;gt;().Publish(e); };
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 14:23:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1344952#M12270</guid>
      <dc:creator>HS_PolkGIS</dc:creator>
      <dc:date>2023-11-02T14:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345062#M12271</link>
      <description>&lt;P&gt;So if you are using Prism, then you should be using the Prism container.&amp;nbsp; I think Prism 7 had an Autofac version, but they depreciated that. starting at Prism 8.&amp;nbsp; I would suggest Prism.DryIoc as that is the direction they are moving with Prism 9.&amp;nbsp; You want to add one of the Prism packages with a container either Prism.Unity or Prism.DryIoc and then let Prism do the rest&lt;/P&gt;&lt;P&gt;Prism provides a method in App.Xaml.cs to override with registrations.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
	containerRegistry.Register&amp;lt;IComboBoxItem, ComboBoxItem&amp;gt;();
	containerRegistry.Register&amp;lt;IToolbarCommand, ToolbarCommand&amp;gt;();
	containerRegistry.RegisterSingleton&amp;lt;IConnectionCheck, PortalConnectionCheck&amp;gt;();
	containerRegistry.RegisterSingleton&amp;lt;ILog4NetFacade, Log4NetLogger&amp;gt;();
       
    //If you did not have Modules or Regions you could register a view for Navigation here
   containerRegistry.RegisterForNavigation&amp;lt;MobileAppMapView, MobileAppMapViewModel&amp;gt;();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If you have Modules the initialization is done in the IModule implementation and would be along these lines&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// &amp;lt;summary&amp;gt;
/// Map Module is primary application module responsible for inital loading
/// of the map and all interations with the MapView
/// &amp;lt;/summary&amp;gt;

public class MapModule : ModuleBase
// I have a ModuleBase class that implements IModule so that common initialization code is put there
{
	public override void RegisterTypes(IContainerRegistry containerRegistry)
	{
		base.RegisterTypes(containerRegistry);
		containerRegistry.Register&amp;lt;IMapLoadService, MapLoadService&amp;gt;();
	}

	public override void OnInitialized(IContainerProvider containerProvider)
	{
		base.OnInitialized(containerProvider);

		//RegionManager comes from base class this is is how one gets RegionManager in Module
		// RegionManager = containerProvider.Resolve&amp;lt;IRegionManager&amp;gt;();
		RegionManager.RegisterViewWithRegion(RegionNames.MapRegion, typeof(MobileAppMapView));
	}
}&lt;/LI-CODE&gt;&lt;P&gt;There is no need to be setting a DataContext, Prism does that for you&lt;/P&gt;&lt;P&gt;As an aside:&amp;nbsp; I would avoid naming my View MapView, I find this confuses things sometimes because it is the same name as the esri class&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 16:17:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345062#M12271</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2023-11-02T16:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345341#M12276</link>
      <description>&lt;P&gt;Attached is a sample of Prism setup using the sample app shown in help.&amp;nbsp; Even with the APIKey I am getting a token error, but the general setup of Prism with injecting IEventAggregator all works&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Nov 2023 23:29:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345341#M12276</guid>
      <dc:creator>JoeHershman</dc:creator>
      <dc:date>2023-11-02T23:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: MapView Events handled in ViewModel</title>
      <link>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345550#M12278</link>
      <description>&lt;P&gt;I will take a look at your sample and thanks again for your help. Your above post will be marked as the answer as I think that does answer the MapView question.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Nov 2023 14:59:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/net-maps-sdk-questions/mapview-events-handled-in-viewmodel/m-p/1345550#M12278</guid>
      <dc:creator>HS_PolkGIS</dc:creator>
      <dc:date>2023-11-03T14:59:34Z</dc:date>
    </item>
  </channel>
</rss>

