【问题标题】:How to use a WinRT component in c# console(windows form) app?如何在 C# 控制台(Windows 窗体)应用程序中使用 WinRT 组件?
【发布时间】:2016-05-27 07:29:58
【问题描述】:

我有一个 c++ WinRT 组件,已添加到我的控制台应用程序参考中。它编译没有任何错误,但是在运行应用程序时出现以下错误

“System.TypeLoadException”类型的未处理异常发生在 mscorlib.dll

附加信息:找不到 Windows 运行时类型 'ProcessorInfoComponent.ProcessorInfoProvider'。 my error

这是我的代码:

.h 文件:

#pragma once

namespace ProcessorInfoComponent
{
public ref class ProcessorInfoProvider sealed
{
   public:
      bool IsNeonSupported();
};
}

.cpp 文件:

#include "pch.h"
#include "ProcessorInfoComponent.h"
using namespace ProcessorInfoComponent;

bool ProcessorInfoProvider::IsNeonSupported()
{
     return IsProcessorFeaturePresent(PF_ARM_VFP_32_REGISTERS_AVAILABLE);
}

在c#中使用的是: .cs

    static void Main(string[] args)

    {

        var processorInfoProvider = new ProcessorInfoComponent.ProcessorInfoProvider();

        var isNeonSupported = processorInfoProvider.IsNeonSupported();

        Console.WriteLine(isNeonSupported);

    }

此 WinRT 在 windows phone 和 WPF APP 中运行良好,但在 windows 窗体和控制台应用程序中无法运行。 谢谢。

【问题讨论】:

    标签: c# windows-runtime console


    【解决方案1】:

    为了将来参考,您应该在 MSDN 页面上阅读 TypeLoadException 并在发生错误的地方放置 try...catch(Exception ex) 并查看 ex.message,这通常是捕获任何错误的好方法可能正在发生。

    就您遇到的错误而言,我很确定您无法混合使用 WinRT 和 WinForms/Console apss,因为它们以不同的方式执行并且 WinRT 是有限的,这意味着它不包括 .Net框架因此不能使用 WinForms 中使用的许多功能。

    此外,它能够正确编译的原因在于 C# 编译和运行程序的方式。 C# 编译器将代码编译为Module,然后编译为assembly,其中包含Intermediate Language 和一些Metadata。简而言之,程序因为没有语法错误而编译,但在执行var processorInfoProvider = new ProcessorInfoComponent.ProcessorInfoProvider();行时遇到了运行时错误。

    查看this 网站以获取有关 WinRT 工作原理的更多信息。 或this 网站了解有关 C# 编译器的更多信息。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2011-10-09
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多