对象不包含getelementbyname的定义,也没有C#中的扩展方法 [英] Object does not contain a definition for getelementbynameand no extension method in C#

查看:72
本文介绍了对象不包含getelementbyname的定义,也没有C#中的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预> 

使用System;使用System.Drawing
;
使用System.Drawing.Drawing2D;
使用System.Text;
使用System.Windows.Forms;


名称空间MindFusion.Gauges.WinForms.Samples.CS.CarGauges
{
public partial class MainForm:Form
{
public MainForm ()
{
InitializeComponent();

//添加燃料比例指标
fuelIndicator = new Indicator();
fuelIndicator.Margin =新厚度(0.47f,0.85f,0.47f,0.09f);
CustomInterval state = new CustomInterval();
state.MinValue = 0;
state.MaxValue = 20;
state.Fill = new MindFusion.Drawing.SolidBrush(Color.Red);
state.Stroke = new MindFusion.Drawing.Pen(Color.White,0);
state.Foreground = Color.Red;
fuelIndicator.States.Add(state);

OvalScale fuelScale = speedometer.GetElementByName(FuelScale)as OvalScale;
fuelScale.ScaleChildren.Add(fuelIndicator);

//添加电池和油文本
文本text1 = new Text();
text1.Content =Volt;
text1.FontFamily =Verdana;
text1.FontSize = new Length(25,LengthType.Relative);
text1.FontStyle = FontStyle.Bold;
text1.Alignment = StringAlignment.Center;
text1.LineAlignment = StringAlignment.Center;
text1.Foreground = Color.White;
text1.Margin = new Thickness(0.3f,0.3f,0.3f,0.3f);

OvalScale batteryScale = batteryMeter.GetElementByName(BatteryScale)as OvalScale;
batteryScale.ScaleChildren.Add(text1);

Text text2 = new Text();
text2.Content =Oil;
text2.FontFamily =Verdana;
text2.FontSize = new Length(25,LengthType.Relative);
text2.FontStyle = FontStyle.Bold;
text2.Alignment = StringAlignment.Center;
text2.LineAlignment = StringAlignment.Center;
text2.Foreground = Color.White;
text2.Margin = new Thickness(0.3f,0.3f,0.3f,0.3f);

OvalScale oilScale = oilMeter.GetElementByName(OilScale)as OvalScale;
oilScale.ScaleChildren.Add(text2);

//添加一些事件处理程序
fuelScale.QueryLabelValue + = fuelScale_QueryLabelValue;

指针gasPointer = speedometer.GetElementByName(GasPointer)作为指针;
gasPointer.ValueChanged + = gasPointer_ValueChanged;
}

private void gasPointer_ValueChanged(object sender,ValueChangedEventArgs< float> e)
{
fuelIndicator.Value = e.NewValue;
}

私人指标fuelIndicator;
私人物体速度计;
私人物品电池表;
私人物品oilMeter;
私人物品batteryTrack;
私人物品fuelTrack;
私有对象rpmTrack;
私人物品oilTrack;
私人对象speedTrack;
私人物体循环仪;

private void fuelScale_QueryLabelValue(object sender,QueryLabelValueEventArgs e)
{
if(e.Settings.TickType == TickType.Major)
{
if (e.CalculatedLabelValue == 0)
e.NewValue =E;
if(e.CalculatedLabelValue == 100)
e.NewValue =F;
}
}

private void speedometer_PrepaintPointer(object sender,PrepaintEventArgs e)
{
e.CancelDefaultPainting = true;

指针指针= e.Element as Pointer;

RectangleF psize = Utils.ToRectangleF(pointer.RenderSize);
Brush fill1 = pointer.Fill.CreateGdiBrush(psize);
Pen stroke1 = pointer.Stroke.CreateGdiPen();

e.Graphics.FillEllipse(fill1,0,0,0.2f * psize.Width,psize.Height);
e.Graphics.DrawEllipse(stroke1,0,0,0.2f * psize.Width,psize.Height);

stroke1.Dispose();
fill1.Dispose();

PointF [] needle = new PointF []
{
new PointF(0,0.425f * psize.Height),
new PointF(0,0.575f * psize.Height),
新PointF(0.95f * psize.Width,0.575f * psize.Height),
新PointF(psize.Width,0.5f * psize.Height),
新的PointF(0.95f * psize.Width,0.425f * psize.Height)
};

刷子fill2 =新的SolidBrush(Color.FromArgb(0xFF,0x33,0x33));
笔划2 =新笔(Color.DarkRed,0);

e.Graphics.FillPolygon(fill2,needle);
e.Graphics.DrawPolygon(stroke2,needle);

stroke2.Dispose();
fill2.Dispose();
}

private void speedometer_PrepaintBackground(object sender,PrepaintEventArgs e)
{
e.CancelDefaultPainting = true;

SizeF size = e.Element.RenderSize;
RectangleF bounds = Utils.ToRectangleF(size);

// ---------------------- Ellipse#1 --------------- -------
LinearGradientBrush fill1 = new LinearGradientBrush(new PointF(),Utils.ToPointF(size),
Color.Transparent,Color.Transparent);

ColorBlend fill1Blend = new ColorBlend();
fill1Blend.Positions = new float [] {0,0.2f,0.8f,1};
fill1Blend.Colors = new Color [] {Color.FromArgb(0x90,0x90,0x90),Color.FromArgb(0x90,0x90,0x90),
Color.FromArgb(0x30,0x30,0x30), Color.FromArgb(0x30,0x30,0x30)};
fill1.InterpolationColors = fill1Blend;

e.Graphics.FillEllipse(fill1,bounds);

fill1.Dispose();

//申请保证金0.015
bounds.Inflate(-0.015f * size.Width,-0.015f * size.Height);

// ---------------------- Ellipse#2 --------------- -------
LinearGradientBrush fill2 = new LinearGradientBrush(new PointF(),Utils.ToPointF(size),
Color.Transparent,Color.Transparent);

ColorBlend fill2Blend = new ColorBlend();
fill2Blend.Positions = new float [] {0,0.2f,0.8f,1};
fill2Blend.Colors = new Color [] {
Color.FromArgb(0x30,0x30,0x30),Color.FromArgb(0x30,0x30,0x30),
Color.FromArgb(0x90,0x90) ,0x90),Color.FromArgb(0x90,0x90,0x90)};
fill2.InterpolationColors = fill2Blend;

e.Graphics.FillEllipse(fill2,bounds);

fill2.Dispose();

//申请保证金0.015
bounds.Inflate(-0.015f * size.Width,-0.015f * size.Height);

// ---------------------- Ellipse#3 --------------- -------
GraphicsPath fill3Path = new GraphicsPath();
fill3Path.AddEllipse(bounds);

PathGradientBrush fill3 = new PathGradientBrush(fill3Path);
fill3.CenterPoint = new PointF(0.5f,0.3f);

ColorBlend fill3Blend = new ColorBlend();
fill3Blend.Positions = new float [] {0,1};
fill3Blend.Colors = new Color [] {
Color.FromArgb(0x20,0x20,0x20),Color.FromArgb(0xBB,0xBB,0xBB)};
fill3.InterpolationColors = fill3Blend;

e.Graphics.FillEllipse(fill3,bounds);

fill3.Dispose();
}

private void speedometer_PrepaintForeground(object sender,PrepaintEventArgs e)
{
e.CancelDefaultPainting = true;

RectangleF bounds = Utils.ToRectangleF(e.Element.RenderSize);

GraphicsPath areaPath = new GraphicsPath();
areaPath.AddEllipse(bounds);

MindFusion.Drawing.PathGradientBrush area1Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(153,Color.White),Color.Transparent);
area1Fill.Path = areaPath;

MindFusion.Drawing.PathGradientBrush area2Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(32,Color.White),Color.Transparent);
area2Fill.Path = areaPath;

