Hi everyone
Good day Esri Community
Im coding an add-in (DockPane) that Subscribe to Events on start (SubscribeToStandaloneTablesAddedEvent and SubscribeToStandaloneTablesRemovedEvent) and them when it closed the DockPane Unsubscribe from the previous subscriptions (SubscribeToStandaloneTablesAddedEvent and SubscribeToStandaloneTablesRemovedEvent).
I have been using the Delegate Action and not the SubscriptionToken to call the subscriptions. Also when i Subscribe to an Event i execute some code to Update a List of OpenStandalone Tables in a ComboBox.
My methods are in the Xaml file and i call them with an instance from the ViewModel file to execute without any positive result.
Any suggestions or help.
My DockPane:

My ViewModel File:
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Events;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Events;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Layouts;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Mapping.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
namespace AppEstandarizaDirecciones
{
internal class Dockpane1ViewModel : DockPane
{
private const string _dockPaneID = "AppEstandarizaDirecciones_Dockpane1";
protected Dockpane1ViewModel() { }
/// <summary>
/// Show the DockPane.
/// </summary>
///
internal static void Show()
{
DockPane pane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
if (pane == null)
return;
pane.Activate();
MessageBox.Show("was shown only once.");
}
protected override void OnShow(bool isVisible)
{
//base.OnShow(isVisible);
if (isVisible == true)
{
MessageBox.Show("was shown every time it was viewed");
}
}
protected override void OnHidden()
{
Dockpane1View ElmmiObject = new Dockpane1View();
ElmmiObject.QuitarsubscribeFromStandaloneTablesAddedEvent();
ElmmiObject.QuitarsubscribeFromStandaloneTablesRemovedEvent();
MessageBox.Show("DockPane Closed.");
}
/// <summary>
/// Text shown near the top of the DockPane.
/// </summary>
private string _heading = "My DockPane";
public string Heading
{
get { return _heading; }
set
{
SetProperty(ref _heading, value, () => Heading);
}
}
}
/// <summary>
/// Button implementation to show the DockPane.
/// </summary>
internal class Dockpane1_ShowButton : Button
{
protected override void OnClick()
{
Dockpane1ViewModel.Show();
}
}
}
My Xaml File:
using ArcGIS.Core.Data;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Internal.Mapping.Locate;
using ArcGIS.Desktop.Mapping;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ArcGIS.Desktop.Core.Events;
using ArcGIS.Desktop.Mapping.Events;
using ArcGIS.Desktop.Framework.Events;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Internal.Mapping.Georeference.Events;
using ArcGIS.Desktop.Framework;
namespace AppEstandarizaDirecciones
{
/// <summary>
/// Interaction logic for Dockpane1View.xaml
/// </summary>
public partial class Dockpane1View : UserControl
{
List<string> listaTablas = new List<string>();
List<string> listaCampos = new List<string>();
List<string> listaConsultas = new List<string>();
public Dockpane1View()
{
InitializeComponent();
var mv = MapView.Active;
if (mv != null)
{
var lyrs = mv.Map.GetStandaloneTablesAsFlattenedList();
int cantidadtablas = lyrs.Count();
MessageBox.Show("Number of Tables: " + cantidadtablas);
foreach (var lyr in lyrs)
{
this.MiComboBox1.Items.Add(lyr.Name);
listaTablas.Add(lyr.Name);
MessageBox.Show("Table: " + lyr.Name);
}
}
SubscribeToStandaloneTablesAddedEvent();
MessageBox.Show("ACTIVE Subscription ADD TABLE");
SubscribeToStandaloneTablesRemovedEvent();
MessageBox.Show("ACTIVE Subscription REMOVE TABLE");
}
public void SubscribeToStandaloneTablesAddedEvent()
{
StandaloneTablesAddedEvent.Subscribe(OnStandaloneTablesAdded);
MessageBox.Show("Subscription is ACTIVE ADD TABLE");
}
public void QuitarsubscribeFromStandaloneTablesAddedEvent()
{
StandaloneTablesAddedEvent.Unsubscribe(OnStandaloneTablesAdded);
MessageBox.Show("Subscription is REMOVED ADD TABLE");
}
public void OnStandaloneTablesAdded(StandaloneTableEventArgs obj)
{
foreach (var standaloneTable in obj.Tables)
{
string nombreTablaAdded;
nombreTablaAdded = standaloneTable.Name;
this.Dispatcher.Invoke(() => {
this.MiComboBox2.ItemsSource = null;
this.MiComboBox2.Items.Clear();
this.MiComboBox2.Items.Refresh();
this.MiComboBox1.ItemsSource = null;
this.MiComboBox1.Items.Clear();
this.MiComboBox1.Items.Refresh();
listaTablas.Add(nombreTablaAdded);
this.MiComboBox1.ItemsSource = listaTablas;
this.MiComboBox1.Items.Refresh();
MessageBox.Show("The table was ADDED" + nombreTablaAdded);
});
}
}
public void SubscribeToStandaloneTablesRemovedEvent()
{
StandaloneTablesRemovedEvent.Subscribe(OnStandaloneTablesRemoved);
MessageBox.Show("Be ACTIVE subscribe REMOVE TABLE");
}
public void QuitarsubscribeFromStandaloneTablesRemovedEvent()
{
StandaloneTablesRemovedEvent.Unsubscribe(OnStandaloneTablesRemoved);
MessageBox.Show("Subscription REMOVED REMOVE TABLE");
}
public void OnStandaloneTablesRemoved(StandaloneTableEventArgs obj)
{
foreach (var standaloneTable in obj.Tables)
{
string nombreTablaRemoved;
nombreTablaRemoved = standaloneTable.Name;
this.Dispatcher.Invoke(() => {
this.MiComboBox2.ItemsSource = null;
this.MiComboBox2.Items.Clear();
this.MiComboBox2.Items.Refresh();
this.MiComboBox1.ItemsSource = null;
this.MiComboBox1.Items.Clear();
this.MiComboBox1.Items.Refresh();
listaTablas.Remove(nombreTablaRemoved);
this.MiComboBox1.ItemsSource = listaTablas;
this.MiComboBox1.Items.Refresh();
MessageBox.Show("The table was REMOVED" + nombreTablaRemoved);
});
}
}
}
}