Reflection .NET API
Walkthrough / Add the Terminal User Control / Set up the WPF Terminal User Control
In This Topic
    Set up the WPF Terminal User Control
    In This Topic

    The Reflection Windows Presentation Foundation (WPF) Terminal User Control can be embedded in your WPF applications to provide a terminal screen on a window along with another application or a different data source. This control renders a terminal emulator session within a given WFP application.

    Note:
    You can use the Reflection WPF Terminal User Control with Visual Studio 2017 targeted to .NET 4.7.1.

    When creating applications on a 64-bit machine and using Reflection "Terminal Controls", make sure the application target is set to "x86" instead of "Any CPU" or "x64". This is required because a 64-bit application does not work directly with 32-bit controls. If the build target (Platform) is not set to "x86", the "COMException: class not registered" error is displayed during runtime.

     

    To set up the WPF terminal User ControlTab

    1. In Visual Studio, create a WPF Application.
    2. In the Design view, drag and drop the control you want to use (for example, IbmTerminalControl) from the Toolbox to MainWindow.
    3. With the View selected, open the Properties window and enter the name for the control. ( In the example, we'll use  ibmTerminalControl1 for an IBM terminal control.)
          
                     

    4. To allow the control to fill the window, remove the alignment attributes shown in the XAML view. For example:


      Change:

      <Grid>
           <IbmHosts:IbmTerminalControl x:Name="ibmTerminalControl1"
           HorizontalAlignment
      ="Left" Margin="191,188,0,0"
           VerticalAlignment
      ="Top"/>
      </Grid>


      To: 

      <Grid>
           <IbmHosts:IbmTerminalControl x:Name="ibmTerminalControl1" />
      </Grid>

    5. In the Design view, select MainWindow and then select  to open the Property window Events view.
    6. Add the Window_Loaded event handler for the MainWindow Loaded event.
            
    7. In the code view for MainWindow.xaml.cs, add a class variable to use as an initialization flag and a call to InitInstance in the Window_Loaded event handler as follows. The InitInstance call initializes the terminal control with a new host type or a session file. The flag is used to ensure that the InitInstance is called only the first time the window is loaded and prevents initialization of additional terminal controls.
      Note: Certain WPF applications can cause the Loaded event to fire more than once.  Calling InitInstance every time the loaded event fires can launch additional terminal controls. This causes your application to lose its connection to the previous control, resulting in an exception error.

         

      public partial class MainWindow : Window
      {
          private bool isInitialized = false;
      
          public MainWindow()
          {
              InitializeComponent();
          }
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              //Make sure InitInstance is only called once 
              if (!isInitialized)
              {
                  ibmTerminalControl1.InitInstance(Attachmate.Reflection.UserControl.Wpf.IbmHosts.HostType.IBM3270);
                  isInitialized = true;
              }
             
          }
      }
      
    8. Back in the Design view of the .xaml file, double-click on the control you added (for example, ibmTerminalControl1). This adds the default event handler for the ibmTerminalControl1 TerminalInitializedEvent event. This event is fired after the terminal is initialized from the InitInstance call (made after the first time the window loads).
    9. In the code view for MainWindow.xaml.cs, add the event handler code as follows. The following code sets the host address and connects to that host.
      Note: This code also checks for terminal initialization errors to make sure that the terminal is properly initialized before setting the host name and connecting.
                                         
      private void ibmTerminalControl1_TerminalInitializedEvent(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
      {
          // Check if error occurred during the terminal initialization
          if (e.Error != null)
          {
              MessageBox.Show("Error during terminal initialization step: " + e.Error); return;
          }
      
          ibmTerminalControl1.IbmTerminal.HostAddress = "zos.efglobe.com";
          ibmTerminalControl1.IbmTerminal.Port = 23;
          ibmTerminalControl1.IbmTerminal.Connect();
      }
      
                                  
    10. Build and run the solution. At runtime, you should get a window that renders a terminal host.

                                  

      Note: You may see the following compiler warning:

      Warning A reference was created to embedded interop assembly 'c:\Windows\assembly\
      GAC_MSIL\Reflection \<version>__13bff1b6907eadcf\Reflection.dll' because of an indirect reference to that assembly created by assembly 'c:\Program Files (x86)\Attachmate\Reflection \Programmer Interfaces\Attachmate.Reflection.UserControl.wpf.IbmHosts.dll'. Consider changing the 'Embed Interop Types' property on either assembly."

      The simplest way to resolve this warning is to set the Embed Interop Types property of the Reflection Reference to False. For more about how to resolve this warning, see Technical Information Document 7021397.

       

    To set up the WPF terminal User ControlTab

    1. In Visual Studio, create a WPF Application.
    2. In the Design view, drag and drop the control you want to use (for example, OpenSystemsTerminalControl) from the Toolbox to MainWindow.                        
    3. With the View selected, open the Properties window and enter the name for the control. ( In the example, we'll use  openSystemsTerminalControl1 for an Open Systems control.)
          

    4. To allow the control to fill the window, remove the alignment attributes shown in the XAML view. For example:


      Change:

      <Grid>
          <OpenSystems:OpenSystemsTerminalControl x:Name="openSystemsTerminalControl1" HorizontalAlignment="Left" Height="138" Margin="125,88,0,0" VerticalAlignment="Top" Width="251"/>
      </Grid>
      

       


      To: 

      <Grid>
          <OpenSystems:OpenSystemsTerminalControl x:Name="openSystemsTerminalControl1" />
      </Grid>
      

    5. In the Design view, select MainWindow and then select  to open the Property window Events view.
    6. Add the Window_Loaded event handler for the MainWindow Loaded event.

    7. In the code view for MainWindow.xaml.cs, add a class variable to use as an initialization flag and a call to InitInstance in the Window_Loaded event handler as follows. The InitInstance call initializes the terminal control with a new host type or a session file. The flag is used to ensure that the InitInstance is called only the first time the window is loaded and prevents initialization of additional terminal controls.
      Note: Certain WPF applications can cause the Loaded event to fire more than once.  Calling InitInstance every time the loaded event fires can launch additional terminal controls. This causes your application to lose its connection to the previous control, resulting in an exception error.

       

      public partial class MainWindow : Window
          {
              private bool isInitialized = false;
              public MainWindow()
              {
                  InitializeComponent();
              }
      
              private void Window_Loaded(object sender, RoutedEventArgs e)
              {
                  //Make sure InitInstance is only called once
                  if (!isInitialized)
                  {
                      openSystemsTerminalControl1.InitInstance();
                      isInitialized = true;
                  }
              }
      
              private void openSystemsTerminalControl1_TerminalInitializedEvent(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
              {
                  // Check if error occurred during the terminal initialization
                  if (e.Error != null)
                  {
                      MessageBox.Show("Error during terminal initialization step: " + e.Error); return;
                  }
      
                  ITerminal Terminal = openSystemsTerminalControl1.Terminal;
                  //Use this code for a telnet connection
                  Terminal.ConnectionType = ConnectionTypeOption.Telnet;
                  ((IConnectionSettingsTelnet)Terminal.ConnectionSettings).HostAddress = "yourHost.com"; // replace this hardcoded host name with your host name
                  ((IConnectingSettingsBestNetwork)Terminal.ConnectionSettings).TelnetPort = 23;
      
                  /* Use this code for a secure connection, instead of the previous code for telnet
                   Terminal.ConnectionType = ConnectionTypeOption.SecureShell;
                  ((IConnectionSettingsSecureShell)Terminal.ConnectionSettings).HostAddress = "yourHost.com"; // replace this hardcoded host name with your host name
                  ((IConnectionSettingsSecureShell)Terminal.ConnectionSettings).UserName = "yourUserName"; // replace this hardcoded user name with your user name
                  ((IConnectionSettingsSecureShell)Terminal.ConnectionSettings).SSHPort = 22;
                  ((IConnectionSettingsSecureShell)Terminal.ConnectionSettings).SSHShowBannerDialog = false;
                  ((IConnectionSettingsSecureShell)Terminal.ConnectionSettings).SSHTermWindowAuth = true;*/
      
                  Terminal.Connect();
              }
      }
      
    8. Add the following using directive:

      using Attachmate.Reflection.Emulation.OpenSystems;

    9. Back in the Design view of the .xaml file, double-click on the control you added (for example, openSystemsTerminalControl1). This adds the default event handler for the ibmTerminalControl1 TerminalInitializedEvent event. This event is fired after the terminal is initialized from the InitInstance call (made after the first time the window loads).
    10. In the code view for MainWindow.xaml.cs, add the event handler code as follows. The following code sets the host address and connects to that host.
      Note: This code also checks for terminal initialization errors to make sure that the terminal is properly initialized before setting the host name and connecting.
                                         
      private void openSystemsTerminalControl1_TerminalInitializedEvent(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
      {
      
          // Check if error occurred during the terminal initialization
          if (e.Error != null)
          {
              MessageBox.Show("Error during terminal initialization step: " + e.Error); return;
          }
      
          ITerminal Terminal = openSystemsTerminalControl1.Terminal;
          ((IConnectingSettingsBestNetwork)Terminal.ConnectionSettings).HostAddress = "yourHostName";
          Terminal.Connect();
      }
      
    11. Build and run the solution. At runtime, you should get a window that renders a terminal host.

                                  

      Note: You may see the following compiler warning:

      Warning A reference was created to embedded interop assembly 'c:\Windows\assembly\
      GAC_MSIL\Reflection \<version>__13bff1b6907eadcf\Reflection.dll' because of an indirect reference to that assembly created by assembly 'c:\Program Files (x86)\Attachmate\Reflection \Programmer Interfaces\Attachmate.Reflection.UserControl.wpf.IbmHosts.dll'. Consider changing the 'Embed Interop Types' property on either assembly."

      The simplest way to resolve this warning is to set the Embed Interop Types property of the Reflection Reference to False. For more about how to resolve this warning, see Technical Information Document 7021397.
    12.