2013-05-06 85 views
0

更新:我說錯了這個問題。內外緯度/經度成員都需要互相更新,從而導致遞歸。外部成員通過數據庫上下文進行更新,而內部成員通過UI進行更新。因此,數據庫中的任何更改都必須傳播到UI,反之亦然。 Coordinates類不能與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. */ } 
    } 
} 

成員CoordinateLatitudeCoordinateLongitude地圖座標類。我想實現的也是相反的。所以當Coordinates類更新時,Location類的成員也應該更新。不要問我爲什麼代碼的結構是這樣的,因爲我現在無權改變它。

還有就是整個代碼,設置座標類的成員項目一標段:location.Coordinates.Latitude = 10.0D;等。因爲Coordinates類(和許多其他人)是輕量級的數據結構,並在很多地方不小心使用,我做不想把他們和事件聯繫起來。

我的問題是,我如何讓Location.Coordinates成員更新Location.CoordinateLatitude值?有沒有一個標準的方式來設置這樣的回調,而不需要清理(如在事件情況下)IDisposable

+0

聽起來像你需要一個屬性事件監聽器,即使你不想要事件.. – 2013-05-06 17:29:13

+1

這似乎已經這樣做了你在問什麼... – FlyingStreudel 2013-05-06 17:29:43

+2

如果你連接'set'方法的基本方式相同'get'方法,它應該做你想做的事,不是?不需要回調。將您的公共屬性'CoordinateLatitude'和'CoordinateLongitude'想象爲'Coordinates'成員相應屬性的別名。 – 2013-05-06 17:31:25

回答

1

但CoordinateLatitude不會得到更新
我測試你的代碼

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不會更新

這是不完全清楚這是內在,哪些是外部和你得到rerecursion。
他們不(不應該)互相更新。
他們都只是引用同一個對象。

這適用於我。

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; } 
    } 
} 
0

。在你的代碼中的錯誤:

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. */ } 
    } 

變化LatitudeCoordinateLongitudeLongitude。另外,我不建議你分開存儲這些。由於它們通常對應於同時需要座標的緯度和經度的點,因此獨立存儲它們是沒有意義的。這隻需要一個電話,你可以完全消除CoordinateLatitude/Longitude

話雖這麼說,所有你需要做的就是添加這些setter方法:

set { this.Coordinates.Latitude = value; }

set { this.Coordinates.Longitude = value; }

0

Coordinates是一類,所以如果你改變它,任何變量正在使用相同的實例被自動更新。假設我理解你想要正確地做了什麼,你的套可以只是:

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; } 
} 

編輯:這將需要以不同的方式做,如果Coordinates是一個結構。這是由於在這種情況下this.Coordinates複製了一份副本。

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

由於Coordinates已經從在CoordinateLatitude您的閱讀,你已經完成它的Location的屬性。 Location.Coordinate.Latitude的讀數與Location.CoordinateLatitude的讀數相同。

public Coordinates Coordinates { get; set; } 

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

在你的setter,你可以更新它來設置Location.Coordinate.Latitude值或只是刪除設置,使之成爲只讀屬性。