我在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快速滚动,则显示项目的强烈延迟正在发生。
任何人都可以建议如何正确显示大量图片或制作胜任的LoadDelay,如LowProfileImageLoader。 任何建议或示例都会有所帮助。
答案 0 :(得分:1)
在我的项目中,我使用Kawagoe Toolkit来完成类似的任务。
在您的代码中,只有一行代码在后台线程中执行:
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;
}
}
}