混淆了自定义对象和UIElement之间的转换

时间:2017-04-25 14:44:03

标签: c# canvas casting uwp children

我的prog将对象添加到grid类型的画布中,实例称为vgridcargo。 我可以通过双击没有问题来删除它们。

但是,他们也会填充列表视图,并希望从画布中删除它们。

我遇到的问题是如何确定画布中特定vgridcargo(网格)对象的索引位置。

非常感谢任何帮助。感谢

如果我将它投射到UIElement,所有项目都会返回-1

var objectToDelete = sender as UIElement;
        //string objID = objectToDelete.vCargoID;
        ShowMessage("element is at index" + _canvasref.Children.IndexOf(objectToDelete));

如果我把它留下来

“无法从VGridCargo转换为Windows.UI.XAML.UI.Element”

var objectToDelete = sender as VGridCargo;
        //string objID = objectToDelete.vCargoID;
        ShowMessage("element is at index" + _canvasref.Children.IndexOf(objectToDelete));



`vgridcargo`. Class



class VGridCargo
{

    private Grid cargo;
    private Canvas _canvasRef;
    private double vCargoWidth;
    private double vCargoHeight;
    private Brush vCargoColour;
    public bool vCargoHeavy;
    public string vCargoDG;
    public string vCargoID;
    public string vCargoImgPath;
    public double vTonnage;
    private bool isActive;
    // private static Dictionary<string,object> = new
    private TextBlock vText = new TextBlock();
    private Windows.UI.Input.PointerPoint startPoint;
    private Point transOrigin = new Point(0.5, 0.5);
    private RotateTransform cargoRotate = new RotateTransform

    {
        CenterX=0,
        CenterY=0,
        Angle=90,

    };



    /*public VGridCargo(Canvas canvas)
    {
        _canvasRef = canvas;
    }*/

   public VGridCargo(Canvas canvas, double inHeight, double inWidth, Brush inColor, bool inHLift, string inPath, string inID,double inTonnage)
    {
        _canvasRef = canvas;
        vCargoWidth = inWidth;
        vCargoHeight = inHeight;
        vCargoColour = inColor;
        vCargoHeavy = inHLift;
        //vCargoDG = inDG;
        vCargoImgPath = inPath;
        vCargoID = inID;
        vTonnage = inTonnage;
    }


    public void CreateCargo()

    {

        cargo = new Grid();
        //Properties
        cargo.Height = Ratioconvert(vCargoHeight);
        cargo.Width = Ratioconvert(vCargoWidth);
        cargo.Name = vCargoID;
        cargo.Children.Add(vText);
        vText.FontSize = 5;
        vText.Text= vCargoID;
        //** Look into sort out color's and brush's in program mixed api's?
        Color myColor = (vCargoColour as SolidColorBrush).Color;
        cargo.Background = new SolidColorBrush(myColor);

        cargo.CanDrag = true;
        //Handlers

        cargo.RightTapped += Cargo_RightTapped;
        cargo.PointerPressed += Cargo_PointerPressed;
        cargo.PointerMoved += Cargo_PointerMoved;
        cargo.PointerReleased += Cargo_PointerReleased;
        cargo.DoubleTapped += Cargo_DoubleTapped;
       // vText.PointerEntered += VText_PointerEntered;
        //vText.PointerExited += VText_PointerExited;
        Canvas.SetLeft(cargo, 0);
        Canvas.SetTop(cargo, 0);
        _canvasRef.Children.Add(cargo);
        Stats.SetCargoCount();
        Stats.SetTonnageCount(vTonnage);



        if (vCargoHeavy)
        {
            Stats.SetHLCount();
        }

        if (!(vCargoDG==null))
        {
            Stats.SetDGCount();

        }
    }

代码

0 个答案:

没有答案