1

我在 WPF 应用程序上遇到了一个非常奇怪的问题。让我清楚地解释我做了什么。

我将 Datagrid 与 selecteditems 作为复选框,因此用户选择并单击“加载”按钮,然后记录将加载到数据库服务器。在此期间,我在将数据加载到数据库服务器时不断旋转图像。因为我有很多记录。

默认情况下,当数据加载属性更改为可见时,我将图像保持为隐藏状态。当它出现时 foreach 语句图像永远不会显示,或者默认情况下它是可见性的图像显示但从不旋转......任何想法或帮助我做错了什么......?

Xml代码...

<Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="1042,83,0,0"
    Name="btnSaveData" Visibility="Hidden" VerticalAlignment="Top" Width="75"
    Cursor="Hand" Click="btnSaveData_Click" Foreground="Green" 
    Background="#FFB0D3D3" FontWeight="Bold" FontSize="14"/>
<Image Height="25" HorizontalAlignment="Left" Margin="1012,83,0,0" Name="imgSpin5"
    Stretch="None" RenderTransformOrigin="0.5,0.5" Visibility="Hidden"
    VerticalAlignment="Top" Width="24"
    Source="/LoadDataSource;component/Images/Spin5.png">
    <Image.RenderTransform>
        <RotateTransform x:Name="TransRotate" Angle="0"/>
    </Image.RenderTransform>
    <Image.Triggers>
        <EventTrigger RoutedEvent="Image.Loaded">
            <BeginStoryboard>
                <Storyboard TargetProperty="Angle">
                    <DoubleAnimation Storyboard.TargetName="TransRotate"
                        Storyboard.TargetProperty="Angle" By="360" 
                        Duration="0:0:1" AutoReverse="False" 
                        RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Image.Triggers>
</Image>

C#代码..

MessageBoxResult result = MessageBox.Show("Do you want to Load Selected items?",
   "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (result == MessageBoxResult.Yes)
{
    imgSpin5.Visibility = Visibility.Visible;
    foreach (CType ctp in dgAttributes.ItemsSource)
    {
        if (ctp.IsSelected)
            imgSpin5.Visibility = Visibility.Visible;
    }
}
4

2 回答 2

0

您可以尝试将可见性更新包装在Dispatcher.Invoke调用中以将其强制到 UI 线程的顶部并foreach在后台工作线程上运行:

MessageBoxResult result = MessageBox.Show("Do you want to Load Selected items?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Information);
    if (result == MessageBoxResult.Yes)
    {
        imgSpin5.Visibility = Visibility.Visible;
        BackgroundWorker backgroundWorker = new BackgroundWorker();
        backgroundWorker.DoWork((s,e)=>{
            foreach (CType ctp in dgAttributes.ItemsSource)
            {
                if (ctp.IsSelected == true)
                {
                    Dispatcher.Invoke(() =>
                    {
                        imgSpin5.Visibility = Visibility.Visible;
                    });
                }
            }
        });
        backgroundWorker.RunWorkerAsync();

    }
于 2013-01-28T14:15:22.937 回答
0

如果(结果 == MessageBoxResult.Yes){

                BackgroundWorker backgroundWorker = new BackgroundWorker();

                backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
                {
                    foreach (CType ctp in dgAttributes.ItemsSource)
                    {
                        if (ctp.IsSelected == true)
                        {

                            Dispatcher.Invoke(() =>
                            {
                                imgSpin.Visibility = Visibility.Visible;
                            });

                        }

                    }

                };
                backgroundWorker.RunWorkerAsync();

                MessageBoxResult results = MessageBox.Show("Sucessfully Loaded..!", "Confirmation", MessageBoxButton.OK);

                imgSpin.Visibility = Visibility.Hidden;
            }
于 2013-01-28T14:41:41.837 回答