当列表中的项目 added/removed 时,ObserableCollection 不会触发更改
ObserableCollection not triggering Change when Items are added/removed from list
我对 ReactiveUI ObservableCollection 有疑问。我正在尝试根据在 ReactiveObject 中更改的列表更新 UI,但由于某种原因,我的列表更改没有被触发,我不确定我做错了什么。
这里有一个完整的回购协议:https://github.com/SebiCiuca/ObserableCollection
应用程序有一个按钮,当它被点击时调用“RandomService”,从列表中删除项目,然后将随机数量的项目添加回列表。
这个列表是一个 ObservableCollection,上面有一个订阅,所以我想在我的 ViewModel 中看到 RandomService 中发生的列表更改正在触发我的 ObservableCollection ModelList 变化.
代码如下:
主窗口
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{
public MainWindow(MainWindowViewModel mainWindowViewModel)
{
InitializeComponent();
ViewModel = mainWindowViewModel;
DataContextChanged += (sender, args) => ViewModel = DataContext as MainWindowViewModel;
this.WhenActivated(cleanup =>
{
this.BindCommand(ViewModel, vm => vm.RandonListCommand, view => view.RandomButton).DisposeWith(cleanup);
});
}
}
MainWindowViewModel
public class MainWindowViewModel : ReactiveObject
{
private readonly IRandomService m_RandomService;
public ReactiveCommand<Unit, Unit> RandonListCommand { get; set; }
public MainWindowViewModel(IRandomService randomService)
{
m_RandomService = randomService;
RandonListCommand = ReactiveCommand.Create(() => { CallRandomService(); });
randomService.WhenAnyValue(rs => rs.ModelList).WhereNotNull().Subscribe(_ => TriggerUpdateUI());
}
private void CallRandomService()
{
Debug.WriteLine("Random is called");
var random = new Random();
var take = random.Next(1, 4);
Debug.WriteLine($"Take is {take}");
m_RandomService.UpdateList(take);
}
private void TriggerUpdateUI()
{
Debug.WriteLine("List changed");
foreach (var model in m_RandomService.ModelList)
{
Debug.WriteLine($"{model.Id} {model.Name}");
}
}
}
随机服务
public class RandomService : ReactiveObject, IRandomService
{
private List<RandomModel> _privateList;
public RandomService()
{
_privateList = new List<RandomModel>
{
new RandomModel { Id = 1, Name = "FirstRandom" },
new RandomModel { Id = 2, Name = "SecondRandom" },
new RandomModel { Id = 3, Name = "SecondRandom" },
new RandomModel { Id = 4, Name = "SecondRandom" }
};
_modelList = new();
}
private ObservableCollection<RandomModel> _modelList;
public ObservableCollection<RandomModel> ModelList
{
get => _modelList;
set => this.RaiseAndSetIfChanged(ref _modelList, value);
}
public void UpdateList(int take)
{
_modelList.Clear();
Debug.WriteLine($"ModelList count {_modelList.Count}");
var addToUI = _privateList.Take(take).ToList();
addToUI.Shuffle();
addToUI.ForEach(p => ModelList.Add(p));
Debug.WriteLine($"ModelList count {_modelList.Count}");
}
}
IRandomService
public interface IRandomService
{
ObservableCollection<RandomModel> ModelList { get; }
void UpdateList(int take);
}
从我的角度来看,如果我阅读了 ObservableCollection 的定义,一切都是正确的
“表示一个动态数据集合,在添加或删除项目时或刷新整个列表时提供通知。”
所以我的问题是,为什么我的 TriggerUpdateUI() 从未调用过。 (应用程序启动时初始化时除外)。
randomService.WhenAnyValue(rs => rs.ModelList).WhereNotNull().Subscribe(_ => TriggerUpdateUI());
WhenAnyValue 正在监视 randomService 以获取 属性 ModelList 的更改通知。您尚未设置任何内容来查找与集合相关的更改。
From my point of view everything is correct if I read the definiton of ObservableCollection
"Represents a dynamic data collection that provides notifications when items get added or removed, or when the whole list is refreshed."
您的引述来自关于 ObservableCollection 的 MS 文档。它能够生成集合更改通知,但您仍然需要设置一些内容以通过 ReactiveUI / DynamicData 对其做出反应。
我对 ReactiveUI ObservableCollection 有疑问。我正在尝试根据在 ReactiveObject 中更改的列表更新 UI,但由于某种原因,我的列表更改没有被触发,我不确定我做错了什么。
这里有一个完整的回购协议:https://github.com/SebiCiuca/ObserableCollection
应用程序有一个按钮,当它被点击时调用“RandomService”,从列表中删除项目,然后将随机数量的项目添加回列表。
这个列表是一个 ObservableCollection,上面有一个订阅,所以我想在我的 ViewModel 中看到 RandomService 中发生的列表更改正在触发我的 ObservableCollection ModelList 变化.
代码如下:
主窗口
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{
public MainWindow(MainWindowViewModel mainWindowViewModel)
{
InitializeComponent();
ViewModel = mainWindowViewModel;
DataContextChanged += (sender, args) => ViewModel = DataContext as MainWindowViewModel;
this.WhenActivated(cleanup =>
{
this.BindCommand(ViewModel, vm => vm.RandonListCommand, view => view.RandomButton).DisposeWith(cleanup);
});
}
}
MainWindowViewModel
public class MainWindowViewModel : ReactiveObject
{
private readonly IRandomService m_RandomService;
public ReactiveCommand<Unit, Unit> RandonListCommand { get; set; }
public MainWindowViewModel(IRandomService randomService)
{
m_RandomService = randomService;
RandonListCommand = ReactiveCommand.Create(() => { CallRandomService(); });
randomService.WhenAnyValue(rs => rs.ModelList).WhereNotNull().Subscribe(_ => TriggerUpdateUI());
}
private void CallRandomService()
{
Debug.WriteLine("Random is called");
var random = new Random();
var take = random.Next(1, 4);
Debug.WriteLine($"Take is {take}");
m_RandomService.UpdateList(take);
}
private void TriggerUpdateUI()
{
Debug.WriteLine("List changed");
foreach (var model in m_RandomService.ModelList)
{
Debug.WriteLine($"{model.Id} {model.Name}");
}
}
}
随机服务
public class RandomService : ReactiveObject, IRandomService
{
private List<RandomModel> _privateList;
public RandomService()
{
_privateList = new List<RandomModel>
{
new RandomModel { Id = 1, Name = "FirstRandom" },
new RandomModel { Id = 2, Name = "SecondRandom" },
new RandomModel { Id = 3, Name = "SecondRandom" },
new RandomModel { Id = 4, Name = "SecondRandom" }
};
_modelList = new();
}
private ObservableCollection<RandomModel> _modelList;
public ObservableCollection<RandomModel> ModelList
{
get => _modelList;
set => this.RaiseAndSetIfChanged(ref _modelList, value);
}
public void UpdateList(int take)
{
_modelList.Clear();
Debug.WriteLine($"ModelList count {_modelList.Count}");
var addToUI = _privateList.Take(take).ToList();
addToUI.Shuffle();
addToUI.ForEach(p => ModelList.Add(p));
Debug.WriteLine($"ModelList count {_modelList.Count}");
}
}
IRandomService
public interface IRandomService
{
ObservableCollection<RandomModel> ModelList { get; }
void UpdateList(int take);
}
从我的角度来看,如果我阅读了 ObservableCollection 的定义,一切都是正确的
“表示一个动态数据集合,在添加或删除项目时或刷新整个列表时提供通知。”
所以我的问题是,为什么我的 TriggerUpdateUI() 从未调用过。 (应用程序启动时初始化时除外)。
randomService.WhenAnyValue(rs => rs.ModelList).WhereNotNull().Subscribe(_ => TriggerUpdateUI());
WhenAnyValue 正在监视 randomService 以获取 属性 ModelList 的更改通知。您尚未设置任何内容来查找与集合相关的更改。
From my point of view everything is correct if I read the definiton of ObservableCollection
"Represents a dynamic data collection that provides notifications when items get added or removed, or when the whole list is refreshed."
您的引述来自关于 ObservableCollection 的 MS 文档。它能够生成集合更改通知,但您仍然需要设置一些内容以通过 ReactiveUI / DynamicData 对其做出反应。