SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
WinForms and WPF: An Integrated Approach


The concept of reuse of code with “software building blocks” has been in practice since long. In
the .NET framework, user controls are the quintessential building blocks that give you a simple
yet powerful way to build a set of visual components into a more complex and appealing one.
Another way of doing this would be by using WinForms and WPF (Windows Presentation
Foundation).

WPF is an advanced technology used for rendering user interfaces in Windows-based
applications. It was first released as part of the .NET Framework 3.0. However, WPF utilizes
DirectX instead of relying on the older GDI subsystem. WinForms user controls can be built and
embedded in WinForms applications, and likewise WPF user controls can be built and embedded
in WPF applications.

Hybrids can be built using WinForms and WPF and then WPF controls can be embedded in a
WinForms application or vice versa. Developing WPF application development is easy but
migrating existing WinForms projects to WPF involves a lot of work and effort.

WPF and Windows Forms present two varied architectures for creating application interfaces. Common
interoperation scenarios are provided by the System. The two most important classes that implement
interoperation capabilities are WindowsFormsHost and ElementHost. The latter is an easy method that
is used to integrate WinForms and WPF. It can also be used to place a WPF UIElement on the
WinForms control. ElementHost enables hosting of WPF controls inside a form and on the other
hand WPF supplies the WindowsFormsHost class that allows hosting of WinForms controls
inside a WPF window.

WPF and Windows Forms Interoperation

Both technologies need to have the appropriate keyboard input processing for interoperation
between the WPF and Windows Forms to take place. A hybrid control has a control from one
technology nested in a control from the other technology, which is known as nested
interoperation. A multilevel hybrid control will have more than one level of hybrid control
nesting.

Following are interoperation scenarios that are supported when a WPF control hosts a WinForms
control:

      The WPF can host one or more Windows Forms controls using code.
      It may control one or more Windows Forms controls using XAML.
      It may host a master/detail form with a WPF master and Windows Forms details.
      It may host Windows Forms container controls that contain other Windows Forms
       controls.
   It may host one or more ActiveX controls and master/detail form with a Windows Forms
       master and WPF details
      It may host hybrid controls using code or XAML.

Integrating WinForms and WPF using ElementHost

The ElementHost control resides in the WindowsFormsIntegration assembly. This control is
responsible for loading the WPF control and painting it. Following is a step-by-step guide:

   1. First create a user control in WPF by creating a StackPanel inside the grid with a
      TextBlock and a Button. In order to view a WPF Event, hookup an event handler to the
      Button.
   2. Create a WinForm Host for this user control and then create a new Winform project and
      add a reference to the dlls- PresentationCore.dll, PresentationFramework.dll,
      WindowsBase.dll, WindowsFormsIntegration.dll and the WPF DLL which is already
      created.
   3. To view interoperability between WPF and WinForms, drag a WinForms label and
      button and hook it to an event handler.
   4. In the Form Load handler, create an ElementHost. Since the WPF control’s assembly
      reference is already added, creation an object of the WPF user control can be done.
   5. The control cannot be added to the Form’s control collection since the Form object has
      no idea how to draw the WPF control. To do this, make use of the ElementHost control
      that takes in the user control as its child control and adds it to the WinForm.

WPF controls can be embedded in a WinForms application or WinForms controls in a WPF
application, using a simple adapter. Due to this inherent symmetry, hybrid applications can be
easily built, mixing controls from the other platform.

Weitere Àhnliche Inhalte

Mehr von Talentica Software

Test Policy and Practices
Test Policy and PracticesTest Policy and Practices
Test Policy and PracticesTalentica Software
 
Setting Up Development Environment For Google App Engine & Python | Talentica
Setting Up Development Environment For Google App Engine & Python | TalenticaSetting Up Development Environment For Google App Engine & Python | Talentica
Setting Up Development Environment For Google App Engine & Python | TalenticaTalentica Software
 
Connected World in android - Local data sharing and service discovery
Connected World in android - Local data sharing and service discoveryConnected World in android - Local data sharing and service discovery
Connected World in android - Local data sharing and service discoveryTalentica Software
 
Mobile App Monetization - Ecosystem & Emerging Trends
Mobile App Monetization - Ecosystem & Emerging TrendsMobile App Monetization - Ecosystem & Emerging Trends
Mobile App Monetization - Ecosystem & Emerging TrendsTalentica Software
 
Android Media Player Development
Android Media Player DevelopmentAndroid Media Player Development
Android Media Player DevelopmentTalentica Software
 
Cross Platform Mobile Technologies
Cross Platform Mobile TechnologiesCross Platform Mobile Technologies
Cross Platform Mobile TechnologiesTalentica Software
 
Big Data Technologies - Hadoop
Big Data Technologies - HadoopBig Data Technologies - Hadoop
Big Data Technologies - HadoopTalentica Software
 
Big Data – Are You Ready?
Big Data – Are You Ready?Big Data – Are You Ready?
Big Data – Are You Ready?Talentica Software
 
Continous Integration: A Case Study
Continous Integration: A Case StudyContinous Integration: A Case Study
Continous Integration: A Case StudyTalentica Software
 
Technology Challenges in Building New Media Applications
Technology Challenges in Building New Media ApplicationsTechnology Challenges in Building New Media Applications
Technology Challenges in Building New Media ApplicationsTalentica Software
 
Flex on Grails - Rich Internet Applications With Rapid Application Development
Flex on Grails - Rich Internet Applications With Rapid Application DevelopmentFlex on Grails - Rich Internet Applications With Rapid Application Development
Flex on Grails - Rich Internet Applications With Rapid Application DevelopmentTalentica Software
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftTalentica Software
 

