类型或命名空间名称' myCollection'无法找到

时间:2014-09-22 07:46:56

标签: c# arguments

似乎无法在收集部分解决此错误。有人能帮忙吗?它说无法找到myCollection,但我已经声明了静态! 我正在尝试将值放入图表软件(sparrowchart)中,以便每秒更新一次。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Collections;

namespace Project
{
public partial class temp : PhoneApplicationPage
{

    double Min = 50;
    double Max = 0;
    double[] AvgTemp = new double[101];

    public temp()
    {
        InitializeComponent();
    }

    private void Pivot_Loaded(object sender, RoutedEventArgs e)
    {
        {
            DispatcherTimer time = new DispatcherTimer();
            time.Tick += time_Tick;
            time.Interval = TimeSpan.FromSeconds(1);
            time.Start();
        }
    }

    int a = 0,t=0;
    void time_Tick(object sender, EventArgs e)
    {
        DispatcherTimer time = new DispatcherTimer();
        Random rnd = new Random();
        double TempRandom = rnd.Next(100, 500); //generate random number
        double TempRandom2 = TempRandom / 10;


        CurrentTemp.Text = TempRandom2.ToString("0.0");
        ViewModel.myCollection.Add(new Model(t++, TempRandom2));
        var myCollection = new myCollection();
            if(myCollection[t] == 10)
            {
                myCollection.RemoveAt(t);
                t--;
            }


        if (TempRandom2 > Max)
        {
            Max = TempRandom2; //Highest Temp = Current Temp

            HighestTemp.Text = Max.ToString("0.0");
        }

        if (TempRandom2 < Min)
        {
            Min = TempRandom2;
            LowestTemp.Text = Min.ToString("0.0"); //Lowest Temp = Current Temp
        }

        AvgTemp[a++ % 100] = TempRandom2;
        if (a > 100)
            AverageTemp.Text = AvgTemp.Average().ToString("0.0");
        else
            AverageTemp.Text = (AvgTemp.Sum() / a).ToString("0.0");
    }
}

public class Model
{
    public double X { get; set; }
    public double Y { get; set; }

    public Model(double x, double y)
    {
        X = x;
        Y = y;
    }
}

// Create a ViewModel
public class ViewModel
{
    public static ObservableCollection<Model> myCollection { get; set; }
    public ViewModel()
    {
        myCollection = new ObservableCollection<Model>();
    }

}
    }

1 个答案:

答案 0 :(得分:1)

看起来您要删除特定索引,然后使用RemoveAt()

Collection.RemoveAt(t);

Remove(T item)需要T项,而RemoveAt(int index)需要索引。