构造函数完成后对象消失

Objects disappearing once constructor finishes

我怀疑有一个术语可以描述这种行为,如果我知道的话,我可以 google 它并了解我需要的东西。但是,我不知道。

这是我的构造函数:

[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 的集合,找到相关的集合,并对其调用 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 调试器中逐步执行此代码时,所有内容都会执行,并且 _profilesProfiles 都被正确分配。但是,当从 MainWindowViewModel 构造函数执行 returns 时, _profilesProfiles 都为空。

我按两次F11来到这里:

可能出了什么问题?我的对象是否以某种方式超出了范围?我想这可能与价值与参考有关,但我找不到任何东西。谢谢!

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,它不会创建新对象并更新现有对象。谢谢@Igor。