【问题标题】:C# bitmap uri source UI to freezeC#位图uri源UI冻结
【发布时间】:2020-07-26 16:53:17
【问题描述】:

这是我使用的代码,它会将 ui 冻结半秒。我能做些什么来阻止短暂的冻结?我试过 Task.Run(() => {} );但仍然无法摆脱半秒 UI 冻结。

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(XDocument.SelectSingleNode("Launcher/News").Attributes["picture_url"].Value, UriKind.Absolute);
bitmapImage.EndInit();

【问题讨论】:

标签: c#


【解决方案1】:

应该需要运行任务来加载不在 UI 线程中的图像,这是正确的。此外,您似乎必须将此行添加到您的代码中,以强制立即将图像缓存到内存:

bitmapImage.CacheOption = BitmapCacheOption.OnLoad;

代码示例:

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad; 
bitmapImage.UriSource = new Uri(XDocument.SelectSingleNode("Launcher/News").Attributes["picture_url"].Value, UriKind.Absolute);
bitmapImage.EndInit();

希望这会有所帮助,这是我找到的链接:Load a large BitmapImage asynchronously

这里有一个小例子来试试:

// XAML-Code (MainWindow.xaml)
<Window x:Class="WpfAppNet.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:local="clr-namespace:WpfAppNet"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        mc:Ignorable="d"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        Title="MainWindow" Height="450" Width="800">

        <Grid>
            <StackPanel>
                <Image Source="{Binding ImageSource}" />
                <Button Content="Load image"
                        Command="{Binding LoadImageCommand}"
                        CommandParameter="{Binding ElementName=MyImage}"/>
            </StackPanel>
        </Grid>
</Window>

// Code-Behind (MainWindow.xaml.cs)
namespace WpfAppNet
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }

    public class MyApplicationList
    {
        public bool IsSelected { get; set; }
        public string ApplicationName { get; set; }
    }
}

// View-Model Code (MainWindowViewModel.cs)
namespace WpfAppNet
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public ICommand LoadImageCommand { get; set; }
        public ImageSource ImageSource { get; set; }

        public MainWindowViewModel()
        {
            LoadImageCommand = new ActionCommand(LoadImageExecute);
        }

        private async void LoadImageExecute(object o)
        {
            try
            {
                var getImg = await LoadImg();
                ImageSource = getImg;
                NotifyPropertyChanged("ImageSource");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private async Task<BitmapImage> LoadImg()
        {
            return await Task.Run(() =>
            {
                BitmapImage bi = new BitmapImage();

                // Begin initialization.
                bi.BeginInit();

                // Set properties.
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.UriSource = new Uri(yourImageName, UriKind.Absolute);
                bi.EndInit();
                bi.Freeze();
                return bi;
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

通过这个例子,我希望你能得到你期望的结果,并可以将它实现到你的代码中。

【讨论】:

  • 并没有太大帮助,但还是感谢您的回答 +rep
  • 这是一个遗憾。您是否还有 UI 冻结的问题或现在另一个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 2015-03-17
  • 2013-10-22
  • 2018-11-23
  • 1970-01-01
  • 2022-01-25
  • 2015-01-16
相关资源
最近更新 更多