2011-11-29 36 views
6

Agregué a mi aplicación WindowsForm una nueva ventana WPF llamada novoLogin.Abrir la ventana WPF en la aplicación WindowsForm

Después de agregarlo, agregué el system.xaml reference .... debug fine.

Ahora estoy tratando de abrir esta nueva ventana desde el WindowsForm existente.

novoLogin nl = new novoLogin(); 
nl.show(); 

El compilador está dando este error:

Error 1 'WindowsFormsApplication1.novoLogin' does not contain a definition for 'show' and no extension method 'show' accepting a first argument of type 'WindowsFormsApplication1.novoLogin' could be found (are you missing a using directive or an assembly reference?)

+4

te conozco de que C# es sensible a mayúsculas, ¿verdad? – madd0

Respuesta

22

Este brief article explica cómo se puede lograr esto.

Si usted se encuentra en necesidad de abrir una ventana de WPF desde un programa de Windows Forms, esta es una manera de hacerlo (funciona para mí):

  1. Crear/Agregar un nuevo proyecto de tipo WPF Custom Control Library
  2. añadir un nuevo elemento de tipo Window (WPF)
  3. Haga su cosa con la ventana de WPF
  4. Desde su aplicación WinForms, crear y abrir la ventana de WPF

    using System; 
    using System.Windows.Forms; 
    using System.Windows.Forms.Integration; 
    
    var wpfwindow = new WPFWindow.Window1(); 
    ElementHost.EnableModelessKeyboardInterop(wpfwindow); 
    wpfwindow.Show(); 
    
+0

@Purplegoldfish: Vi que agregaste el código (y por qué lo hiciste en la revisión). ¡Gracias por esto, ahora puedo tener esto en cuenta para futuras respuestas! :) – Abbas

3

Tener un vistazo a esto: http://www.mobilemotion.eu/?p=1537&lang=en

Resumen:

Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor. The top node usually contains several tags, one for each build configuration and a global one. In the global node (the one without Condition attribute), search for the sub-node or create one if it does not exist. This node should contain two GUIDs: FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project, and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The full line should look as follows:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

(If you’re interested in details, codeproject holds a complete list of potential project GUIDs: http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)

Reload the project in Visual Studio, and open the Add New Item wizard.

Since the project is now officially classified as WPF project, this wizard should now contain the WPF window option. By the way, since there is no WinForms project GUID that could be overwritten, this approach does not harm the existing project components.

he intentado este enfoque para un proyecto VB.NET y funciona!

El uso de VB.NET, obviamente, tiene que editar por encima de las líneas sustituyendo el GUID de {FAE04EC0-301F-11d3-BF4B-00C04F79EFBC} a {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

0

quería mostrar wpf formulario en windowForm y había algún problema de recursos ...

(porque utilicé recursos ...). Finalmente he utilizado este código en mi proyecto windowsForm:

En primer lugar crear una instancia global de la clase de aplicación como esta:

WPFTest.App app; 

por qué esto es global?

porque esta clase es el producto único y no se puede crear más de una instancia en el mismo dominio de aplicación

Ahora, por ejemplo, usted tiene un evento de botón para mostrar la forma de WPF. En el evento de botón tenemos:

private void button1_Click(object sender, EventArgs e) 
    { 
     if (System.Windows.Application.Current == null) 
     { 
      app = new WPFTest.App() 
      { 
       ShutdownMode = ShutdownMode.OnExplicitShutdown 
      }; 
      app.InitializeComponent(); 
     } 
     else 
     { 
      app = (WPFTest.App)System.Windows.Application.Current; 
      app.MainWindow = new WPFTest.YourWindow(); 
      System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow); 
      app.MainWindow.Show(); 
     } 
    } 

nota: WPFTest es el nombre de su proyecto y YourWindow() es la ventana que quieres mostrar

Cuestiones relacionadas