【发布时间】:2014-09-16 11:59:29
【问题描述】:
我在使用 GalaSoft 的 RelayCommand 时遇到了一些问题。
我有一个 NextCommand 属性有效,但只有几次。
之后,它完全停止工作。
您可以通过示例项目进行尝试:
http://s000.tinyupload.com/?file_id=65828891881629261404
行为如下:
-
下一个命令:
- 弹出所有项目直到活动索引
- 如果剩余少于 50 个项目,则推送 1 个新项目
- 将新项目标记为活动
-
BackCommand:
- 将活动索引向后移动 1 个位置
复制步骤:
- “+”(OemPlus) 键已绑定到 NextCommand
- '-' (OemMinus) 键已绑定到 BackCommand
- 按住“+”键直到列表停止增长(限制为 50 项)
- 按住“-”键直到列表中的第一项处于活动状态
- 重复
(复制错误)所需的重复次数不一致。
有时我会在 4 次重复后得到它;其他时间到 9 点。
// Items Collection
public class ItemCollection : ViewModelBase
{
// List of Items
private readonly ObservableCollection<Item> _items = new ObservableCollection<Item>();
public ObservableCollection<Item> Items
{
get { return _items; }
}
// Constructor
public ItemCollection()
{
BackCommand = new RelayCommand(
() =>
{
// Go to previous page
var index = Items.IndexOf(ActiveItem);
if (index > 0)
{
ActiveItem = Items[index - 1];
}
},
() => ActiveItem != null && Items.IndexOf(ActiveItem) > 0);
}
// Back command
public RelayCommand BackCommand { get; set; }
// Next command
public RelayCommand NextCommand { get; set; }
// The currently-active item
private Item _activeItem;
public Item ActiveItem
{
get { return _activeItem; }
set
{
Set(() => ActiveItem, ref _activeItem, value);
}
}
}
// Item
public class Item : ViewModelBase
{
public string Title { get; set; }
}
当我进入 RelayCommand 的代码时,执行操作的 isAlive 标志为假。但我似乎无法弄清楚这是怎么发生的。
【问题讨论】:
-
+1 为您解释问题的精美动画 Gif。
-
我没有看到
NextCommand在您的代码中实例化的位置。问题可能存在(禁用它的东西)。 -
也就是说,我们需要看看
NextCommand是什么 -
您的示例项目不再可用
标签: c# wpf mvvm-light relaycommand