WPF立即更改图像源

时间:2016-02-23 14:51:35

标签: c# wpf xaml

我已经设法解决了如何以编程方式更改WPF表单中的图像,但我真的很难弄清楚如何立即执行此操作。我有一个GUI报告了许多进程的进度,下面的代码用于更改图像:

cacheBuilderStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox-complete.png"));

但这并没有立即更新图像,我不知道如何强制重绘GUI。我已尝试过这两种情况,但似乎也没有任何影响:

        cacheBuilderStatus.InvalidateVisual();
        cacheBuilderStatus.UpdateLayout();

我甚至试过这个都无济于事:

        BitmapImage cbImage = new BitmapImage(); //(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        cbImage.BeginInit();
        cbImage.CacheOption = BitmapCacheOption.None;
        cbImage.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
        cbImage.CacheOption = BitmapCacheOption.OnLoad;
        cbImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        cbImage.UriSource = new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png");
        cbImage.EndInit();

如果有人能帮我理解如何实现这一目标,我将非常感激。

谢谢。

编辑:相关图片的XAML是:

            <Image x:Name="cacheBuilderStatus" Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" Width="20" Source="Resources/checkbox.png"/>

背景中的代码如下所示:

        // Reset progress images
    private void resetStatus()
    {
        BitmapImage cbImage = new BitmapImage(); //(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        cbImage.BeginInit();
        cbImage.CacheOption = BitmapCacheOption.None;
        cbImage.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
        cbImage.CacheOption = BitmapCacheOption.OnLoad;
        cbImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        cbImage.UriSource = new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png");
        cbImage.EndInit();

        cacheBuilderStatus.InvalidateVisual();
        cacheBuilderStatus.UpdateLayout();

        cacheBuilderStatus.Source = cbImage;
        ciUserStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        accessCiUserStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        configItemStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        enumerationsStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        serviceOfferingStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        workItemStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        websiteStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
        appPoolStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox.png"));
    }


    // Buttons event handlers
    // Restart portal
    private void primaryToggleButton_Checked(object sender, RoutedEventArgs e)
    {
        // Result variables
        string serviceRestartResult;
        bool cIUserResult;
        bool accessUserWIResult;
        bool configurationItemsResult;
        bool enumerationResult;
        bool serviceOfferingResult;
        bool workItemResult;
        string websiteRestartResult;
        string appPoolRestartResult;

        // Pull timeout variables from app.config
        serviceStatusChangeTimeout = Int32.Parse(ConfigurationManager.AppSettings.Get("serviceStatusChangeTimeout"));
        websiteStatusChangeTimeout = Int32.Parse(ConfigurationManager.AppSettings.Get("websiteStatusChangeTimeout"));

        // Get current DateTIme for LastModified tests
        DateTime time = DateTime.Now.ToUniversalTime();


        // Reset status images
        resetStatus();
        Thread.Sleep(5000);

        // Service restart
        serviceRestartResult = MidTier.ServiceRestart(serviceStatusChangeTimeout);
        if (serviceRestartResult == "Success")
        {
            // If success set service status icon to completed image
            cacheBuilderStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox-complete.png"));
        }
        else
        {
            // If fail set image to failed red cross, display error text and error text margin to 5 and height to Auto.
            cacheBuilderStatus.Source = new BitmapImage(new Uri("pack://application:,,,/WpfCustomControlLibrary1;component/Resources/checkbox-error.png"));
            displayErrorText(serviceRestartResult);
        }

1 个答案:

答案 0 :(得分:0)

您更改图片的代码看起来很好,应立即更新。我假设您遇到的问题是您在UI线程上处理一些信息,因此它不会更新。

您的处理应始终移至后台线程,否则您将锁定UI线程直到完成并且无法更新。

例如,如果你这样做:

private void button_Click(object sender, RoutedEventArgs e)
{
    myImage.Source = new BitmapImage(new Uri(@"e:\icons\transfer1.ico"));
}

它会立即更新。但是如果你在那里进行处理,那就不会:

private void button_Click(object sender, RoutedEventArgs e)
{
    myImage.Source = new BitmapImage(new Uri(@"e:\icons\transfer1.ico"));
    Thread.Sleep(5000); //simulate doing stuff
}

所以改为在不同的线程上进行处理,如下所示:

private async void button_Click(object sender, RoutedEventArgs e)
{
    myImage.Source = new BitmapImage(new Uri(@"e:\icons\transfer1.ico"));

    await Task.Run(() =>
        {
            //Do work here
            Thread.Sleep(5000);
        });
}