Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 回调函数和最佳实践_C#_.net_Events_Delegates_Callback - Fatal编程技术网

C# 回调函数和最佳实践

C# 回调函数和最佳实践,c#,.net,events,delegates,callback,C#,.net,Events,Delegates,Callback,更新:我把问题的措辞弄错了。内部和外部纬度/经度成员都需要相互更新,从而产生递归。外部成员由数据库上下文更新,而内部成员由UI更新。因此,数据库中的任何更改都必须传播到UI,反之亦然。坐标类不能与UI分离,外部成员不能与数据库分离 我想一个防止递归的标志是解决这个问题的最简单的方法 我有以下课程: class Coordinates { public double Latitude { get; set; } public double Longitude { get; set;

更新:我把问题的措辞弄错了。内部和外部纬度/经度成员都需要相互更新,从而产生递归。外部成员由数据库上下文更新,而内部成员由UI更新。因此,数据库中的任何更改都必须传播到UI,反之亦然。坐标类不能与UI分离,外部成员不能与数据库分离

我想一个防止递归的标志是解决这个问题的最简单的方法

我有以下课程:

class Coordinates
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

class Location
{
    public string Name { get; set; }

    public Coordinates Coordinates { get; set; }

    public double CoordinateLatitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }

    public double CoordinateLongitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }
}
成员
coordinarelationality
coordinarelationalitude
映射到坐标类。我想要达到的也是相反的效果。因此,当
坐标
类更新时,
位置
类的成员也应该更新。不要问我为什么代码的结构是这样的,因为我现在无权更改它

整个项目中有许多代码用于设置坐标类的成员:
location.Coordinates.Latitude=10.0D,等等。由于
坐标
类(以及其他许多类)是轻量级数据结构,并且在许多地方使用不小心,因此我不想将它们与事件联系起来


我的问题是,如何让Location.coordinations成员更新Location.coordinationRelativity值?是否有一种标准方法可以设置这样的回调,而不需要
IDisposable
进行清理(如在事件中)?

您的代码中有一个错误:

public double CoordinateLatitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }

    public double CoordinateLongitude
    {
        get { return (this.Coordinates.Latitude); }
        set { /* This member needs to get updated every time Location.Coordinates.Latitude gets updated. */ }
    }
坐标长度中的
纬度
更改为
经度
。另外,我不建议你把这些分开存放。因为它们通常对应于一个坐标同时需要纬度和经度的点,所以单独存储它们是没有意义的。这只需要一个调用,您可以完全消除协调度/经度

也就是说,您所要做的就是添加这些setter:

设置{this.Coordinates.Latitude=value;}


设置{this.Coordinates.Longitude=value;}

坐标
是一个类,因此如果您更改它,使用该实例的任何变量都会自动更新。假设我正确理解了您想要做的事情,您的集合可以是:

public double CoordinateLatitude
{
    get { return (this.Coordinates.Latitude); }
    set { this.Coordinates.Latitude = value; }
}

public double CoordinateLongitude
{
    get { return (this.Coordinates.Longitude); }
    set { this.Coordinates.Longitude = value;  }
}
编辑:如果
坐标
是一个结构,则需要以不同的方式进行编辑。这是因为在这种情况下,
This.Coordinates
会检索副本

public double CoordinateLongitude
{
    get { return (this.Coordinates.Longitude); }
    set {
        Coordinate temp = this.Coordinates;
        temp.Longitude = value;
        this.Coordinates = temp;
    }
}

但协调性确实得到了更新
我测试了你的代码

Location myLoc = new Location();
myLoc.Coordinates = new Coordinates();
myLoc.Coordinates.Latitude = 10;
System.Diagnostics.Debug.WriteLine(myLoc.CoordinateLatitude.ToString());
myLoc.Coordinates.Latitude = 20;
System.Diagnostics.Debug.WriteLine(myLoc.CoordinateLatitude.ToString());
您可能想问的是为什么UI不更新

哪一个是内在的,哪一个是外在的,以及你在哪里得到了重新保证,这一点都不清楚。
他们不会(也不应该)互相更新。
它们都只是引用同一个对象

这对我有用

class Coordinates
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

class Location : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public string Name { get; set; }
    private Coordinates coordinates = new Coordinates();
    public Coordinates Coordinates 
    {
        get { return coordinates; }
        set
        {
            if (coordinates == value) return;
            coordinates = value;
            NotifyPropertyChanged("Coordinates");
            NotifyPropertyChanged("CoordinateLatitude");
            NotifyPropertyChanged("CoordinateLongitude");
        }
    }

    public double CoordinateLatitude
    {
        get { return (this.Coordinates.Latitude); }
        set { this.Coordinates.Latitude = value; }
    }

    public double CoordinateLongitude
    {
        get { return (this.Coordinates.Longitude); }
        set { this.Coordinates.Longitude = value; }
    }
}

由于
坐标
已经是您在
坐标
中读取的
位置
的属性,因此您已经完成了读取。从
位置.坐标.纬度
读取与从
位置.坐标
读取相同

public Coordinates Coordinates { get; set; }

public double CoordinateLatitude
{
  get { return (this.Coordinates.Latitude); }
  set { this.Coordinates.Latitude = value;}
}

在setter中,您可以对其进行更新以设置
位置.坐标.纬度
值,或者删除该设置以使其成为只读属性。

听起来您需要一个属性事件侦听器,即使你不想要事件..这似乎已经满足了你的要求..如果你用与
get
方法相同的基本方式连接
set
方法,它应该满足你的要求,不是吗?不需要回调。将您的公共属性
coordinarelationality
coordinarelationgitude
视为
Coordinates
成员对应属性的别名。是什么让您认为它没有更新。试试myLoc.Coordinates.Latitude=10;Debug.WriteLine myLoc.CoordinationRelational.ToString();“纬度”成员(内部和外部)的set方法需要相互更新。外部将由数据库更新,而内部将由UI更新。我想防止递归应该可以做到这一点。