在Windows8中编写c# /XAML与c++ /XAML WinRT应用程序的优缺点是什么?

时间:2022-09-02 09:05:29

I'd like to go down the route of porting a WPF/Silverlight component to Windows 8. For a bit of context, the component is a real-time WPF Chart, which uses a mixture of WPF/XAML and bitmap rendering to achieve high performance.

我想继续把WPF/Silverlight组件移植到Windows 8。对于一些上下文,这个组件是一个实时WPF图,它使用WPF/XAML和位图渲染的混合来实现高性能。

I'd like the component to be Metro compatible, e.g. used in metro mode as well as desktop mode. I read a lot about creating C++/WinRT applications in Windows 8 as well as C#/XAML applications, but what are the differences between the two frameworks?

我希望这个组件是Metro兼容的,例如在Metro模式和desktop模式中使用。我读了很多关于在Windows 8中创建c++ /WinRT应用程序以及c# /XAML应用程序的文章,但是这两个框架之间有什么区别呢?

Are there limitations if you choose C#/XAML over C++/XAML? Also consider the porting from C#/Xaml in .NET4.0 to Windows8 would be far easier if I could stick to C#/XAML, however will I be able to create a fully featured Metro component using this method?

如果您选择c# /XAML而不是c++ /XAML,会有什么限制吗?还要考虑到,如果我能坚持使用c# /Xaml,那么从.NET4.0中的c# /Xaml移植到Windows8会容易得多,但是我是否能够使用这种方法创建一个功能齐全的Metro组件?

Your comments/suggestions appreciated.

您的意见/建议表示赞赏。

Edit:

编辑:

If you're voting to close this thread, please post a comment why. Its a valid question, has +6 votes, four answers and one favourite. Seems reasonable to keep it to me!

如果你想关闭这个帖子,请发表评论。这是一个有效的问题,有+6票,4个答案和1个最喜欢的。对我来说似乎是合理的!

4 个解决方案

#1


22  

I see the difference as a design choice, than a personal preference of language. Preference would be more related to VB vs C#. And generally it's the same differences you get in any application where you choose C++ or .NET.

我把这种差异看作是一种设计选择,而不是个人对语言的偏好。偏好更多地与VB和c#相关。一般来说,在你选择c++或。net的任何应用程序中都有相同的差别。

C++ will give you faster startup times. IIRC, .NET 4.5 has auto NGENing abilities (not sure how it related to metro apps), so this may help mitigate typical slow startup times of .NET applications.

c++会给你更快的启动时间。net 4.5具有自动生成功能(不确定它与metro应用程序的关系),因此这可能有助于减少. net应用程序启动缓慢的典型情况。

C++ will give you lower general memory usage as it does not use a garbage collector. This becomes increasingly more important on resource constrained devices such as tablets. IIRC, .NET 4.5 has more mitigations into GC pauses (which can cause the UI to studder), they are still a reality with managed code.

由于不使用垃圾收集器,c++将降低一般内存使用量。这在资源受限的设备(如平板电脑)上变得越来越重要。net 4.5在GC暂停(这会导致UI抖动)方面有更多的缓解,它们仍然是托管代码的现实。

Since .NET and C++ use the same WinRT framework, there probably won't be too much difference in interacting with the XAML/WinRT platform (technically faster interacting with WinRT objects via C++ but the hit is really small), but of course your user code will generally be faster with C++ than .NET.

由于. net和c++使用了相同的WinRT框架,因此与XAML/WinRT平台交互可能不会有太大的差异(技术上通过c++与WinRT对象交互的速度更快,但影响非常小),但是当然,使用c++的用户代码通常比。net快。

C++ is generally more difficult to reverse engineer, even when compared with obfuscated .NET code. Though sly thieves can steal your IP regardless.

一般来说,即使与混淆的。net代码相比,使用c++进行逆向工程也更加困难。虽然狡猾的小偷可以偷你的IP不管怎样。

Since .NET was created first for the convenience of the developer and developer productivity, you will have more convenience options in architecting your applications (eg, reflection based tools such as DI/IoC).

由于。net是为了开发人员和开发人员的工作效率而创建的,所以在设计应用程序时(例如,基于反射的工具,例如DI/IoC),您将会有更多的便利选项。

Iterating application code may be easier via .NET as .NET compiles quicker than C++, but correctly created C++ projects this can be mitigated substantially.

通过。net编译,迭代应用程序代码可能更容易,因为。net编译速度比c++快,但是正确地创建了c++项目,这可以大大减轻。

Pure .NET projects can support "Any CPU", which means your application can run on all supported WinRT platforms. C++ projects you will simply have to recompile to support ARM, x86/64. If you .NET application depends on a custom C++ component, you will have to compile for each architecture.

纯。net项目可以支持“任何CPU”,这意味着您的应用程序可以在所有支持的WinRT平台上运行。您只需重新编译以支持ARM,即x86/64。如果. net应用程序依赖于自定义c++组件,则必须对每个体系结构进行编译。

