无法解析符号 T

发布于 2024-08-02 01:57:46 字数 967 浏览 2 评论 0原文

我对 ObservableCollection 类有疑问。 我无法解决这个问题。

using System.Collections.ObjectModel;

    #region ViewModelProperty: CustomerList
    private ObservableCollection<T> _customerList = new ObservableCollection<T>();
    public ObservableCollection<T> CustomerList
    {
      get
      {
        return _customerList;
      }

      set
      {
        _customerList = value;
        OnPropertyChanged("CustomerList");
      }
    }
    #endregion

我的 ObservableCollection 类继承了 ViewModelBase:

  public abstract class ViewModelBase : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
      PropertyChangedEventHandler handler = PropertyChanged;

      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

知道吗,问题出在哪里?

I have a problem with ObservableCollection Class. I cant resolve this.

using System.Collections.ObjectModel;

    #region ViewModelProperty: CustomerList
    private ObservableCollection<T> _customerList = new ObservableCollection<T>();
    public ObservableCollection<T> CustomerList
    {
      get
      {
        return _customerList;
      }

      set
      {
        _customerList = value;
        OnPropertyChanged("CustomerList");
      }
    }
    #endregion

My class with the ObservableCollection inherits ViewModelBase:

  public abstract class ViewModelBase : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
      PropertyChangedEventHandler handler = PropertyChanged;

      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

any idea, where is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-08-09 01:57:46

T 只是一个占位符。 您需要在某处为 T 提供实际类型。

例如,如果您有 List,则可以创建 ListList (T code> 是符合给定约束的任何其他类型)。 即,在第一种情况下,Tint,在第二种情况下,Tstring

T is just a placeholder. You need to supply an actual type somewhere for T.

E.g. if you have List<T>, you could make a List<int> or List<string> (T is any other type which fits the given constraints). I.e. T is int in the first case and string in the second.

你又不是我 2024-08-09 01:57:46

我认为当您阅读一点 泛型< /a>

您可以这样做:

ObservableCollection<Customer> customerList = new ObservableCollection<Customer>()

然后,您有一个类型化集合,它将能够存储 Customer 类的实例(以及从 Customer 继承的子类的实例)。
因此,如果您想要一个能够添加多种类型实例的集合,您可以创建一个基类(或接口),并从该基类继承或实现此接口,然后创建一个 ObservableCollection例如,前。

I think that it will become clear to you when you read up a bit on Generics

You could do it like this:

ObservableCollection<Customer> customerList = new ObservableCollection<Customer>()

Then, you have a typed collection which will be able to store instances of the Customer class (and also instances of subclasses that inherit from Customer).
So, if you want to have a collection where you want to be able to add instances of multiple types, you could create a base-class (or interface), and inherit from that base-class or implement this interface, and create an ObservableCollection instance for ex.

一城柳絮吹成雪 2024-08-09 01:57:46

如前所述,T 是占位符。 你可能想要这样的东西:

private ObservableCollection<T> _customerList = new ObservableCollection < ClassYouWantObserved > ();

As previously posted, T is a placeholder. You probably want something like:

private ObservableCollection<T> _customerList = new ObservableCollection < ClassYouWantObserved > ();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文