7 Replies Latest reply on Nov 12, 2018 7:34 PM by zburke

    How can I create an AF picker object in a WPF window?

    TaoIPE

      How can I create PI/AF pickers objects in a WPF window like what we had in Forms. Do we have example code of this kind?

      Thanks,

        • Re: How can I create an AF picker object in a WPF window?
          gregor

          Hello Tao,

           

          If your question is related to how to create a WPF application, you will find a bunch of WPF tutorials in the Internet.

          In case your question is specific to AF SDK, can you please ask more detailed? Are you e.g. looking for a way to retrieve a list of known servers?

            • Re: How can I create an AF picker object in a WPF window?
              TaoIPE

              Hey, Gregor,

              Yes, my question is specific to AF SDK. I want to build a windows form dialog in WPF to retrieve a list of known servers and connect to one of them.

                • Re: How can I create an AF picker object in a WPF window?
                  Nocodes79

                  Hi Tao,

                   

                  I think this is a more general question: how can I embed WindowsForms Controls in a WPF application?

                  In my solution I would first create a Windows Form with all the Windows Forms controls I need.

                  Then I would create a WPF application and embed the Windows Form rpeviously created.

                   

                  Hre goes how to embed Windows Forms controls in WPF:

                  https://www.dotnetperls.com/windowsformshost-wpf

                    • Re: How can I create an AF picker object in a WPF window?
                      zburke

                      Sorry to dig up an old thread, but I wanted to throw out one more option for posterity's sake.

                       

                      You can also directly add these WinForms hosted controls directly to your XAML. See below:

                      <Window x:Class="PiUiWpf.MainWindow"
                              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:local="clr-namespace:PiUiWpf"
                              xmlns:pi="clr-namespace:OSIsoft.AF.UI;assembly=OSIsoft.AF.UI"  
                              xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
                              mc:Ignorable="d"
                              Title="Embedding PI WinForm UI Example" Height="200" Width="600">
                          <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                              <WindowsFormsHost>
                                  <pi:PISystemPicker />
                              </WindowsFormsHost>
                          </StackPanel>
                      </Window>
                      

                       

                      This will give you something that will look like this:

                       

                      Note: for this to work, you will need to add references to the following:

                      • OSIsoft.AF.UI
                      • System.Windows.Forms
                      • WindowFormsIntegration

                       

                      There are two particularly important lines of code here:

                              xmlns:pi="clr-namespace:OSIsoft.AF.UI;assembly=OSIsoft.AF.UI"  
                              xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
                      

                       

                      The first line makes OSIsoft's AF UI elements accessible to us using the "pi" namespace. We can then reference this late (as seen below). The second isn't really required for your code to run, but in my experience is needed IntelliSense to work properly here.

                              <WindowsFormsHost>
                                  <pi:PISystemPicker />
                              </WindowsFormsHost>
                      

                       

                      Everyone has their own style, but I like this approach because I can manage the visual side of things in XAML. By looking at the Properties window, I can see what changes are available. These changes can be implemented either as attributes to the PISystemPicker or as property elements. In the sample below, there was no reason why "ShowDelete" and "ShowNew" were specified where they were. They both could have been defined in either place!

                              <WindowsFormsHost>
                                  <pi:PISystemPicker ShowDelete="False">
                                      <pi:PISystemPicker.ShowNew>
                                          False
                                      </pi:PISystemPicker.ShowNew>
                                  </pi:PISystemPicker>
                              </WindowsFormsHost>
                      

                       

                      The above changes to the code results in something that looks like this (notice two buttons are now missing):

                       

                      Hope this helps!

                      1 of 1 people found this helpful
                    • Re: How can I create an AF picker object in a WPF window?
                      gregor

                      Hello Tao,

                       

                      Please see below sample. If you are connecting against a Collective, the Collective property will have the name of the Collective, otherwise it will be null. Please note that it is usually not necessary to call Connect, Disconnect methods as AF SDK is handling connections internally when requests for data are send. This may be different with your application if the user should be allowed to switch between servers.

                       

                              static void Main(string[] args)
                              {
                                  // Create an object of known AF Servers 
                                  OSIsoft.AF.PISystems knownPISystems = new OSIsoft.AF.PISystems();
                                  // Iterate through the list
                                  Console.WriteLine("List of known AF Servers");
                                  foreach (OSIsoft.AF.PISystem piSystem in knownPISystems)
                                  {
                                      Console.WriteLine(" -> {0}", piSystem.Name);
                                  }
                                  // Connect to an AF Server by its name
                                  string afServerHost = "AFServer";
                                  OSIsoft.AF.PISystem afServer = knownPISystems[afServerHost];
                                  afServer.Connect();
                                  Console.WriteLine();
                                  // Create an object of known PI Data Archives 
                                  OSIsoft.AF.PI.PIServers knownPIDataArchives = new OSIsoft.AF.PI.PIServers();
                                  // Iterate through the list
                                  Console.WriteLine("List of known PI Data Archives Servers");
                                  foreach (OSIsoft.AF.PI.PIServer piServer in knownPIDataArchives)
                                  {
                                      Console.WriteLine(" -> {0}", piServer.Name);
                                  }
                                  // Connect to an PI Data Archive by its name
                                  string piDataArchiveHost = "PIServer";
                                  OSIsoft.AF.PI.PIServer piDataArchive = knownPIDataArchives[piDataArchiveHost];
                                  piDataArchive.Connect();
                                  Console.WriteLine();
                                  Console.Write("Done. Press any key to quit .. ");
                                  Console.ReadKey();
                              }
                      
                      1 of 1 people found this helpful
                    • Re: How can I create an AF picker object in a WPF window?
                      Jaison_Rodrigues

                      Hi Gregor,

                       

                      How can we add controls like AFDatabasePicker, PISystemPicker controls that are part of OSIsoft.AF.UI library to a WPF application?

                       

                      They load perfectly fine for Windows Form but are not available while creating a WPF application.

                       

                      Regards,

                      Jaison Rodrigues

                        • Re: How can I create an AF picker object in a WPF window?
                          gregor

                          Hello Jaison,

                           

                          OSIsoft.AF.UI.dll, the assembly containing the controls you mention is a .NET library.

                           

                          My earlier understanding was that WPF is the client version of Silverlight but I found this resource which does a better job in Contrasting Silverlight and WPF

                          One of the major difference is the .NET Framework support. Silverlight only supports .NET Framework for Silverlight while according to the before mentioned resource WPF supports .NET Framework.

                           

                          A core sentence related to your question is: Windows Presentation Foundation (WPF) provides developers with a unified programming model for building rich Windows Forms applications that incorporate user interface (UI) ...

                           

                          Well, this tells me what you like to do should be possible: Using Windows Forms controls offered by OSIsoft.AF.UI.dll in your WPF application. The Walkthrough: Hosting a Windows Forms Control in WPF describes how this is done.