2011-11-29 75 views
6

我在WindowsForm應用程序中添加了一個名爲novoLogin的新WPF窗口。在Windows窗體中打開WPF窗口APP

添加它後,我添加了system.xaml引用....調試罰款。

現在我試圖從現有的windows窗體中打開這個新窗口。

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

編譯器是給這個錯誤:

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

你知道C#窗口是大小寫敏感的,對不對? – madd0

回答

22

brief article介紹如何實現這一點。

如果你需要從一個WinForms程序中打開一個WPF窗口中找到自己,這是做這件事(對我的作品):

  1. 創建/加入類型的新項目WPF Custom Control Library
  2. 添加型Window (WPF)
  3. 的一個新的項目做與WPF窗口
  4. 從您的WinForms應用程序你的東西,創建並打開WPF窗口

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

@Purplegoldfish:我看到你添加了代碼(以及你爲什麼在修訂版中這樣做)。感謝這一點,現在我可以將這一點銘記在將來的答案! :) – Abbas

3

看一看這樣的:http://www.mobilemotion.eu/?p=1537&lang=en

摘要:

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.

我只是嘗試這種方法的VB.NET項目和它的作品!

使用VB.NET很明顯,你必須編輯上面替換GUID從{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}到{F184B08F-C81C-45F6-A57F-5ABD9991F28F}線

0

我想表現WPF窗體在windowForm中,並有一些資源問題...

(因爲我使用資源..)。最後,我在windowsForm項目中使用此代碼:

首先創建應用類的這樣一個全球性的實例:

WPFTest.App app; 

爲什麼這是全球性的?

因爲這個類是單身,你不能在同一個AppDomain中

創建多個實例現在比如你有一個按鈕事件顯示WPF的形​​式。在按鈕事件有:

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

注:WPFTest是你的項目和YourWindow的名稱()是你想顯示