ArcArea area1 = new ArcArea();
area1.RelativeCoordinates = false;
area1.StartAngle = 160;
area1.EndAngle = -20;
area1.Margin = new Thickness(0.03f);
area1.Fill = area1Fill;

ArcArea area2 = new ArcArea();
area2.RelativeCoordinates = false;
area2.StartAngle = 140;
area2.EndAngle = -40;
area2.Margin = new Thickness(0.03f);
area2.Fill = area2Fill;

e.PaintVisualElement(area1,e.Element.RenderSize);
e.PaintVisualElement(area2,e.Element.RenderSize);

areaPath.Dispose();
}

private void speedometer_PrepaintScale(object sender,PrepaintEventArgs e)
{
OvalScale scale = e.Element as OvalScale;
if(scale.Name ==FuelScale)
{
//绘制加油站图标
PathFigure figure = new PathFigure(M0,0.08 C0,0.03 0.05, 0 0.1,0 L0.45,0 C0.5,0 0.55,0.03+
0.55,0.08 L0.55,0.41 L0.68,0.41 C0.74,0.41 0.78,0.46 0.78,0.52 L0。 78,0.89 C0.78,0.96+
0.925,0.96 0.925,0.89 L0.86,0.6 L0.86,0.29 C0.86,0.27 0.86,0.26 0.84,0.24 L0.64,0.08+
L0.7,0.05 L0.9,0.21 C0.93,0.24 0.93,0.26 0.93,0.28 L0.93,0.58 L1,0.9 C0.98,1.04 0.72,1.04+
0.7, 0.9 L0.7,0.5 C0.7,0.47 0.67,0.47 0.66,0.47 L0.55,0.47 L0.55,1 L0,1 L0,0.08 M0.11,0.06+
L0.44,0.06 C0.46,0.06 0.47,0.07 0.47,0.09 L0.47,0.29 C0.47,0.3 0.46,0.32 0.44,0.32 L0.11,0.32+
C0.09,0.32 0.08,0.31 0.08,0.29 L0.08,0.09 C0.08,0.07 0.09,0.06 0.11,0.06 z);

figure.Stroke = new MindFusion.Drawing.Pen(Color.Transparent,0);
figure.Fill = new MindFusion.Drawing.SolidBrush(Color.White);
figure.Margin = new Thickness(0.46f,0.73f,0.46f,0.17f);

e.PaintVisualElement(图,e.Element.RenderSize);
}
}

