【问题标题】:Can´t instantiate some .NET objects无法实例化一些 .NET 对象
【发布时间】:2015-08-13 10:27:00
【问题描述】:

我正在尝试实例化 System.Drawing.Graphics 对象,但由于某种原因 Visual Studio 找不到该类。 .NET 库中的某些类(但并非所有类)似乎都是这种情况。

我已经检查了目标框架并尝试使用命名空间进行实例化。此外,在 Visual Studio 菜单中没有解决它的选项,所以看起来 Visual Studio 甚至不知道对象。

示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Media;


namespace Notify.Classes
{
    class NotificationDrawer
    {
    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("User32.dll")]
    public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        IntPtr desktopPtr = GetDC(IntPtr.Zero);
        Graphics g = Graphics.FromHdc(desktopPtr);

        SolidBrush b = new SolidBrush(Color.White);
        g.FillRectangle(b, new Rectangle(0, 0, 1920, 1080));

        g.Dispose();
        ReleaseDC(IntPtr.Zero, desktopPtr);
    }
    }
}

【问题讨论】:

  • 如果我不得不打赌,那就是这个问题:stackoverflow.com/q/8553136/1348195
  • Graphics 没有公共构造函数。它有一些工厂方法你想做什么?
  • @BenjaminGruenbaum 不,不是,我已经检查过,正如我在问题中所说的那样。
  • @CharlesMager 没关系,这只是一个示例代码。找不到类,与缺少构造函数无关
  • 关于你的代码的一切都很奇怪。即使找到 Graphics 类,您的示例代码也不会编译。你正在尝试访问 Graphics 类的私有构造函数。

标签: c# .net


【解决方案1】:

根据documentationSystem.Drawing.GraphicsSystem.Drawing 程序集中。

确保您有对 System.Drawing 的引用。

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 2022-08-19
    • 2016-01-18
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多