CanExecute抛出异常

时间:2016-04-06 08:57:28

标签: c# .net wpf

在我的XAML代码中,我做了一些命令:

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Open" Executed="OpenCommandHandler"  />
    <CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommandHandler" CanExecuted="CanSaveCommandHandler" />
    <CommandBinding Command="ApplicationCommands.Delete" Executed="DeleteCommandHandler" CanExecuted="CanDeleteCommandHandler" />
    <CommandBinding Command="{x:Static local:MainWindow.AddCommand}" Executed="AddCommandHandler" CanExecuted="CanAddCommandHandler" />

我的CanExecute方法如何:

private ResearcherObservableCollection list;
    public Researcher res;
    public static RoutedCommand AddCommand = new RoutedCommand("Add", typeof(WpfApplication3.MainWindow));
    private int _errors = 0;

    public MainWindow()
    {
        InitializeComponent();
        list = new ResearcherObservableCollection();
        res = new Researcher();
        this.DataContext = list;
    }

 private void CanSaveCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        if (list.Changes == true)
            e.CanExecute = true;
        else e.CanExecute = false;
    }
private void CanAddCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {


        {
            e.CanExecute = true;


            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Adding); i++)
            {
                var child = VisualTreeHelper.GetChild(Adding, i);
                if (Validation.GetHasError(child)) { e.CanExecute = false; }
            }
        }

    }

private void CanDeleteCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        if (((Project)this.projlst.SelectedItem) != null) e.CanExecute = true;
        else e.CanExecute = false;
    }

当我不使用CanExecute方法并仅使用Executed时 - 一切正常。但问题是,我必须在执行前检查一些参数。我有异常TargetInvocationException。也许你有类似的问题?我该如何更正我的代码?

1 个答案:

答案 0 :(得分:0)

我处理了我的问题。我想比我创建列表更早地调用CanExecute方法。所以我们必须检查它们是否不为空:

 private void CanAddCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        //if (VisualTreeHelper.GetChild(Adding))

        {
            e.CanExecute = true;

            //for (int i = 0; i < VisualTreeHelper.GetChildrenCount(Adding); i++)
            //{
            //    var child = VisualTreeHelper.GetChild(Adding, i);
            //    if (Validation.GetHasError(child)) { e.CanExecute = false; }
            //}

            if (Adding != null)
            {

                for (int i = 0; i < Adding.Children.Count; i++)
                {
                    var child = Adding.Children[i];
                    if (Validation.GetHasError(child)) { e.CanExecute = false; break; }
                }
            }
            else e.CanExecute = false;

        }

    }

 private void CanSaveCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        if (lst != null)
        {
            if (list.Changes == true)
                e.CanExecute = true;
            else e.CanExecute = false;
        }
        else e.CanExecute = false;
    }

private void CanDeleteCommandHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        if (lst != null)
        {
            if (((Researcher)this.lst.SelectedItem) != null) e.CanExecute = true;
        }
        else e.CanExecute = false;
    }

XAML代码中的一些更改:

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Open" Executed="OpenCommandHandler" />
    <CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommandHandler" CanExecute="CanSaveCommandHandler" />
    <CommandBinding 
                    Command="ApplicationCommands.Delete"
                    CanExecute="CanDeleteCommandHandler"
                    Executed="DeleteCommandHandler" />
    <CommandBinding Command="{x:Static local:MainWindow.AddCommand}" Executed="AddCommandHandler" CanExecute="CanAddCommandHandler"/>

</Window.CommandBindings>