private void batteryMeter_PrepaintBackground(object sender,PrepaintEventArgs e)
{
e.CancelDefaultPainting = true;

float widthExtent = 0;
if(batteryMeter.width> batteryMeter.Height)
widthExtent =(float)(batteryMeter.Width - batteryMeter.Height)/ 2;

// RectangleF bounds = new RectangleF(-widthExtent,0,1 + 2 * widthExtent,1);

//矩形#1
// LinearGradientBrush fill1 =新的LinearGradientBrush(
//新的PointF(-widthExtent,0),新的PointF(1 + 2 * widthExtent,1 ),Color.Transparent,Color.Transparent);

// ColorBlend fill1Blend = new ColorBlend();
//fill1Blend.Positions = new float [] {0,0.2f,0.8f,1};
//fill1Blend.Colors = new Color [] {Color.FromArgb(0x90,0x90,0x90),Color.FromArgb(0x90,0x90,0x90),
// Color.FromArgb(0x30,0x30) ,0x30),Color.FromArgb(0x30,0x30,0x30)};
//fill1.InterpolationColors = fill1Blend;

//e.Graphics.FillRectangle (fill1,bounds);

//fill1.Dispose();

//bounds.Inflate(-0.03f,-0.03f);

RoundRectangle rect = new RoundRectangle();
rect.Name =BateryBackground;
rect.RelativeCoordinates = false;
rect.Roundness = 20;
//rect.Margin = new Thickness(-widthExtent,0,-widthExtent + 0.01f,0 + 0.01f);
rect.X = -widthExtent; ///2;
rect.Fill = e.CreateLinearGradient(新的PointF(),新的PointF(1,1),
0.2f,Color.FromArgb(0x90,0x90,0x90),0.8f,Color.FromArgb( 0x30,0x30,0x30));
e.PaintVisualElement(rect,batteryMeter.Size);

//矩形#2
// LinearGradientBrush fill2 =新的LinearGradientBrush(
//新的PointF(-widthExtent,0),新的PointF(1 + 2 * widthExtent,1 ),Color.Transparent,Color.Transparent);

// ColorBlend fill2Blend = new ColorBlend();
//fill2Blend.Positions = new float [] {0,0.2f,0.8f,1};
//fill2Blend.Colors = new Color [] {Color.FromArgb(0x30,0x30,0x30),Color.FromArgb(0x30,0x30,0x30),
// Color.FromArgb(0x90,0x90) ,0x90),Color.FromArgb(0x90,0x90,0x90)};
//fill2.InterpolationColors = fill2Blend;

//e.Graphics.FillRectangle(fill2,bounds);

//fill2.Dispose();

//bounds.Inflate(-0.03f,-0.03f);
rect = new RoundRectangle();
rect.Roundness = 20;
rect.Margin = new Thickness(0.015f * batteryMeter.Width,false);
rect.X = -widthExtent;
rect.Fill = e.CreateLinearGradient(新的PointF(0,0),新的PointF(1,1),
0.2f,Color.FromArgb(0x30,0x30,0x30),0.8f,颜色.FromArgb(0x90,0x90,0x90));
e.PaintVisualElement(rect,batteryMeter.Size);

//矩形#3
rect = new RoundRectangle();
rect.X = -widthExtent;
rect.Margin = new Thickness(0.03f * batteryMeter.Width,false);
rect.Roundness = 20;

//后台填充
rect.Fill = new MindFusion.Drawing.SolidBrush(Color.FromArgb(0x60,0x60,0x60));
e.PaintVisualElement(rect,batteryMeter.Size);

//径向填充
MindFusion.Drawing.PathGradientBrush fill = e.CreatePathGradient(新PointF(0.4f * batteryMeter.Width,0.3f * batteryMeter.Height),
0.2 f,Color.FromArgb(0x60,0x60,0x60),0.8f,Color.FromArgb(0xBB,0xBB,0xBB));
GraphicsPath fillPath = new GraphicsPath();
RectangleF bounds = new RectangleF(new Point(),batteryMeter.Size); // new RectangleF(-widthExtent,0,1 + 2 * widthExtent,1);
bounds.Inflate(-0.06f * bounds.Width,-0.06f * bounds.Height);
fillPath.AddEllipse(bounds);
fill.Path = fillPath;
rect.Fill = fill;
e.PaintVisualElement(rect,batteryMeter.Size);
fillPath.Dispose();
}

