【问题标题】:Visual Studio - WPF - References not adding properlyVisual Studio - WPF - 引用未正确添加
【发布时间】:2015-07-09 02:26:37
【问题描述】:

我已经查看了过去有关此问题的问题,但无济于事。

我刚刚在 VS 2013 中创建了一个新的 WPF 项目。我转到添加引用,然后选择 System.Windows.Form。它补充说。伟大的!

但是,工具箱中的相应工具仍显示为灰色。是的,自动更新工具箱已打开。我已经全部展示了。我已经重新启动了 VS 并重建了我的解决方案。我已将using System.Windows.Forms; 添加到我的 MainWindow.xaml.cs 文件中。

在这里,我只有最基本的代码,因为这是一个全新的项目,我还没有接触过。

我错过了什么?我试过将 .dll 文件拖到工具箱中,工具仍然是灰色的。我在某处缺少一段代码吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace sub20tool3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

还有:

<Window x:Class="sub20tool3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

【问题讨论】:

  • 您不能直接在 WPF 中托管 Winforms 控件。您需要使用 WindowsFormHost 在 wpf 中添加 Winforms 控件。参考简单教程wpf-tutorial.com/misc-controls/the-windowsformshost-control
  • 此外,如果您只是在寻找要放在 WPF 页面上的标准控件,请使用这些控件的 WPF 版本,而不是 WinForms 版本。

标签: c# wpf xaml visual-studio-2013


【解决方案1】:

您需要一个 WindowsFormHost 元素来保存 WPF 项目中的 winforms 控件。 WindowsFormHost 还需要对 WindowsFormIntegration 的引用。

这是一个关于如何使用它们的好教程:http://www.wpf-tutorial.com/misc-controls/the-windowsformshost-control/

<Window x:Class="WpfTutorialSamples.Misc_controls.WindowsFormsHostSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="WindowsFormsHostSample" Height="350" Width="450">
    <Grid>
        <WindowsFormsHost Name="wfhSample">
            <WindowsFormsHost.Child>
                <wf:WebBrowser DocumentTitleChanged="wbWinForms_DocumentTitleChanged" />
            </WindowsFormsHost.Child>
        </WindowsFormsHost>
    </Grid>
</Window>

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2022-07-06
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多