WPF MVVMLight RelayCommand不是随机执行

时间:2019-03-04 02:20:16

标签: wpf mvvm-light

我在WPF应用程序中有一个按钮,它的功能很奇怪,有时它可以完全正常工作,而另一些则根本不会触发。

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

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

查看型号代码:         公共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;
    }

0 个答案:

没有答案