【问题标题】:.NET WPF progress bar updating throws invocation exception.NET WPF 进度条更新引发调用异常
【发布时间】:2014-04-04 18:13:17
【问题描述】:

我有一个列表视图,其中包含列表的 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;
                        }


                }
            }));
    }

【问题讨论】:

  • 创建一个合适的 ViewModel 并使用合适的 DataBinding,你所有的问题都会神奇地消失。
  • 你能举个例子吗?
  • @HighCore - 我已经添加了 XAML 代码。这里有什么需要改变的吗?
  • google 上有数百万个例子。搜索 MVVM
  • 我没有使用 .NET 4.0 或 WPF 4.0 我使用的是 3.5 我不知道 MVVM 是否适用于这个版本的 .net 或 wpf

标签: .net wpf listview exception progress


【解决方案1】:

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

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

  • 在异常弹出窗口底部的“操作”标题下,单击链接“查看详细信息...”

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

  • 接下来,通过(单击左侧的小三角形或双击)打开异常的详细信息。
  • 选择“InnerException”节点,并打开其详细信息(方法相同)。

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

问题可能很简单,例如“对象引用未设置为对象的实例”。 但是,您的基本策略很好并且会起作用。 希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多