首页 > 解决方案 > WPF MVVMLight RelayCommand 未随机执行

问题描述

我在 WPF 应用程序中有一个按钮,它的行为很奇怪,有时它会完全正常工作,有时它根本不会触发。

如果按钮不起作用,我可以重新启动应用程序几次,它会再次随机工作。我在 上添加了一个 Debug 断点,CanScan = false;并且当按钮不起作用时不会触发该断点。

当按钮坏了是从应用程序开始就坏了,它不是第一次工作然后停止的地方。当按钮工作时,无论我单击它多少次并运行 RelayCommand,它都可以正常工作。

查看模型代码:public ICommand OnScanPage { get; 放; }

    public ScannerViewModel(ScanningService scanningService, Action<int> processingAction)
    {
        this.processingAction = processingAction;
        this.scanningService = scanningService;

        Pages = new ObservableCollection<ScannerPageViewModel>();

        CanScan = true;

        OnScanPage = new RelayCommand(() => {
            try
            {
                CanScan = false;
                processingAction.Invoke(1);

                scanningService.ScanPage(img =>
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        CanScan = true;
                        processingAction.Invoke(-1);
                        Pages.Add(new ScannerPageViewModel(this, processingAction, img));
                    });
                });
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        });
    }

查看型号代码:

<UserControl x:Class="DocumentScanner.Views.Scanner.ScannerUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DocumentScanner.Views.Scanner"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Grid.Row="0" Content="Scan page" IsEnabled="{Binding CanScan}" Command="{Binding OnScanPage}" />

        <ListView Margin="0" Grid.Row="1" ItemsSource="{Binding Pages}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Image MaxWidth="200" Source="{Binding Image}" Grid.RowSpan="2" />

                            <Button Command="{Binding Rotate}" Grid.Row="0" Grid.Column="0" Padding="10">Rotate</Button>
                            <Button Command="{Binding Remove}" Grid.Row="0" Grid.Column="1" Padding="10">Remove</Button>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

扫描服务:

public void ScanPage(Action<BitmapImage> onImage)
{
    Task.Run(() =>
    {
        try
        {
            ImageProcesser.Push(onImage);

            var myDS = selectedSource;
            myDS.Open();

            myDS.Capabilities.CapAutoFeed.SetValue(BoolType.True);
            myDS.Capabilities.CapAutoScan.SetValue(BoolType.True);

            myDS.Enable(SourceEnableMode.NoUI, true, IntPtr.Zero);
            myDS.Close();
        }
        catch (Exception exp)
        {
            Debug.WriteLine(exp.Message);
        }
    });
}

更新: 我将 RelayCommand 更改为我的新 ECommand(见下文),因为它现在每次都在工作。我将这个问题悬而未决,因为我必须对 RelayCommand 做错了什么。

   public class ECommand : ICommand
    {
        private Action a;

        public ECommand(Action a)
        {
            this.a = a;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            a.Invoke();
        }

        public event EventHandler CanExecuteChanged;
    }

标签: wpfmvvm-light

解决方案


推荐阅读