1

我在 IsolatedStorage 中有 1000 多个图像(拇指),需要在 JumpList 中显示它们

我有一个 XAML 代码:

<telerikData:RadJumpList   telerikCore:InteractionEffectManager.IsInteractionEnabled="True" Name="TitleCollection" ItemsSource="{Binding TitleCollection}">
<telerikData:RadJumpList.ItemTemplate >
 <DataTemplate>
    <StackPanel Orientation="Horizontal">                                    
        <Image Source= "{Binding Cover}" CacheMode="BitmapCache" />
        <TextBlock Text="{Binding FirstLine}" />                                        
    <StackPanel>
</DataTemplate>

和模型

 public class TitleList : ViewModelBase
{      
    public String Id_on_server { get; set; }

    public String FirstLine
    {
        get;set;        
    } 

    private static Mutex _mut = new Mutex(false,"mutex control");

    BitmapImage _Cover;

   public BitmapImage Cover
    {
        get
        {             
            if (_Cover == null)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(
                    (object sender, DoWorkEventArgs e) =>
                    {
                        String fileName = App.ImageDirectory + "/" + this.Id_on_server + "_front_thumb.jpg";
                        Deployment.Current.Dispatcher.BeginInvoke(delegate()
                        {
                            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                if (myIsolatedStorage.FileExists(fileName))
                                {
                                    try
                                    {
                                        _Cover = new BitmapImage();
                                        _Cover.SetSource(myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read));
                                    }
                                    catch (Exception ex)
                                    {
                                        System.Diagnostics.Debug.WriteLine(ex.Message);
                                    }
                                }
                                else
                                {
                                    _Cover = new BitmapImage();
                                    _Cover.UriSource = new System.Uri("/Image/Movie.Icon.Default.png", System.UriKind.Relative);                                       
                                }
                            }
                        });
                    });

                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                    (object sender, RunWorkerCompletedEventArgs e) =>
                    {
                        _mut.ReleaseMutex();
                        Deployment.Current.Dispatcher.BeginInvoke(delegate()
                        {
                            RaisePropertyChanged("Cover");
                        });
                    }
                    );

                _mut.WaitOne();
                bw.RunWorkerAsync();
            }

            return _Cover;
        }
        set
        {
            _Cover = value;
            RaisePropertyChanged("Cover");
        }
    }
}

所以我从后台的隔离存储中获取图像并显示在列表中。此代码有效,但如果 JumpList 快速滚动,则会发生显示项目的强烈延迟。

谁能建议如何正确显示大量图片或像 LowProfileImageLoader 一样制作称职的 LoadDelay。任何建议或示例都会有所帮助。

4

1 回答 1

1

在我的项目中,我使用Kawagoe Toolkit来完成类似的任务。

在您的代码中,只有 1 行代码在后台线程中执行:

String fileName = App.ImageDirectory + "/" + this.Id_on_server + "_front_thumb.jpg";

您的其余代码(包括文件 I/O)在 UI 线程中执行,这是错误的。一种修复方法:

public class TitleList : ViewModelBase
{
    public String Id_on_server { get; set; }

    BitmapImage _Cover;

    static MemoryStream readImage(string id)
    {
        String fileName = Path.Combine( App.ImageDirectory, this.Id_on_server + "_front_thumb.jpg" );
        using( IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication() )
        {
            if( !myIsolatedStorage.FileExists( fileName ) )
                return null;
            try
            {
                using( var stm = myIsolatedStorage.OpenFile( fileName, FileMode.Open, FileAccess.Read ) )
                {
                    var ms = new MemoryStream();
                    stm.CopyTo( ms );
                    return ms;
                }
            }
            catch( Exception ex )
            {
                System.Diagnostics.Debug.WriteLine( ex.Message );
            }
            return null;
        }
    }

    public BitmapImage Cover
    {
        get
        {
            if( null != _Cover )
                return _Cover;

            ThreadPool.QueueUserWorkItem( s => 
            {
                var ms = readImage( Id_on_server );
                Deployment.Current.Dispatcher.BeginInvoke( () => 
                {
                    var img = new BitmapImage();
                    if( null == ms )
                        img.UriSource = new Uri( "/Image/Movie.Icon.Default.png", System.UriKind.Relative );
                    else
                        img.SetSource( ms );
                    _Cover = img;
                    RaisePropertyChanged( "Cover" );
                } );
            } );
            return null;
        }
    }
}
于 2013-02-15T02:16:53.300 回答