WPF 是否适用于 C++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4776355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:26:39  来源:igfitidea点击:

Does WPF Work with C++?

c++wpfvisual-studio

提问by Jonathan Wood

My understanding is that Microsoft Visual Studio was rewritten to use WPF. I'm still not clear on why, but acknowledge my knowledge about WPF is very limited.

我的理解是 Microsoft Visual Studio 被重写为使用 WPF。我仍然不清楚为什么,但承认我对 WPF 的了解非常有限。

My question is if anyone knows how much support WPF has for C++, and if Visual Studio is still written in C++.

我的问题是是否有人知道 WPF 对 C++ 有多少支持,以及 Visual Studio 是否仍然用 C++ 编写。

Personally, WPF primarily appears to be .NET/VB/C# thing. Is anyone using it with C++?

就个人而言,WPF 主要似乎是 .NET/VB/C# 的东西。有人在 C++ 中使用它吗?

回答by Reed Copsey

You can use WPF with C++/CLI. It is a .NET API, however, so it requires the .NET Framework.

您可以将 WPF 与 C++/CLI 结合使用。但是,它是一个 .NET API,因此它需要 .NET Framework。

That being said, the designer support is non-existent with C++. This means that, for practical purposes, WPF doesn't really work with C++.

话虽如此,C++ 不存在设计器支持。这意味着,出于实际目的,WPF 并不真正适用于 C++。

Typically, the user interface layer is written in C# (or VB.NET), then calls into C++ code, often exposed via P/Invoke or C++/CLI layers. By using C++/CLI, it's very easy to interoperate between C++ code and C#/VB.NET code.

通常,用户界面层是用 C#(或 VB.NET)编写的,然后调用 C++ 代码,通常通过 P/Invoke 或 C++/CLI 层公开。通过使用 C++/CLI,可以很容易地在 C++ 代码和 C#/VB.NET 代码之间进行互操作。

回答by jalf

WPF is a .NET technology. Of course it canbe used with C++, like any other part of .NET can, but it requires you to jump through some interop hoops, or possibly write it all in C++/CLI. (And you'll have to write a lot of boilerplate code yourself, as the designer doesn't work with C++/CLI.)

WPF 是一种 .NET 技术。当然,它可以与 C++ 一起使用,就像 .NET 的任何其他部分一样,但它需要您跳过一些互操作环节,或者可能全部用 C++/CLI 编写。(而且您必须自己编写大量样板代码,因为设计人员不使用 C++/CLI。)

And Visual Studio isn't, and probably never was, "written in C++". With 2010, members of the VS team have stated on their blogs that VS is now primarily a managed application. Of course there's still a ton of C++ code in there, and that's not going away any time soon, but a lot of it is C#/VB today.

Visual Studio 不是,也可能永远不会是“用 C++ 编写的”。在 2010 年,VS 团队的成员在他们的博客上表示 VS 现在主要是一个托管应用程序。当然,那里仍然有大量的 C++ 代码,而且不会很快消失,但今天有很多是 C#/VB。

But that didn't happen overnight. Managed code has gradually been added to Visual Studio with every release. Visual Studio is written in many different languages.

但这并不是一夜之间发生的。随着每个版本的发布,托管代码已逐渐添加到 Visual Studio 中。Visual Studio 是用多种不同的语言编写的。

If what you're actually asking is "can I write an addin for Visual Studio using C++", then the answer is "yes".

如果您实际问的是“我可以使用 C++ 为 Visual Studio 编写插件吗”,那么答案是“可以”。

If you're asking "is it practical to write an application in C++, and still use WPF", the answer is probably "only if you write the WPF code in C#, and then have some interop code binding this together with your C++ app.

如果您问“使用 C++ 编写应用程序并仍然使用 WPF 是否可行”,答案可能是“仅当您使用 C# 编写 WPF 代码,然后使用一些互操作代码将其与您的 C++ 应用程序绑定在一起时.

回答by scx

Noesis gui can run WPF UIs in c++. You will have to adapt the c# classes to c++ (using their reflection macros, etc). Some controls aren't supported, but it is quite elegant.

Noesis gui 可以在 C++ 中运行 WPF UI。您将不得不使 c# 类适应 c++(使用它们的反射宏等)。不支持某些控件,但它非常优雅。

For example, WPF might generate :

例如,WPF 可能会生成:

MainWindow.xaml.cs

主窗口.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Input;

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

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
        }

        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
        }

        private void ContainerBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
        }
    }
}

Then, you would convert it to c++ :

然后,您将其转换为 c++ :

namespace BlendTutorial
{

class MainWindow final: public Window
{
public:
    MainWindow()
    {
        InitializeComponent();
    }

private:
    void InitializeComponent()
    {
        Noesis::GUI::LoadComponent(this, "MainWindow.xaml");
    }

    bool ConnectEvent(BaseComponent* source, const char* event, const char* handler) override
    {
        NS_CONNECT_EVENT(Button, Click, AddButton_Click);
        NS_CONNECT_EVENT(Button, Click, RemoveButton_Click);
        NS_CONNECT_EVENT(Border, PreviewMouseLeftButtonDown, ContainerBorder_MouseDown);
        NS_CONNECT_ATTACHED_EVENT(ToggleButton, Checked, RadioButton_Checked);
        return false;
    }

    void AddButton_Click(BaseComponent*, const RoutedEventArgs&)
    {
    }

    void RemoveButton_Click(BaseComponent*, const RoutedEventArgs&)
    {
    }

    void ContainerBorder_MouseDown(BaseComponent*, const MouseButtonEventArgs&)
    {
    }

    void RadioButton_Checked(BaseComponent*, const RoutedEventArgs&)
    {
    }

    NS_IMPLEMENT_INLINE_REFLECTION(MainWindow, Window)
    {
        NsMeta<TypeId>("BlendTutorial.MainWindow");
    }
};

More info here : https://www.noesisengine.com/docs/Gui.Core.BlendTutorial.html

更多信息在这里:https: //www.noesisengine.com/docs/Gui.Core.BlendTutorial.html

They have some pretty nifty stuff if you want to go ham with data models, bindings and mvvp. Or you can just hook up lambdas to control events.

如果你想使用数据模型、绑定和 mvvp,它们有一些非常漂亮的东西。或者你可以连接 lambdas 来控制事件。

It is a paid framework, though it's free for less than 100K yearly income.

它是一个付费框架,但年收入低于 10 万是免费的。