简体   繁体   中英

How do I write this Hex Gradient in Codebehind?

I trying to get the same color gradient in C# code

        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="#FF4557BA" Offset="1"/>
        </LinearGradientBrush>

So far I have this but it is wrong(can't find how to enter in Hex so I tried argb)

  LinearGradientBrush gradient = new LinearGradientBrush();
    gradient.StartPoint = new Point( 0, 0 );
    gradient.EndPoint = new Point( 1, 1 );

    GradientStop color1 = new GradientStop();
    color1.Color = Colors.Black;
    color1.Offset = 0;
    gradient.GradientStops.Add(color1);

    GradientStop color2 = new GradientStop();
    color2.Color = Color.FromArgb(100,69,87,186);
    color2.Offset = 1;
    gradient.GradientStops.Add( color2 );

Edit

I am trying to do this in wp7 where I have this gradient in a property that I will bind to the "background" of my controls.

I however it just seems like I get a solid color and not the gradient.

You can try like this
 Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

by refering 
using System.Windows.Media;

So far this is what I got:

  LinearGradientBrush gradient = new LinearGradientBrush();
    gradient.StartPoint = new Point( 0.5, 0 );
    gradient.EndPoint = new Point( 0.5, 1 );

    gradient.GradientStops.Add(new GradientStop(Colors.Black, 0));
    gradient.GradientStops.Add(new GradientStop(Color.FromArgb(100,69,87,186), 1));


    whatevercontrolyougot.Fill = gradient;

Works fine here.

A screenshot:
在此处输入图片说明

The Problem is that you are refereeing alpha channel as 100 in code and in xamal you have 0xFF==255

so use this line in code

Color.FromArgb(0xFF, 0x45, 0x57, 0xBA);

C# allows you to enter values in Decimal, Binary or Hex,

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