Member 14120682 Ответов: 1

Объект не содержит определения для getelementbynameи никакого метода расширения в C#


<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;
        }
    }
}


Что я уже пробовал:

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

1 Ответов

Рейтинг:
1

OriginalGriff

Вы заявляете: speedometer в качестве объекта:

private object speedometer;
Это означает, что независимо от того, какой объект он содержит, вы можете получить доступ только к нему object свойства и методы. Чтобы получить доступ к фактическим свойствам класса, вам нужно либо привести переменную объекта к нужному классу, либо (лучше) объявить ее как правильный класс для начала:
object o = "Hello World!";
Console.WriteLine(((string) o).Substring(0, 5));

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


Richard Deeming

Черт возьми! Вы сделали ложь из обычного комментария о том, что никто не пробирается через массивную стену кода, как это, без указания того, на какой строке находится ошибка. :)

OriginalGriff

Прости! :смеяться:

Но это не помогло бы: он подошел бы ко всем случаям использования, но не там, где проблема была - определение. Сообщение об ошибке говорит: "это объект, идиот", так что этот идиот просто пошел искать определение с помощью CTRL+F.