.NET WPF进度条更新会引发调用异常

时间:2014-04-04 18:13:17

标签: .net wpf listview exception progress

我有一个列表视图,其中包含列表的itemsource。当我尝试执行以下代码时,调用类未处理InvocationException。如果需要更多代码,我可以添加所请求的内容。

我的目标是能够更新列表视图中的进度条。从按钮单击事件调用worker1.RunWorkerAsync()。

XAML:

<Window x:Class="CS3000Config2.Dialogs.Clone"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Clone Scanner Configuration" Height="768" Width="1024" FontSize="16" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Loaded="Window_Loaded">
<Window.Background>
    <LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
        <GradientStop Color="#b8e1fc" Offset="0" />
        <GradientStop Color="#a9d2f3" Offset="0.10" />
        <GradientStop Color="#90bae4" Offset="0.25" />
        <GradientStop Color="#90bcea" Offset="0.37" />
        <GradientStop Color="#90bff0" Offset=".50" />
        <GradientStop Color="#6ba8e5" Offset=".51" />
        <GradientStop Color="#a2daf5" Offset=".83" />
        <GradientStop Color="#bdf3fd" Offset="1" />
    </LinearGradientBrush>
</Window.Background>

<DockPanel>
    <Grid>
        <DockPanel Height="650" Margin="50,40,50,0" Width="800" VerticalAlignment="Top" Background="White">
            <Grid>
                <ToolBar Height="46" Margin="0,6,-11,0" Name="toolBar1" VerticalAlignment="Top" ToolBarTray.IsLocked="True" BorderBrush="Black" BorderThickness=".5" Loaded="toolBar1_Loaded">
                    <Button ToolBar.OverflowMode="AsNeeded" Click="Button_Click">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/dsave.png" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0">Load To Selected Scanners</TextBlock>
                        </StackPanel>
                    </Button>
                    <Separator />
                    <Button ToolBar.OverflowMode="Never">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/document-save-all2.png" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Load To All Scanners</TextBlock>
                        </StackPanel>
                    </Button>
                    <Separator />
                    <Button ToolBar.OverflowMode="Never" Click="Button_Click_1">
                        <StackPanel  Orientation="Horizontal" VerticalAlignment="Center" >
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Margin" Value="10,0,0,0"/>
                                </Style>
                            </StackPanel.Resources>
                            <Image Source="/CS3000Config2;component/Resources/refresh.gif" />
                            <TextBlock VerticalAlignment="Center" FontSize="16" Margin="5,0,0,0"> Refresh Scanner List</TextBlock>
                        </StackPanel>
                    </Button>
                </ToolBar>
                <ScrollViewer Margin="6.5,58,6,6" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
                    <Grid>
                        <ListView  Margin="0,5" Name="listView1" SelectionMode="Multiple" HorizontalContentAlignment="Center">
                            <ListView.View >
                                <GridView >
                                    <GridViewColumn Width="50" Header="">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <Image Source="/CS3000Config2;component/Resources/cs3000-small.png" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Width="100" Header="Drive Letter" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Drive}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                    <GridViewColumn Width="200" Header="Model" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Model}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn Width="200" Header="Serial Number" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Margin="5,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=Serial}" TextAlignment="Center" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn Width="225" Header="" >
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <ProgressBar  BorderBrush="Black" BorderThickness=".5" Width="200" Height="20" HorizontalAlignment="Center" Visibility="{Binding Path=isVisible}" Minimum="0" Value="{Binding Path=progress}" Maximum="{Binding Path=maxProgress}"/>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>

                        </ListView>
                    </Grid>
                </ScrollViewer>
            </Grid>
        </DockPanel>

    </Grid>
</DockPanel>

C#代码:

private void worker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Config tmpConfig = config;
        Config tmpConfig2 = new Config();
        Dispatcher.Invoke((Action)(() =>
            {

                foreach (Scanner s in listView1.SelectedItems)
                {
                    Int32 index = listView1.SelectedIndex;
                    tmpConfig2.LoadConfig(s.Drive + "\\Parameters\\Config.ini");
                    tmpConfig2.LoadSysInfo(s.Drive + "\\Parameters\\SYSINFO.TXT");
                    if (tmpConfig2.Model == "CS3070")
                    {
                        tmpConfig.BtName = tmpConfig2.BtName;
                        tmpConfig.BtPIN = tmpConfig2.BtPIN;
                    }
                    tmpConfig.WriteConfig(s.Drive + "\\Parameters\\Config.ini", tmpConfig);
                    String[] lines = File.ReadAllLines(s.Drive + "\\Parameters\\Config.ini");
                    s.maxProgress = lines.Length;
                    s.isVisible = Visibility.Visible;

                        for (int i = 0; i != lines.Length; i++)
                        {
                            items[index] = new Scanner() { Drive = s.Drive, Model = s.Model, isVisible = Visibility.Visible, progress = i + 1, maxProgress = lines.Length, Serial = s.Serial };
                            System.Threading.Thread.Sleep(10);

                            listView1.ItemsSource = null;
                            System.Threading.Thread.Sleep(10);
                            listView1.ItemsSource = items;
                        }


                }
            }));
    }

1 个答案:

答案 0 :(得分:0)

你当然可以这样做,而MVVM在这里是一个不相关的主题。通常,当您收到调用异常时,包装了原始异常(和关联调用堆栈),您应该查看内部异常。

如果您(或其他人)不熟悉......

  • 在异常弹出窗口的“操作”标题下方,点击“Veiw Detail ...”链接

  • 这将弹出“查看详细信息”对话框。

  • 接下来,打开例外的详细信息(单击左侧的小三角形或双击)。
  • 选择'InnerException'节点,并打开其详细信息(以相同的方式)。

  • 继续向下钻取InnerExceptions,直到找到原始异常。

问题可能就像'对象引用未设置为对象的实例'一样简单。 但是你的基本策略很好并且可行。 希望这有帮助!