Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 结构和INotifyPropertyChanged_C#_Wpf_Mvvm - Fatal编程技术网

C# 结构和INotifyPropertyChanged

C# 结构和INotifyPropertyChanged,c#,wpf,mvvm,C#,Wpf,Mvvm,我正在尝试使用我的第一个MVVM应用程序向我的模型添加属性。 现在我想添加一个地方,以干净的方式保存特定的数据,所以我使用了一个struct。 但我在通知属性更改时遇到问题,它无法访问该方法(非静态字段需要对象引用) 有人能给我解释一下为什么会这样,并告诉我一个适合我需要的策略吗 谢谢 public ObservableCollection<UserControl> TimerBars { get { return _TimerBars; } set {

我正在尝试使用我的第一个MVVM应用程序向我的模型添加属性。 现在我想添加一个地方,以干净的方式保存特定的数据,所以我使用了一个struct。 但我在通知属性更改时遇到问题,它无法访问该方法(非静态字段需要对象引用) 有人能给我解释一下为什么会这样,并告诉我一个适合我需要的策略吗

谢谢

public ObservableCollection<UserControl> TimerBars
{
    get { return _TimerBars; }
    set
    {
        _TimerBars = value;
        OnPropertyChanged("TimerBars");
    }
}

public struct FBarWidth
{
    private int _Stopped;
    public int Stopped
    {
        get { return _Stopped; }
        set
        {
            _Stopped = value;
            OnPropertyChanged("Name"); //ERROR: An object reference is required for the non-static field
        }
    }

    private int _Running;
    //And more variables
}


private void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
public event PropertyChangedEventHandler PropertyChanged;
公共可观测收集计时器
{
获取{return\u timerbers;}
设置
{
_计时器=值;
OnPropertyChanged(“TimerBars”);
}
}
公共结构FBarWidth
{
私人INTU停止;
公共int停止
{
获取{return\u Stopped;}
设置
{
_停止=值;
OnPropertyChanged(“Name”);//错误:非静态字段需要对象引用
}
}
私人互联网运营;
//还有更多的变量
}
私有void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共事件属性更改事件处理程序属性更改;

OnPropertyChanged
需要在要更新其属性的范围内定义

为此,您必须实现接口
INotifyPropertyChanged

最后,您必须为
OnPropertyChanged
方法提供正确的参数。在本例中,“已停止”

编辑:在您的评论中,您提到有一个类SOROUND您在示例中提供的代码

这意味着您在类中嵌套了一个结构。
仅仅因为嵌套了结构,并不意味着它从外部类继承属性和方法。您仍然需要在结构中实现
INotifyPropertyChanged
,并在其中定义
OnPropertyChanged
方法。

FBarWidth
不实现任何INotifyPropertyChanged,并且该结构范围中未定义
OnPropertyChanged
方法。你难道看不出来吗?这是非常基本的C#理解。所有这些都在实现INotifyPropertyChanged的类内部。所有这些都适用于正则变量,但当我想使用结构定义“分组”变量时,我无法让它工作。是的,我对这一点很陌生……根据你最后的评论;请看下面我的最新答案。您应该避免在此处使用
struct
。这远非“干净的方式”。在很少使用结构的情况下,将它们设计为不可变的。那么INPC就无关紧要了。为什么你选择用结构而不是类来处理这些对象呢?正如Henk所说,结构应该是不可变的,如果不是的话,那么当你的代码在你不希望的情况下复制变量时,可能会发生很多容易的错误。所以我需要实现INotifyPropertyChanged的接口两次,在结构的内部和外部?您需要在结构中使用需要更新的属性实现接口。在上面的示例中,您还没有这样做。好的,非常感谢,所以我假设这里没有比在需要的地方复制实现更好的策略了?您可以创建一个类来实现INotifyPropertyChanged接口以及所需的方法和字段。然后在所有viewmodels上继承该类。请记住,不能在结构上使用继承。
public struct FBarWidth : INotifyPropertyChanged
{
    private int _Stopped;
    public int Stopped
    {
        get { return _Stopped; }
        set
        {
            _Stopped = value;
            OnPropertyChanged("Stopped");
        }
    }

    private int _Running;
    //And more variables

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}