当我调用OnPropertyChanged(" Something")时,是否可以再次声明和初始化全局变量? 例如,我有这段代码:
private ObservableCollection<MatchesViewModel> matches;
private ObservableCollection<string> listWithLists;
private string directoryName = "currentMatches";
private void HandleLoadListClicked(object obj)
{
directoryName = selectedItem;
this.Matches = DataGetterAndSetter.GetMatches("Lists\\" + directoryName + ".xml");
MainPage window = new MainPage();
window.Show();
}
public IEnumerable<MatchesViewModel> Matches
{
get
{
matches = DataGetterAndSetter.GetMatches("Lists\\"+ directoryName + ".xml");
return this.matches;
}
set
{
if (matches == null)
{
matches = new ObservableCollection<MatchesViewModel>();
}
matches.Clear();
foreach (var item in value)
{
matches.Add(item);
}
this.OnPropertyChanged("Matches");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
var propertyChangedEventArgs = new PropertyChangedEventArgs(propertyName);
this.PropertyChanged(this, propertyChangedEventArgs);
}
}
每当OnPropertyChanged(&#34; Matches&#34;)被省略时,将在类的开头使用它的默认值 - &#34; currentMatches&#34;再次初始化directoryName变量。这是正常的,我如何保存directoryName的值,以便以后在我获取属性中的数据时使用它?