简体   繁体   中英

How to deal with System.Drawing.Color <--> System.Windows.Media.Color ambiguous reference

I'm using System.Drawing to fill rectangles and draw lines and stuff. The System.Drawing.Color object only has a list of pre-defined colors, and I want to assign my own colors using RGB. So I've added the System.Windows.Media namespace, and now all references to "Color" say they're a ambiguous references.

I understand why. But I am wondering if there is a better solution than doing this

System.Windows.Media.Color colorVariableName;

wherever I reference a Color variable.

You're able to alias your usings at the top, so you can say something like

using MediaColor = System.Windows.Media.Color

And you'll be able to say

MediaColor colorVariableName

With System.Drawing.Color , you can do

Color c = Color.FromArgb(255,255,255);

to initialize color from your own R, G and B values and maybe you can skip System.Windows.Media.Color

Use Color.FromArgb(r, g, b)) to convert from rgb to System.Color. r,g, and b are ints

The other option would be to alias one of the namespaces in the using clause like so:

using System.Windows.Media = med;

Then you can access the colours with:

med.Blue

Also if it's just that particular class that you're interested in you can give that an alias in the using clause also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM