2011-11-29 38 views
6

Tôi đã thêm vào ứng dụng WindowsForm của mình một cửa sổ WPF mới có tên novoLogin.Mở cửa sổ WPF trong WindowsForm APP

Sau khi thêm, tôi đã thêm tham chiếu system.xaml .... gỡ lỗi.

Bây giờ tôi đang cố gắng mở cửa sổ mới này từ các cửa sổ hiện có.

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

Trình biên dịch được đưa ra lỗi này:

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

Bạn biết rằng C# là trường hợp nhạy cảm, phải không? – madd0

Trả lời

22

này brief article giải thích làm thế nào bạn có thể đạt được điều này.

Nếu bạn thấy mình có nhu cầu để mở một cửa sổ WPF từ một chương trình WinForms, đây là một cách để làm điều đó (chỉ hoạt động đối với tôi):

  1. Tạo/Thêm một dự án mới của loại WPF Custom Control Library
  2. Thêm một mục mới của loại Window (WPF)
  3. làm điều bạn với WPF Window
  4. Từ ứng dụng WinForms của bạn, tạo và mở WPF Window

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

@Purplegoldfish: Tôi thấy bạn đã thêm mã (và lý do bạn thực hiện việc này trong bản sửa đổi). Cảm ơn vì điều này, bây giờ tôi có thể ghi nhớ điều này cho các câu trả lời trong tương lai! :) – Abbas

3

Có một cái nhìn như thế này: http://www.mobilemotion.eu/?p=1537&lang=en

Tóm tắt:

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.

Tôi chỉ cố gắng tiếp cận này cho một dự án VB.NET và nó hoạt động!

Sử dụng VB.NET rõ ràng là bạn đã chỉnh sửa trên dòng thay GUID từ {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} để {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

0

tôi muốn hiển thị dạng wpf trong windowForm và có một số vấn đề về tài nguyên ...

(vì tôi đã sử dụng tài nguyên ..). Cuối cùng tôi đã sử dụng mã này trong dự án windowsForm tôi:

Đầu tiên tạo một đối tượng toàn cầu của lớp ứng dụng của bạn như thế này:

WPFTest.App app; 

tại sao điều này là toàn cầu?

vì lớp này là singleton và bạn không thể tạo ra nhiều hơn một ví dụ trong cùng một AppDomain

Bây giờ ví dụ bạn có một sự kiện nút để hiển thị hình thức WPF. Tại sự kiện nút ta có:

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(); 
     } 
    } 

lưu ý: WPFTest là tên của dự án và YourWindow của bạn() là cửa sổ mà bạn muốn để hiển thị