无法创建抽象类的实例-来自文档 [英] Cannot create an instance of an abstract class - From documentation
问题描述
我正在尝试使用Microsoft文档中提供的代码对Tiff图像进行解码:
I am trying to decode a Tiff image using the code provided in the Microsoft Documentation:
但是,当我进入这一行时:
However, when I get to this line:
Image myImage = new Image();
我收到一条错误消息,告诉我无法创建抽象类的实例.我已经知道您不能创建抽象类的实例,我想知道为什么在文档中这样做?
I get an error telling me that I cannot create an instance of an abstract class. I already knew you cannot create an instance of an abstract class, I'm wondering why is this in the documentation?
这是引起问题的整个代码块:
Here is the whole block of code that is causing problems:
Image myImage = new Image();
myImage.Source = bitmapSource;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(20);
它还说System.Drawing.Image不包含 .Source
, .Stretch
和 .Margin
的定义以为我需要使用其他名称空间?因此,当我查找 Image.Source
的文档时:
It also says that System.Drawing.Image does not contain a definition for .Source
, .Stretch
and .Margin
which got me thinking I need to use a different namespace? So when I looked up the documentation for Image.Source
:
http://msdn.microsoft.com/en-us/library/system.drawing.image(v = vs.110).aspx
它说它使用 System.Windows.Controls
命名空间,该命名空间当然是一个包含在 PresentationFramework.dll
-我已经拥有的类中定义的类的命名空间.
It says it uses the System.Windows.Controls
namespace which of course is a namespace that contains classes defined in PresentationFramework.dll
- which I already have.
有什么想法我想念的吗?
Any ideas what I am missing?
推荐答案
The documentation - which you linked to - contains, as you say, this line of code:
Image myImage = new Image();
您说得到此编译器错误:
for which you say you get this compiler error:
无法创建抽象类或接口的实例
Cannot create an instance of the abstract class or interface
这意味着您使用了错误的 Image
类.
This means you're using the wrong Image
class.
在.NET框架和周围的核心库中,只有两个这样的 Image
类:
In the .NET framework and surrounding core libraries, there's only 2 such Image
classes:
该框架将第一个用作图像解码和编码的基类,这对您的问题似乎很有希望,只是它是一个 true 基类,因此它是抽象的,解释您收到的错误消息.
The first is used by the framework as the base class for image decoding and encoding, which would look promising for your question, except that it is a true base class, so it is abstract, which explains the error message you got.
表示框架WPF使用另一个类来表示屏幕上的图像.但是,此类不是 抽象.
The other class is used by the presentation framework, WPF, to represent an image on screen. This class, however, is not abstract.
因此很可能您在此处使用了错误的类,并引入了 System.Drawing.Image
类,而您确实想使用 System.Windows.Controls.Image
类.
So very likely you're using the wrong class here and have pulled in the System.Drawing.Image
class whereas you really want to use the System.Windows.Controls.Image
class.
有几种方法可以解决此问题:
There are several ways to fix this problem:
-
在文件中添加
using
指令:
using System.Windows.Controls;
但是,这很可能会将现有的编译器错误替换为另一个错误,即
However, this will likely just replace the existing compiler error with another one, namely
图像"是"System.Drawing.Image"和"System.Windows.Controls.Image"之间的模糊引用
'Image' is an ambiguous reference between 'System.Drawing.Image' and 'System.Windows.Controls.Image'
要使现有代码确定您使用的是错误的类,您可能已经使用System.Drawing; 了.如果您可以删除它,那就太好了!否则,...
To get the existing code to figure out that you're using the wrong class you likely already have using System.Drawing;
. If you can remove that, great! Otherwise, ...
添加一个 using
别名以专门控制您正在谈论的 Image
类:
Add a using
alias to specifically control which Image
class you're talking about:
using Image = System.Windows.Controls.Image;
此可能有效,然后再次无效.如果可以,那就太好了!否则,...
This may work, and then again may not. If it does, great! Otherwise, ...
使用完整的命名空间为类的用法添加前缀:
Prefix the usage of the class with the full namespace:
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
这篇关于无法创建抽象类的实例-来自文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!