編輯2如何將字符串添加到ICollection <string>?
我在過去幾天對我嘗試工作的問題有一些幫助。在得到多個用戶的有用支持後,我遇到了一個錯誤,我一直試圖在週末修復,但仍然沒有成功。
我創建了一本詞典,在那裏我通過string Country
以及ICollection
該國家的地方。
Dictionary<string, NewCountryClass> NTCD = new Dictionary<string, NewCountryClass>();
public void AddCountryCollection()
{
newCountryClass = new NewCountryClass(newCountry);
Collections.Add(newCountryClass);
NTCD.Add(newCountryClass.Country, newCountryClass);
}
public void AddPlace()
{
string Country = selectedItem.Country;
RenameQuestion(placeName);
NTCD[Country].Place.Add(placeName);
}
這是我newCountryClass
在那裏我存儲在國家和地方在該國。
private ICollection<string> _places;
public ICollection<string> Places
{
get
{
return _places;
}
set
{
if (value == _places)
return;
_places = value;
RaisePropertyChanged(() => Places);
}
}
這是可以添加的地方,但我在加入國家階段創建我的類的實例,因此當時不能通行的地方。 (編輯 - 我建議將集合的初始化移入構造函數而不是位置屬性中)。
public NewCountryClass(string country)
{
_places = new ObservableCollection<string>();
if (country != null)
{
_country = country;
}
}
因此,我試圖創建一個renamePlace()
方法:
public void RenamePlace(string place)
{
_places.Add(place);
}
然而,_places
似乎仍然是空即使有這樣的嘗試。任何進一步的想法或我做錯了什麼?
如果您不能使用物業的地方,而不是場_places在NewCountryClass構造?然後_places字段將被初始化。 – twrowsell
@twrowsell但是,正如你可以在places屬性的'get'部分看到的那樣,'_places'被驗證並且仍然返回null。 – Ben
只是爲了澄清,在構造函數中測試位置!= null。我看不到指向哪個字段或變量。你有一個字段_places和一個屬性Places。這應該是_places? – twrowsell