Because WinRT was created from the ground up to support many languages, my suggestion to devs that arent comfortable with C++ is to stick with .NET but explore areas that benefit from C++. Microsoft has done a great job with the /CX projections and most C# devs should be able to find their way around. My suggestion to C++ devs is to stick with C++ and get all the benefits of C++.

因为WinRT是为支持多种语言而创建的,所以我对开发人员的建议是不要太喜欢c++,应该坚持使用。net,而应该探索一下c++的好处。微软在/CX预测方面做得很好,大多数c#开发人员应该能够找到自己的方法。我对c++开发人员的建议是坚持使用c++,获得c++的所有好处。

#2


3  

From a XAML perspective it becomes a language choice. The XAML UI stack is the same regardless of which code language you choose here. Depending on your goal of the app it might make more sense to use C++ if you need the benefits of what that language provides you.

从XAML的角度来看,它变成了一种语言选择。无论您在这里选择哪种代码语言,XAML UI堆栈都是相同的。如果您需要使用c++语言所提供的好处,那么根据应用程序的目标,使用c++可能更有意义。

We also have the ability to mix DirectX and XAML now in Win8 and that usually means C++ -- however with projects like SharpDX that still isn't totally valid (yes I realize you will be paying a performance hit in DirectX for wrapping in managed code...I'm just pointing out it can be done).

我们也有能力在Win8中混合DirectX和XAML,这通常意味着c++——然而,像SharpDX这样的项目仍然不是完全有效的(是的,我意识到你将会在DirectX中为在托管代码中包装而支付性能)。我只是指出这是可以做到的。

Your question seems to be about creating a re-usable component that can be used across the desktop and Metro. This may be somewhat challenging a bit depending on how you architect it because of how some changes were required to how resources (i.e., generic.xaml) are loaded from a file location versus an embedded resource.

您的问题似乎是关于创建可在桌面和Metro中使用的可重用组件。这可能有点挑战性,这取决于您如何构建它,因为资源(例如)如何需要一些更改。从文件位置加载,而不是从嵌入式资源加载。

#3


2  

The only advantage I can think of to use C++/XAML is if speed is important to your project. The advantage of C#/XAML is that is much more easier to code, especially if your project its in C# already.

我能想到的使用c++ /XAML的唯一优点是速度对项目很重要。c# /XAML的优点是代码更容易编写,特别是如果您的项目已经在c#中。

By now there is no way to make an application that targets both the Metro and the desktop in Windows 8.

到目前为止,还没有办法开发一个同时针对Metro和Windows 8桌面的应用程序。

Hope this helps.

希望这个有帮助。

#4


2  

Well others may know more, but based on Microsoft's answer to a question I posed back around WinRT announcement time:

其他人可能知道的更多,但是基于微软对我在WinRT发布时间提出的问题的回答:

WinRT is a protocol and a set of Native APIs, allowing each language to remain true to its existing execution environment - Chakra for JavaScript, CLR for C# and the CRT/raw native code for C++.

WinRT是一种协议和一组本地api,允许每种语言对其现有的执行环境保持真实——JavaScript的查克拉,c#的CLR和c++的CRT/raw本地代码。

That suggests similar performance trade-offs to what we currently experience using the CLR vs. native code to access what is essentially a native code API (WinRT). But I look forward to seeing some empirical investigations into this to see just how different WinRT is.

这与我们目前使用CLR和本机代码访问本质上是本机代码API (WinRT)的体验类似。但我期待着看到一些实证研究,看看WinRT有多么不同。

#1


22  

I see the difference as a design choice, than a personal preference of language. Preference would be more related to VB vs C#. And generally it's the same differences you get in any application where you choose C++ or .NET.

我把这种差异看作是一种设计选择,而不是个人对语言的偏好。偏好更多地与VB和c#相关。一般来说,在你选择c++或。net的任何应用程序中都有相同的差别。

C++ will give you faster startup times. IIRC, .NET 4.5 has auto NGENing abilities (not sure how it related to metro apps), so this may help mitigate typical slow startup times of .NET applications.

c++会给你更快的启动时间。net 4.5具有自动生成功能(不确定它与metro应用程序的关系),因此这可能有助于减少. net应用程序启动缓慢的典型情况。

C++ will give you lower general memory usage as it does not use a garbage collector. This becomes increasingly more important on resource constrained devices such as tablets. IIRC, .NET 4.5 has more mitigations into GC pauses (which can cause the UI to studder), they are still a reality with managed code.

由于不使用垃圾收集器,c++将降低一般内存使用量。这在资源受限的设备(如平板电脑)上变得越来越重要。net 4.5在GC暂停(这会导致UI抖动)方面有更多的缓解,它们仍然是托管代码的现实。

Since .NET and C++ use the same WinRT framework, there probably won't be too much difference in interacting with the XAML/WinRT platform (technically faster interacting with WinRT objects via C++ but the hit is really small), but of course your user code will generally be faster with C++ than .NET.

