KeyBinding递增其命令调用

本文关键字:命令 调用 KeyBinding | 更新日期: 2023-09-27 18:29:53

所以我的应用程序基本上是一个带有ContentControl的shell,根据用户对菜单的选择,我用自定义的UserControl填充它。

但现在我有一个奇怪的行为。

我已将ContentControlContent属性附加到ViewModel属性,并根据需要实例化该属性。这很好,但我有两个问题。

  1. 当我选择菜单的一个选项时,它会创建指定UserControl的新实例,并将Content属性设置为shell。这很有效,因为我看到了控件并可以与它交互,当我从菜单中选择另一个选项时,它会向我显示另一个UserControl,但当我再次选择上次选择的选项时,似乎正在加载相同的上次实例化的控件(正确的控件,但有旧的输入,我正在执行一个新的XXXControl(),然后将其设置为ContentControlContent属性。

  2. 我通过命令从自定义控件内部调用ShowDialog()(从用户控件视图模型中,我通过MVVM Light消息调用视图,然后显示对话框),这很有效。但当我试图关闭对话框时,它会再次显示,次数与我从菜单中选择不同选项的次数相同。

例如,我从A菜单开始并显示对话框,然后关闭按钮工作,然后我转到B菜单并返回A,然后第二次按下关闭按钮工作(调用两个ShowDialog()),依此类推…

我不知道为了给这篇文章提供更多的上下文,我必须粘贴代码的哪一部分,但任何输入都将不胜感激。我被这个(我的)虫子卡住了。

代码

在外壳视图上:

<ContentControl Grid.Row="2" Content="{Binding CurrentView}" Margin="15,10"/>

壳上视图模型:

if (action == null || action == SEARCH_ACTION)
    {
        ActionsMenuSelected = SEARCH_ACTION;
        var view = new SearchDocumentView();
        CurrentView = view;
    }

在内部视图(SearchDocumentView)上:

public SearchDocumentView()
{
    InitializeComponent();
    Messenger.Default.Register<NotificationMessage<Entity>>(this, NotificationMessageReceived);
}

private void NotificationMessageReceived(NotificationMessage<Entity> msg)
{
    if (msg.Notification == "ViewResult")
    {
        var view = new DocumentViewer( ServiceLocator.Current.GetInstance<IDataService>(),msg.Content);
        view.ShowDialog();
    }
}

在内部视图(SearchDocumentView)上xaml:

<ListBox x:Name="SearchResults"  ItemsSource="{Binding SearchResults}" SelectedItem="{Binding SelectedSearchResult}">
            <ListBox.InputBindings>
                <KeyBinding
                            Key="Enter"
                            Command="{Binding ViewResult}" />
                <KeyBinding
                            Key="Return"
                            Command="{Binding ViewResult}" />
            </ListBox.InputBindings>
            ...

在内部视图(SearchDocumentView)ViewModel:

private RelayCommand _viewResut;
        /// <summary>
        /// Gets the ViewResult.
        /// </summary>
        public RelayCommand ViewResult
        {
            get
            {
                return _viewResut
                    ?? (_viewResut = new RelayCommand(
                                          () =>
                                          {
                                              MessengerInstance.Send(new NotificationMessage<Entity>((Entity)SelectedSearchResult, "ViewResult"));
                                          },
                                          () => ((Entity)SelectedSearchResult!=null)?true:false ));
            }
        }

KeyBinding递增其命令调用

为什么您看到了我现在看不到的旧数据,但我猜您对新创建的视图使用了相同的(模型)数据。

第二个问题应该在这里:

public SearchDocumentView()
{
    InitializeComponent();
    Messenger.Default.Register<NotificationMessage<Entity>>(this, NotificationMessageReceived);
}

对于您注册的每个新视图,都会有一个Notification,它会显示您的消息框,但我看不出您是否注销了这些消息框,如果您没有注册,处理程序会将视图模型保存在内存中,并且仍然会显示消息框。

即使我弄错了(这是你的"控件"-viewmodel吗?),它也应该非常相似,但你可以通过在消息上的.Show设置断点并在调试时查看调用堆栈来轻松找到这一点。