【问题标题】:The type or namespace name 'System' does not exist in the namespace 'Windows.Win32'命名空间“Windows.Win32”中不存在类型或命名空间名称“System”
【发布时间】:2021-12-18 21:05:56
【问题描述】:

我有一个 .Net5.0-windows 项目,其中我有以下功能:

public static void EnableDisplayTimeout()
{
     PInvoke.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}

这需要使用智能感知识别的 Windows.Win32.System.Power,为我提供正确的导入,然后一切似乎都井井有条。当我尝试构建项目时,它失败并出现错误Error CS0234: The type or namespace name 'System' does not exist in the namespace 'Windows.Win32' (are you missing an assembly reference?) (2, 21)

我不确定我是否理解问题所在。我是 .NET 的新手,所以我不确定我是否了解 VS 的内部工作原理。我正在使用 VS 2019、CsWin32 NuGet 和 ReSharper 2021.1.3。我已经尝试禁用 ReSharper,但问题仍然存在。我是否缺少一些配置步骤?

【问题讨论】:

  • 你能提供你在这门课上使用的用途吗?问题似乎在某处
  • 尝试清理你的 bin 和 obj 文件夹并重建?确保你也恢复了所有的 nuget 包。
  • @TomaszJuszczak 我只使用 System.Windows、Windows.Win32 和 Windows.Win32.System.Power
  • @StriplingWarrior 我刚刚试过这些,同样的错误

标签: c# .net windows visual-studio resharper


【解决方案1】:

您确定Windows.Win32 命名空间?也许Microsoft.Win32 应该是?还是Windows.System.Power?或者什么都没有,因为SetThreadExecutionState 在那里不存在并且取自DllImportkernel32.dll

PInvoke.cs:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApp
{
    public class PInvoke
    {
        [Flags]
        public enum EXECUTION_STATE : uint
        {
            ES_AWAYMODE_REQUIRED = 0x00000040,
            ES_CONTINUOUS = 0x80000000,
            ES_DISPLAY_REQUIRED = 0x00000002,
            ES_SYSTEM_REQUIRED = 0x00000001
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
    }
}

及用法:

Program.cs:

using System;

namespace ConsoleApp
{
    public class Program
    {
        static void Main()
        {
            var executionState = PInvoke.SetThreadExecutionState(PInvoke.EXECUTION_STATE.ES_CONTINUOUS);
            Console.WriteLine(executionState);
            Console.ReadKey();
        }
    }
}

【讨论】:

  • 考虑到错误的性质,我不确定。如果我不使用 Windows.Win32,PInvoke 会被标记为“无法解析符号”,并且是智能提示建议导入 Windows.Win32。
【解决方案2】:

原来这是一个 sdk 错误。我升级到 .net sdk 6,问题就消失了。

根据这些找到解决方案:
https://github.com/microsoft/CsWin32/issues/7
https://github.com/dotnet/wpf/issues/3404

【讨论】:

    猜你喜欢
    • 2012-02-22
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多