我怀疑这个行为有一个术语,如果我知道的话,我可以谷歌并了解我需要的东西。但是,我不知道。
这是我的构造函数:
[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : ObservableObject
{
private readonly IProductService _productService;
private readonly IProfileService _profileService;
public IEnumerable<ProductViewModel> Products { get; private set; }
private ProductViewModel _productSelected;
[ImportingConstructor]
public MainWindowViewModel(IProductService productService, IProfileService profileService, ILoggingService logger)
{
Products = _productService.InstalledProducts.Select(p => new ProductViewModel(p, _profileService, _logger));
SelectTheProductInDirectoryRunningFrom();
_productSelected.Load();
}
protected virtual void SelectTheProductInDirectoryRunningFrom()
{
string currentDir = Directory.GetCurrentDirectory();
if (_productSelected != null && _productSelected.InstalledPath != null &&
!_productSelected.InstalledPath.Contains(currentDir))
{
_productSelected =
Products.Where(p => currentDir.Contains(p.InstalledPath)).Select(p => p).DefaultIfEmpty(
_productSelected).SingleOrDefault();
}
}
这似乎很明显。它构建了ProductViewModel
s的集合,找到相关的集合,并在其上调用Load()
。 ProductViewModel.Load()
包含以下代码:
public class ProductViewModel : ObservableObject
{
private readonly IProfileService _profileService;
private readonly ILoggingService _logger;
private ObservableCollection<ProfileViewModel> _profiles;
private ConfigProfile _defaultConfig;
private ProfileViewModel _currentProfile;
public ListCollectionView Profiles { get; set; }
public bool Load(string errorMessage, bool critical)
{
List<ProfileViewModel> profileVms = new List<ProfileViewModel> { _currentProfile };
profileVms.AddRange(
_profileService.GetSavedProfiles(_data.ProductType).Select(
p =>
{
p.FilePath = current.FilePath;
return new ProfileViewModel(p, _defaultConfig, _profileService) { IsChanged = false };
}));
_profiles = new ObservableCollection<ProfileViewModel>(profileVms);
Profiles = new ListCollectionView(_profiles);
Profiles.SortDescriptions.Add(new SortDescription("ProfileTypeValue", ListSortDirection.Ascending));
Profiles.SortDescriptions.Add(new SortDescription("ProfileName", ListSortDirection.Ascending));
Profiles.CurrentChanged += (sender, args) =>
{
((ProfileViewModel)Profiles.CurrentItem).Initialize();
_currentProfile = Profiles.CurrentItem as ProfileViewModel;
};
return true;
}
当我在visual studio调试器中逐步执行此代码时,所有内容都会执行,_profiles
和Profiles
都会正确分配。但是,当执行从MainWindowViewModel
构造函数返回时,_profiles
和Profiles
都为空。
我按两次F11到达这里:
可能出现什么问题?我的物体是否以某种方式超出了范围?我想也许它与价值与参考有关,但我无法找到任何东西。谢谢!
答案 0 :(得分:0)
从WhereSelectEnumerableIterator
分配对象会创建一个新对象,因此原始列表未被更新。
_productSelected =
Products.Where(p => currentDir.Contains(p.InstalledPath)).Select(p => p).DefaultIfEmpty(
_productSelected).SingleOrDefault();
我在此次通话中添加了.ToList()
Products = _productService.InstalledProducts.Select(p => new ProductViewModel(p, _profileService, _logger));
将IEnumerable
强制转换为List
,而string s1 = "test";
string s5 = s1.Substring(0, 3)+"t";
string s6 = s1.Substring(0,4)+"";
Console.WriteLine("{0} ", object.ReferenceEquals(s1, s5)); //False
Console.WriteLine("{0} ", object.ReferenceEquals(s1, s6)); //True
不会创建新对象并更新现有对象。谢谢@Igor。