由于. net和c++使用了相同的WinRT框架,因此与XAML/WinRT平台交互可能不会有太大的差异(技术上通过c++与WinRT对象交互的速度更快,但影响非常小),但是当然,使用c++的用户代码通常比。net快。

C++ is generally more difficult to reverse engineer, even when compared with obfuscated .NET code. Though sly thieves can steal your IP regardless.

一般来说,即使与混淆的。net代码相比,使用c++进行逆向工程也更加困难。虽然狡猾的小偷可以偷你的IP不管怎样。

Since .NET was created first for the convenience of the developer and developer productivity, you will have more convenience options in architecting your applications (eg, reflection based tools such as DI/IoC).

由于。net是为了开发人员和开发人员的工作效率而创建的,所以在设计应用程序时(例如,基于反射的工具,例如DI/IoC),您将会有更多的便利选项。

Iterating application code may be easier via .NET as .NET compiles quicker than C++, but correctly created C++ projects this can be mitigated substantially.

通过。net编译,迭代应用程序代码可能更容易,因为。net编译速度比c++快,但是正确地创建了c++项目,这可以大大减轻。

Pure .NET projects can support "Any CPU", which means your application can run on all supported WinRT platforms. C++ projects you will simply have to recompile to support ARM, x86/64. If you .NET application depends on a custom C++ component, you will have to compile for each architecture.

纯。net项目可以支持“任何CPU”,这意味着您的应用程序可以在所有支持的WinRT平台上运行。您只需重新编译以支持ARM,即x86/64。如果. net应用程序依赖于自定义c++组件,则必须对每个体系结构进行编译。

Because WinRT was created from the ground up to support many languages, my suggestion to devs that arent comfortable with C++ is to stick with .NET but explore areas that benefit from C++. Microsoft has done a great job with the /CX projections and most C# devs should be able to find their way around. My suggestion to C++ devs is to stick with C++ and get all the benefits of C++.

因为WinRT是为支持多种语言而创建的,所以我对开发人员的建议是不要太喜欢c++,应该坚持使用。net,而应该探索一下c++的好处。微软在/CX预测方面做得很好,大多数c#开发人员应该能够找到自己的方法。我对c++开发人员的建议是坚持使用c++,获得c++的所有好处。

#2


3  

From a XAML perspective it becomes a language choice. The XAML UI stack is the same regardless of which code language you choose here. Depending on your goal of the app it might make more sense to use C++ if you need the benefits of what that language provides you.

从XAML的角度来看,它变成了一种语言选择。无论您在这里选择哪种代码语言,XAML UI堆栈都是相同的。如果您需要使用c++语言所提供的好处,那么根据应用程序的目标,使用c++可能更有意义。

We also have the ability to mix DirectX and XAML now in Win8 and that usually means C++ -- however with projects like SharpDX that still isn't totally valid (yes I realize you will be paying a performance hit in DirectX for wrapping in managed code...I'm just pointing out it can be done).

我们也有能力在Win8中混合DirectX和XAML,这通常意味着c++——然而,像SharpDX这样的项目仍然不是完全有效的(是的,我意识到你将会在DirectX中为在托管代码中包装而支付性能)。我只是指出这是可以做到的。

Your question seems to be about creating a re-usable component that can be used across the desktop and Metro. This may be somewhat challenging a bit depending on how you architect it because of how some changes were required to how resources (i.e., generic.xaml) are loaded from a file location versus an embedded resource.

您的问题似乎是关于创建可在桌面和Metro中使用的可重用组件。这可能有点挑战性,这取决于您如何构建它,因为资源(例如)如何需要一些更改。从文件位置加载,而不是从嵌入式资源加载。

#3


2  

The only advantage I can think of to use C++/XAML is if speed is important to your project. The advantage of C#/XAML is that is much more easier to code, especially if your project its in C# already.

我能想到的使用c++ /XAML的唯一优点是速度对项目很重要。c# /XAML的优点是代码更容易编写,特别是如果您的项目已经在c#中。

By now there is no way to make an application that targets both the Metro and the desktop in Windows 8.

到目前为止,还没有办法开发一个同时针对Metro和Windows 8桌面的应用程序。

Hope this helps.

希望这个有帮助。

#4


2  

Well others may know more, but based on Microsoft's answer to a question I posed back around WinRT announcement time:

其他人可能知道的更多,但是基于微软对我在WinRT发布时间提出的问题的回答:

WinRT is a protocol and a set of Native APIs, allowing each language to remain true to its existing execution environment - Chakra for JavaScript, CLR for C# and the CRT/raw native code for C++.

WinRT是一种协议和一组本地api,允许每种语言对其现有的执行环境保持真实——JavaScript的查克拉,c#的CLR和c++的CRT/raw本地代码。

That suggests similar performance trade-offs to what we currently experience using the CLR vs. native code to access what is essentially a native code API (WinRT). But I look forward to seeing some empirical investigations into this to see just how different WinRT is.

这与我们目前使用CLR和本机代码访问本质上是本机代码API (WinRT)的体验类似。但我期待着看到一些实证研究,看看WinRT有多么不同。