AddIn Button won't populate Windows Form Listbox with returned string from web request

846
4
Jump to solution
02-04-2022 09:57 AM
MarietteShin
New Contributor III

I'm a newbie at building AddIns with the Pro SDK.

I have created an ArcGIS Pro AddIn button using VS2017 C#. I am trying to get a Windows Form (frmSAPAttributes) Listbox (lstAttributes) to populate with a parsed JSON string. The parsing works, and I can display each string individually in a messagebox.show(), but when I try to add the items to my Windows Form Listbox during runtime, and show the form, the form is empty and it just hangs in Pro. The following is the section of code:

protected async override void OnClick()
{
await QueuedTask.Run(() =>
{
// get the currently selected features in the map
var selectedFeatures = ArcGIS.Desktop.Mapping.MapView.Active.Map.GetSelection();

// get the first layer and its corresponding selected feature OIDs
var firstSelectionSet = selectedFeatures.First();

// create an instance of the inspector class
var inspector = new ArcGIS.Desktop.Editing.Attributes.Inspector();

// load the selected features into the inspector using a list of object IDs
inspector.Load(firstSelectionSet.Key, firstSelectionSet.Value);

//get the value of
var pscode = inspector["SAP_ID"];
var SAPID = pscode.ToString();
var urlLink = "http://sappsprod.marinwater.local:8000/equi/" + SAPID;

// Create a request using a URL that can receive a post.
//MessageBox.Show(NewURL);
WebRequest request = WebRequest.Create(urlLink) as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
WebResponse response = request.GetResponse() as HttpWebResponse;
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(responseFromServer);
JToken outer = JToken.Parse(responseFromServer);
JArray inner = outer["DATA"].Value<JArray>();
frmSAPAttributes form = new frmSAPAttributes();
string strSAPID = "SAPID : " + SAPID;

form.lstAttributes.BeginUpdate();
form.lstAttributes.Items.Add(strSAPID);
form.lstAttributes.EndUpdate();
form.Show();

 

Would someone please help me understand what's going on?

Thank You!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
BerndtNording
New Contributor III

Whole new world - maybe. More like a different planet. The "help" is unfortunately rather fragmented:

Reference  The reference for all the Pro objects. For the most part auto-generated and doesn't give you much more than what intellisense does.

Snippets  Bits of code to do various things. Very good, but requires a lot of searching through the various categories if you didn't create the SDK and thus don't know what's in all the various SDK assemblies.

Samples  Lots of very good samples. Requires some editing in wordpad of the .csproj files if you did not install ArcGIS in the default location.

The one you want to look at is under "Framework" called DockpaneSimple. It has a couple of listboxes. Pay attention to the Bindings in BookmarkDockpane.XAML for the listboxes, and the corresponding properties in BookmarkDockpaneViewModel.cs  That app makes a list of Bookmark names, but can just as easily be some text items you get from your WebResponse.

DockPanels don't go away once they are created, so if you want your app to work like a geoprocessing tool and go away once it's done doing it's work, you'll need to add this at the end:

        DockPane dockPane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
        if (dockPane != null) { dockPane.IsVisible = false; }

You can find what _docPaneID for your app is supposed to be in the Config.DAML for your addin.

 

View solution in original post

0 Kudos
4 Replies
BerndtNording
New Contributor III

Not sure why its hanging, could be something with doing UI stuff in a queuedTask. The bigger issue is that it looks like your combo box is a Windows.Forms.ComboBox and while it supposedly possible to use that, Pro AddIn development is supposed to use WPF. Take a look at this video: https://www.youtube.com/watch?v=ItzVMZZDRGc . It does a good job of explaining how to set up the combo box and get it to automatically update when adding stuff.

It's worth it to watch the whole thing, but the relevant part to populating combo boxes starts around 22:00.

Pro AddIns, especially with some sort of UI using WPF and MVVM are a sizeable learning curve jump from ArcMap AddIns and will take a while to get used to, but not too bad after that. Just have to get used to writing considerably more code to do the same thing.

From another newbie.

0 Kudos
MarietteShin
New Contributor III

Thank you very much for your reply, Berndt.

After looking at the video you sent me, I realized that just displaying a list requires use of WPF and a View Model dockpane. I'm trying to find a good existing .NET project for the display of a simple list that I can use to build on. If you know of any, please send my way.

I see that it's a whole new world for ArcObjects now.

Thank you.

Mariette

0 Kudos
BerndtNording
New Contributor III

Whole new world - maybe. More like a different planet. The "help" is unfortunately rather fragmented:

Reference  The reference for all the Pro objects. For the most part auto-generated and doesn't give you much more than what intellisense does.

Snippets  Bits of code to do various things. Very good, but requires a lot of searching through the various categories if you didn't create the SDK and thus don't know what's in all the various SDK assemblies.

Samples  Lots of very good samples. Requires some editing in wordpad of the .csproj files if you did not install ArcGIS in the default location.

The one you want to look at is under "Framework" called DockpaneSimple. It has a couple of listboxes. Pay attention to the Bindings in BookmarkDockpane.XAML for the listboxes, and the corresponding properties in BookmarkDockpaneViewModel.cs  That app makes a list of Bookmark names, but can just as easily be some text items you get from your WebResponse.

DockPanels don't go away once they are created, so if you want your app to work like a geoprocessing tool and go away once it's done doing it's work, you'll need to add this at the end:

        DockPane dockPane = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
        if (dockPane != null) { dockPane.IsVisible = false; }

You can find what _docPaneID for your app is supposed to be in the Config.DAML for your addin.

 

0 Kudos
MarietteShin
New Contributor III

Thank you, Berndt!

i have downloaded the community samples and have looked at the DockpaneSimple example you suggested. I am trying to figure out how to use this for my needs. I wish that there was an easier way 🙂

You have been very helpful, and I really appreciate your time in helping me out.

I think that these samples are as good as it gets, at least for the time being. I think I need to find some WPF snd MVVM trainings...

thanks again.

0 Kudos