private void batteryMeter_PrepaintForeground(object sender,PrepaintEventArgs e)
{
e.CancelDefaultPainting = true;

GraphicsPath areaPath = new GraphicsPath();
areaPath.AddEllipse(Utils.ToRectangleF(batteryMeter.Size));

MindFusion.Drawing.PathGradientBrush area1Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(153,Color.White),Color.Transparent);
area1Fill.Path = areaPath;

MindFusion.Drawing.PathGradientBrush area2Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(32,Color.White),Color.Transparent);
area2Fill.Path = areaPath;

ArcArea area1 = new ArcArea();
area1.RelativeCoordinates = false;
area1.StartAngle = 160;
area1.EndAngle = -20;
area1.Margin = new Thickness(0.03f);
area1.Fill = area1Fill;

ArcArea area2 = new ArcArea();
area2.RelativeCoordinates = false;
area2.StartAngle = 140;
area2.EndAngle = -40;
area2.Margin = new Thickness(0.03f);
area2.Fill = area2Fill;

e.PaintVisualElement(area1,e.Element.RenderSize);
e.PaintVisualElement(area2,e.Element.RenderSize);

areaPath.Dispose();
}

private void speedTrack_Scroll(object sender,EventArgs e)
{
指针speedPointer = speedometer.GetElementByName(SpeedPointer)作为指针;
speedPointer.Value = speedTrack.Value;
}

private void fuelTrack_Scroll(object sender,EventArgs e)
{
指针gasPointer = speedometer.GetElementByName(GasPointer)as Pointer;
gasPointer.Value = fuelTrack.Value;
}

private void rpmTrack_Scroll(object sender,EventArgs e)
{
指针rpmPointer = cyclometer.GetElementByName(RpmPointer)as Pointer;
rpmPointer.Value = rpmTrack.Value;
}

private void batteryTrack_Scroll(object sender,EventArgs e)
{
指针batteryPointer = batteryMeter.GetElementByName(BatteryPointer)作为指针;
batteryPointer.Value = batteryTrack.Value;
}

private void oilTrack_Scroll(object sender,EventArgs e)
{
Pointer oilPointer = oilMeter.GetElementByName(OilPointer)as Pointer;
oilPointer.Value = oilTrack.Value;
}
}
}





我尝试了什么:



对象不包含GetElementByName的定义,c#中没有扩展方法

解决方案

您将车速表声明为对象:

  private   object  speedometer; 

这意味着无论它包含什么类型的对象,你都只能访问对象属性和方法。为了访问实际的类属性,您需要将对象变量强制转换为所需的类,或者(更好地)将其声明为正确的类开始:

  object  o =   Hello World!< /跨度>; 
Console.WriteLine((( string )o).Substring( 0 5 ));



  string  o =   Hello World!; 
Console.WriteLine(o.Substring( 0 5 ));


<pre>

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;


