使用命令来通知集合已更改

发布于 2024-09-07 14:09:13 字数 627 浏览 2 评论 0原文

我想知道是否有任何方法可以处理集合更改时的情况,然后向用户显示一条消息。

由于我使用 MVVM,我没有对视图项目内的模型的任何引用,因此我无法执行此操作,

MyCollection col = InstanceOfViewModel.Read();

因为视图对“MyCollection”一无所知,而且我不想在 if 内测试方法本身,例如上面的代码:

if(InstanceOfViewModel.Read().Count == 0)

所以我想到了命令。我已经在使用一个来处理我的储蓄和存款。更新条件(而不是大量的 if)。但现在我被困住了。我不知道我应该做什么/使用什么,因为 ObservableCollections 公开了一个事件 CollectionChanged。

一种解决方案可能是:让 viewmodel 订阅此事件,当事件发生时,检查计数是否等于 0,如果为 true,则向用户显示:“您的搜索未检索到任何行”或其他任何内容。但我不喜欢在视图模型、消息和任何类型的用户交互中使用消息框,我试图委托给视图,

但我想知道我是否可以使用命令来做到这一点,或者我是否在一个视图中变得太复杂相对容易实现的事情。

等待答案并提前致谢!

I was wondering if there's any way i could handle when a collection changes and, after that, display a message to the user.

As im using MVVM, i dont have any references to the model inside the view project, so i couldn't do

MyCollection col = InstanceOfViewModel.Read();

Since View doesn't know anything about "MyCollection", and i dont want to test the method itself inside a if, like the code above:

if(InstanceOfViewModel.Read().Count == 0)

So i thought about Commands. I'm already using one to handle my saving & updating conditions (instead of tons of if's). But now im trapped. I cant figure out what should i do/use cause, ObservableCollections exposes one event, CollectionChanged.

One solution could be: make the viewmodel subscribe to this event, and when it happens, check if the count is equals 0, if true, show to user: "Your search didnt retrieved any rows" or anything else. But i dont like to use MESSAGEBOXES in a viewmodel, messages and any kind of User Interactivity im trying to delegate to the View(s)

But i would like to know if i CAN do it using COMMANDS, or if im getting too complicated in a thing that is relatively easy to implement.

Waiting for answers and thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-09-14 14:09:14

如果您的集合位于视图模型中并且您正在使用 onPropertyChanged...每当您的整个集合发生更改(而不是单个项目)时,它都会触发 onPropertychanged,您可以在 onPropertyChanged 方法中进行监视...也许我不理解您的代码结构...

if your collection is in a viewmodel and you are using the onPropertyChanged...anytime your entire collection changes(not the individual items) it would fire onPropertychanged which you could watch for in your onPropertyChanged method...Maybe I am not understandig your code structure...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文