Mehr von Talentica Software (15)

Micro Frontends
Micro FrontendsMicro Frontends
Micro Frontends
 
Test Policy and Practices
Test Policy and PracticesTest Policy and Practices
Test Policy and Practices
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Setting Up Development Environment For Google App Engine & Python | Talentica
Setting Up Development Environment For Google App Engine & Python | TalenticaSetting Up Development Environment For Google App Engine & Python | Talentica
Setting Up Development Environment For Google App Engine & Python | Talentica
 
Connected World in android - Local data sharing and service discovery
Connected World in android - Local data sharing and service discoveryConnected World in android - Local data sharing and service discovery
Connected World in android - Local data sharing and service discovery
 
Mobile App Monetization - Ecosystem & Emerging Trends
Mobile App Monetization - Ecosystem & Emerging TrendsMobile App Monetization - Ecosystem & Emerging Trends
Mobile App Monetization - Ecosystem & Emerging Trends
 
Android Media Player Development
Android Media Player DevelopmentAndroid Media Player Development
Android Media Player Development
 
Cross Platform Mobile Technologies
Cross Platform Mobile TechnologiesCross Platform Mobile Technologies
Cross Platform Mobile Technologies
 
Big Data Technologies - Hadoop
Big Data Technologies - HadoopBig Data Technologies - Hadoop
Big Data Technologies - Hadoop
 
Big Data – Are You Ready?
Big Data – Are You Ready?Big Data – Are You Ready?
Big Data – Are You Ready?
 
Legacy modernization
Legacy modernizationLegacy modernization
Legacy modernization
 
Continous Integration: A Case Study
Continous Integration: A Case StudyContinous Integration: A Case Study
Continous Integration: A Case Study
 
Technology Challenges in Building New Media Applications
Technology Challenges in Building New Media ApplicationsTechnology Challenges in Building New Media Applications
Technology Challenges in Building New Media Applications
 
Flex on Grails - Rich Internet Applications With Rapid Application Development
Flex on Grails - Rich Internet Applications With Rapid Application DevelopmentFlex on Grails - Rich Internet Applications With Rapid Application Development
Flex on Grails - Rich Internet Applications With Rapid Application Development
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thrift
 

KĂŒrzlich hochgeladen

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

KĂŒrzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

WinForms and WPF: An Integrated Approach

  • 1. WinForms and WPF: An Integrated Approach The concept of reuse of code with “software building blocks” has been in practice since long. In the .NET framework, user controls are the quintessential building blocks that give you a simple yet powerful way to build a set of visual components into a more complex and appealing one. Another way of doing this would be by using WinForms and WPF (Windows Presentation Foundation). WPF is an advanced technology used for rendering user interfaces in Windows-based applications. It was first released as part of the .NET Framework 3.0. However, WPF utilizes DirectX instead of relying on the older GDI subsystem. WinForms user controls can be built and embedded in WinForms applications, and likewise WPF user controls can be built and embedded in WPF applications. Hybrids can be built using WinForms and WPF and then WPF controls can be embedded in a WinForms application or vice versa. Developing WPF application development is easy but migrating existing WinForms projects to WPF involves a lot of work and effort. WPF and Windows Forms present two varied architectures for creating application interfaces. Common interoperation scenarios are provided by the System. The two most important classes that implement interoperation capabilities are WindowsFormsHost and ElementHost. The latter is an easy method that is used to integrate WinForms and WPF. It can also be used to place a WPF UIElement on the WinForms control. ElementHost enables hosting of WPF controls inside a form and on the other hand WPF supplies the WindowsFormsHost class that allows hosting of WinForms controls inside a WPF window. WPF and Windows Forms Interoperation Both technologies need to have the appropriate keyboard input processing for interoperation between the WPF and Windows Forms to take place. A hybrid control has a control from one technology nested in a control from the other technology, which is known as nested interoperation. A multilevel hybrid control will have more than one level of hybrid control nesting. Following are interoperation scenarios that are supported when a WPF control hosts a WinForms control:  The WPF can host one or more Windows Forms controls using code.  It may control one or more Windows Forms controls using XAML.  It may host a master/detail form with a WPF master and Windows Forms details.  It may host Windows Forms container controls that contain other Windows Forms controls.
  • 2.  It may host one or more ActiveX controls and master/detail form with a Windows Forms master and WPF details  It may host hybrid controls using code or XAML. Integrating WinForms and WPF using ElementHost The ElementHost control resides in the WindowsFormsIntegration assembly. This control is responsible for loading the WPF control and painting it. Following is a step-by-step guide: 1. First create a user control in WPF by creating a StackPanel inside the grid with a TextBlock and a Button. In order to view a WPF Event, hookup an event handler to the Button. 2. Create a WinForm Host for this user control and then create a new Winform project and add a reference to the dlls- PresentationCore.dll, PresentationFramework.dll, WindowsBase.dll, WindowsFormsIntegration.dll and the WPF DLL which is already created. 3. To view interoperability between WPF and WinForms, drag a WinForms label and button and hook it to an event handler. 4. In the Form Load handler, create an ElementHost. Since the WPF control’s assembly reference is already added, creation an object of the WPF user control can be done. 5. The control cannot be added to the Form’s control collection since the Form object has no idea how to draw the WPF control. To do this, make use of the ElementHost control that takes in the user control as its child control and adds it to the WinForm. WPF controls can be embedded in a WinForms application or WinForms controls in a WPF application, using a simple adapter. Due to this inherent symmetry, hybrid applications can be easily built, mixing controls from the other platform.