namespace MindFusion.Gauges.WinForms.Samples.CS.CarGauges
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            // Add fuel scale indicator
            fuelIndicator = new Indicator();
            fuelIndicator.Margin = new Thickness(0.47f, 0.85f, 0.47f, 0.09f);
            CustomInterval state = new CustomInterval();
            state.MinValue = 0;
            state.MaxValue = 20;
            state.Fill = new MindFusion.Drawing.SolidBrush(Color.Red);
            state.Stroke = new MindFusion.Drawing.Pen(Color.White, 0);
            state.Foreground = Color.Red;
            fuelIndicator.States.Add(state);

            OvalScale fuelScale = speedometer.GetElementByName("FuelScale") as OvalScale;
            fuelScale.ScaleChildren.Add(fuelIndicator);

            // Add Battery and Oil texts
            Text text1 = new Text();
            text1.Content = "Volt";
            text1.FontFamily = "Verdana";
            text1.FontSize = new Length(25, LengthType.Relative);
            text1.FontStyle = FontStyle.Bold;
            text1.Alignment = StringAlignment.Center;
            text1.LineAlignment = StringAlignment.Center;
            text1.Foreground = Color.White;
            text1.Margin = new Thickness(0.3f, 0.3f, 0.3f, 0.3f);

            OvalScale batteryScale = batteryMeter.GetElementByName("BatteryScale") as OvalScale;
            batteryScale.ScaleChildren.Add(text1);

            Text text2 = new Text();
            text2.Content = "Oil";
            text2.FontFamily = "Verdana";
            text2.FontSize = new Length(25, LengthType.Relative);
            text2.FontStyle = FontStyle.Bold;
            text2.Alignment = StringAlignment.Center;
            text2.LineAlignment = StringAlignment.Center;
            text2.Foreground = Color.White;
            text2.Margin = new Thickness(0.3f, 0.3f, 0.3f, 0.3f);

            OvalScale oilScale = oilMeter.GetElementByName("OilScale") as OvalScale;
            oilScale.ScaleChildren.Add(text2);

            // Add some event handlers
            fuelScale.QueryLabelValue += fuelScale_QueryLabelValue;

            Pointer gasPointer = speedometer.GetElementByName("GasPointer") as Pointer;
            gasPointer.ValueChanged += gasPointer_ValueChanged;
        }

        private void gasPointer_ValueChanged(object sender, ValueChangedEventArgs<float> e)
        {
            fuelIndicator.Value = e.NewValue;
        }

        private Indicator fuelIndicator;
        private object speedometer;
        private object batteryMeter;
        private object oilMeter;
        private object batteryTrack;
        private object fuelTrack;
        private object rpmTrack;
        private object oilTrack;
        private object speedTrack;
        private object cyclometer;

        private void fuelScale_QueryLabelValue(object sender, QueryLabelValueEventArgs e)
        {
            if (e.Settings.TickType == TickType.Major)
            {
                if (e.CalculatedLabelValue == 0)
                    e.NewValue = "E";
                if (e.CalculatedLabelValue == 100)
                    e.NewValue = "F";
            }
        }

        private void speedometer_PrepaintPointer(object sender, PrepaintEventArgs e)
        {
            e.CancelDefaultPainting = true;

            Pointer pointer = e.Element as Pointer;

            RectangleF psize = Utils.ToRectangleF(pointer.RenderSize);
            Brush fill1 = pointer.Fill.CreateGdiBrush(psize);
            Pen stroke1 = pointer.Stroke.CreateGdiPen();

            e.Graphics.FillEllipse(fill1, 0, 0, 0.2f * psize.Width, psize.Height);
            e.Graphics.DrawEllipse(stroke1, 0, 0, 0.2f * psize.Width, psize.Height);

            stroke1.Dispose();
            fill1.Dispose();

            PointF[] needle = new PointF[]
            {
                new PointF(0, 0.425f * psize.Height),
                new PointF(0, 0.575f * psize.Height),
                new PointF(0.95f * psize.Width, 0.575f * psize.Height),
                new PointF(psize.Width, 0.5f * psize.Height),
                new PointF(0.95f * psize.Width, 0.425f * psize.Height)
            };

            Brush fill2 = new SolidBrush(Color.FromArgb(0xFF, 0x33, 0x33));
            Pen stroke2 = new Pen(Color.DarkRed, 0);

            e.Graphics.FillPolygon(fill2, needle);
            e.Graphics.DrawPolygon(stroke2, needle);

            stroke2.Dispose();
            fill2.Dispose();
        }

        private void speedometer_PrepaintBackground(object sender, PrepaintEventArgs e)
        {
            e.CancelDefaultPainting = true;

            SizeF size = e.Element.RenderSize;
            RectangleF bounds = Utils.ToRectangleF(size);

            // ---------------------- Ellipse #1 ----------------------
            LinearGradientBrush fill1 = new LinearGradientBrush(new PointF(), Utils.ToPointF(size),
                Color.Transparent, Color.Transparent);

            ColorBlend fill1Blend = new ColorBlend();
            fill1Blend.Positions = new float[] { 0, 0.2f, 0.8f, 1 };
            fill1Blend.Colors = new Color[] { Color.FromArgb(0x90, 0x90, 0x90), Color.FromArgb(0x90, 0x90, 0x90),
                Color.FromArgb(0x30, 0x30, 0x30), Color.FromArgb(0x30, 0x30, 0x30) };
            fill1.InterpolationColors = fill1Blend;

            e.Graphics.FillEllipse(fill1, bounds);

            fill1.Dispose();

            // Apply margin of 0.015
            bounds.Inflate(-0.015f * size.Width, -0.015f * size.Height);

            // ---------------------- Ellipse #2 ----------------------
            LinearGradientBrush fill2 = new LinearGradientBrush(new PointF(), Utils.ToPointF(size),
                Color.Transparent, Color.Transparent);

            ColorBlend fill2Blend = new ColorBlend();
            fill2Blend.Positions = new float[] { 0, 0.2f, 0.8f, 1 };
            fill2Blend.Colors = new Color[] {
                Color.FromArgb(0x30, 0x30, 0x30), Color.FromArgb(0x30, 0x30, 0x30),
                Color.FromArgb(0x90, 0x90, 0x90), Color.FromArgb(0x90, 0x90, 0x90) };
            fill2.InterpolationColors = fill2Blend;

            e.Graphics.FillEllipse(fill2, bounds);

            fill2.Dispose();

            // Apply margin of 0.015
            bounds.Inflate(-0.015f * size.Width, -0.015f * size.Height);

            // ---------------------- Ellipse #3 ----------------------
            GraphicsPath fill3Path = new GraphicsPath();
            fill3Path.AddEllipse(bounds);

            PathGradientBrush fill3 = new PathGradientBrush(fill3Path);
            fill3.CenterPoint = new PointF(0.5f, 0.3f);

            ColorBlend fill3Blend = new ColorBlend();
            fill3Blend.Positions = new float[] { 0, 1 };
            fill3Blend.Colors = new Color[] {
                Color.FromArgb(0x20, 0x20, 0x20), Color.FromArgb(0xBB, 0xBB, 0xBB) };
            fill3.InterpolationColors = fill3Blend;

            e.Graphics.FillEllipse(fill3, bounds);

            fill3.Dispose();
        }

        private void speedometer_PrepaintForeground(object sender, PrepaintEventArgs e)
        {
            e.CancelDefaultPainting = true;

            RectangleF bounds = Utils.ToRectangleF(e.Element.RenderSize);

            GraphicsPath areaPath = new GraphicsPath();
            areaPath.AddEllipse(bounds);

            MindFusion.Drawing.PathGradientBrush area1Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(153, Color.White), Color.Transparent);
            area1Fill.Path = areaPath;

            MindFusion.Drawing.PathGradientBrush area2Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(32, Color.White), Color.Transparent);
            area2Fill.Path = areaPath;

            ArcArea area1 = new ArcArea();
            area1.RelativeCoordinates = false;
            area1.StartAngle = 160;
            area1.EndAngle = -20;
            area1.Margin = new Thickness(0.03f);
            area1.Fill = area1Fill;

            ArcArea area2 = new ArcArea();
            area2.RelativeCoordinates = false;
            area2.StartAngle = 140;
            area2.EndAngle = -40;
            area2.Margin = new Thickness(0.03f);
            area2.Fill = area2Fill;

            e.PaintVisualElement(area1, e.Element.RenderSize);
            e.PaintVisualElement(area2, e.Element.RenderSize);

            areaPath.Dispose();
        }

        private void speedometer_PrepaintScale(object sender, PrepaintEventArgs e)
        {
            OvalScale scale = e.Element as OvalScale;
            if (scale.Name == "FuelScale")
            {
                // Paint the gas station icon
                PathFigure figure = new PathFigure("M0,0.08 C0,0.03 0.05,0 0.1,0 L0.45,0 C0.5,0 0.55,0.03 " +
                    "0.55,0.08 L0.55,0.41 L0.68,0.41 C0.74,0.41 0.78,0.46 0.78,0.52 L0.78,0.89 C0.78,0.96 " +
                    "0.925,0.96 0.925,0.89 L0.86,0.6 L0.86,0.29 C0.86,0.27 0.86,0.26 0.84,0.24 L0.64,0.08 " +
                    "L0.7,0.05 L0.9,0.21 C0.93,0.24 0.93,0.26 0.93,0.28 L0.93,0.58 L1,0.9 C0.98,1.04 0.72,1.04 " +
                    "0.7,0.9 L0.7,0.5 C0.7,0.47 0.67,0.47 0.66,0.47 L0.55,0.47 L0.55,1 L0,1 L0,0.08 M0.11,0.06 " +
                    "L0.44,0.06 C0.46,0.06 0.47,0.07 0.47,0.09 L0.47,0.29 C0.47,0.3 0.46,0.32 0.44,0.32 L0.11,0.32 " +
                    "C0.09,0.32 0.08,0.31 0.08,0.29 L0.08,0.09 C0.08,0.07 0.09,0.06 0.11,0.06 z");

                figure.Stroke = new MindFusion.Drawing.Pen(Color.Transparent, 0);
                figure.Fill = new MindFusion.Drawing.SolidBrush(Color.White);
                figure.Margin = new Thickness(0.46f, 0.73f, 0.46f, 0.17f);

                e.PaintVisualElement(figure, e.Element.RenderSize);
            }
        }

        private void batteryMeter_PrepaintBackground(object sender, PrepaintEventArgs e)
        {
            e.CancelDefaultPainting = true;

            float widthExtent = 0;
            if (batteryMeter.width > batteryMeter.Height)
                widthExtent = (float)(batteryMeter.Width - batteryMeter.Height) / 2;

            //RectangleF bounds = new RectangleF(-widthExtent, 0, 1 + 2 * widthExtent, 1);

            // Rectangle #1
            //LinearGradientBrush fill1 = new LinearGradientBrush(
            //    new PointF(-widthExtent, 0), new PointF(1 + 2 * widthExtent, 1), Color.Transparent, Color.Transparent);

            //ColorBlend fill1Blend = new ColorBlend();
            //fill1Blend.Positions = new float[] { 0, 0.2f, 0.8f, 1 };
            //fill1Blend.Colors = new Color[] { Color.FromArgb(0x90, 0x90, 0x90), Color.FromArgb(0x90, 0x90, 0x90),
            //    Color.FromArgb(0x30, 0x30, 0x30), Color.FromArgb(0x30, 0x30, 0x30) };
            //fill1.InterpolationColors = fill1Blend;

            //e.Graphics.FillRectangle(fill1, bounds);

            //fill1.Dispose();

            //bounds.Inflate(-0.03f, -0.03f);

            RoundRectangle rect = new RoundRectangle();
            rect.Name = "BateryBackground";
            rect.RelativeCoordinates = false;
            rect.Roundness = 20;
            //rect.Margin = new Thickness(-widthExtent, 0, -widthExtent + 0.01f, 0 + 0.01f);
            rect.X = -widthExtent;// / 2;
            rect.Fill = e.CreateLinearGradient(new PointF(), new PointF(1, 1),
                0.2f, Color.FromArgb(0x90, 0x90, 0x90), 0.8f, Color.FromArgb(0x30, 0x30, 0x30));
            e.PaintVisualElement(rect, batteryMeter.Size);

            // Rectangle #2
            //LinearGradientBrush fill2 = new LinearGradientBrush(
            //    new PointF(-widthExtent, 0), new PointF(1 + 2 * widthExtent, 1), Color.Transparent, Color.Transparent);

            //ColorBlend fill2Blend = new ColorBlend();
            //fill2Blend.Positions = new float[] { 0, 0.2f, 0.8f, 1 };
            //fill2Blend.Colors = new Color[] { Color.FromArgb(0x30, 0x30, 0x30), Color.FromArgb(0x30, 0x30, 0x30),
            //    Color.FromArgb(0x90, 0x90, 0x90), Color.FromArgb(0x90, 0x90, 0x90) };
            //fill2.InterpolationColors = fill2Blend;

            //e.Graphics.FillRectangle(fill2, bounds);

            //fill2.Dispose();

            //bounds.Inflate(-0.03f, -0.03f);
            rect = new RoundRectangle();
            rect.Roundness = 20;
            rect.Margin = new Thickness(0.015f * batteryMeter.Width, false);
            rect.X = -widthExtent;
            rect.Fill = e.CreateLinearGradient(new PointF(0, 0), new PointF(1, 1),
                0.2f, Color.FromArgb(0x30, 0x30, 0x30), 0.8f, Color.FromArgb(0x90, 0x90, 0x90));
            e.PaintVisualElement(rect, batteryMeter.Size);

            // Rectangle #3
            rect = new RoundRectangle();
            rect.X = -widthExtent;
            rect.Margin = new Thickness(0.03f * batteryMeter.Width, false);
            rect.Roundness = 20;

            // Background fill
            rect.Fill = new MindFusion.Drawing.SolidBrush(Color.FromArgb(0x60, 0x60, 0x60));
            e.PaintVisualElement(rect, batteryMeter.Size);

            // Radial fill
            MindFusion.Drawing.PathGradientBrush fill = e.CreatePathGradient(new PointF(0.4f * batteryMeter.Width, 0.3f * batteryMeter.Height),
                0.2f, Color.FromArgb(0x60, 0x60, 0x60), 0.8f, Color.FromArgb(0xBB, 0xBB, 0xBB));
            GraphicsPath fillPath = new GraphicsPath();
            RectangleF bounds = new RectangleF(new Point(), batteryMeter.Size); //new RectangleF(-widthExtent, 0, 1 + 2 * widthExtent, 1);
            bounds.Inflate(-0.06f * bounds.Width, -0.06f * bounds.Height);
            fillPath.AddEllipse(bounds);
            fill.Path = fillPath;
            rect.Fill = fill;
            e.PaintVisualElement(rect, batteryMeter.Size);
            fillPath.Dispose();
        }

        private void batteryMeter_PrepaintForeground(object sender, PrepaintEventArgs e)
        {
            e.CancelDefaultPainting = true;

            GraphicsPath areaPath = new GraphicsPath();
            areaPath.AddEllipse(Utils.ToRectangleF(batteryMeter.Size));

            MindFusion.Drawing.PathGradientBrush area1Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(153, Color.White), Color.Transparent);
            area1Fill.Path = areaPath;

            MindFusion.Drawing.PathGradientBrush area2Fill = new MindFusion.Drawing.PathGradientBrush(Color.FromArgb(32, Color.White), Color.Transparent);
            area2Fill.Path = areaPath;

            ArcArea area1 = new ArcArea();
            area1.RelativeCoordinates = false;
            area1.StartAngle = 160;
            area1.EndAngle = -20;
            area1.Margin = new Thickness(0.03f);
            area1.Fill = area1Fill;

            ArcArea area2 = new ArcArea();
            area2.RelativeCoordinates = false;
            area2.StartAngle = 140;
            area2.EndAngle = -40;
            area2.Margin = new Thickness(0.03f);
            area2.Fill = area2Fill;

            e.PaintVisualElement(area1, e.Element.RenderSize);
            e.PaintVisualElement(area2, e.Element.RenderSize);

            areaPath.Dispose();
        }

        private void speedTrack_Scroll(object sender, EventArgs e)
        {
            Pointer speedPointer = speedometer.GetElementByName("SpeedPointer") as Pointer;
            speedPointer.Value = speedTrack.Value;
        }

        private void fuelTrack_Scroll(object sender, EventArgs e)
        {
            Pointer gasPointer = speedometer.GetElementByName("GasPointer") as Pointer;
            gasPointer.Value = fuelTrack.Value;
        }

        private void rpmTrack_Scroll(object sender, EventArgs e)
        {
            Pointer rpmPointer = cyclometer.GetElementByName("RpmPointer") as Pointer;
            rpmPointer.Value = rpmTrack.Value;
        }

        private void batteryTrack_Scroll(object sender, EventArgs e)
        {
            Pointer batteryPointer = batteryMeter.GetElementByName("BatteryPointer") as Pointer;
            batteryPointer.Value = batteryTrack.Value;
        }

        private void oilTrack_Scroll(object sender, EventArgs e)
        {
            Pointer oilPointer = oilMeter.GetElementByName("OilPointer") as Pointer;
            oilPointer.Value = oilTrack.Value;
        }
    }
}



What I have tried:

object does not contain a definition for GetElementByNameand no extension method in c#

解决方案

You declare speedometer as an object:

private object speedometer;

Which means that no matter what kind of object it contains, you can only ever access object properties and methods. In order to access the actual class properties, you need to either cast the object variable to the class you want, or (better) declare it as the right class to start with:

object o = "Hello World!";
Console.WriteLine(((string) o).Substring(0, 5));


string o = "Hello World!";
Console.WriteLine(o.Substring(0, 5));


这篇关于对象不包含getelementbyname的定义,也没有C#中的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