我试图在可缩放ScrollViewer中加载矢量图像,在我的笔记本电脑上效果很好,但放大/缩小时平板电脑上的图像非常缓慢。在图像渲染过程中,UI被冻结。Windows商店应用程序异步xaml渲染
我的图像是用Inkscape从svg转换成xaml文件的。 (〜2.37 MB)
C#
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
string fileContent;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Images/plan.xaml"));
using (StreamReader sRead = new StreamReader(await file.OpenStreamForReadAsync()))
fileContent = await sRead.ReadToEndAsync();
var xamlRead = XamlReader.Load(fileContent) as Canvas;
ImgMainGrid.Height = xamlRead.Height;
ImgMainGrid.Width = xamlRead.Width;
ImgMainGrid.Children.Add(xamlRead);
}
XAML
<ScrollViewer Grid.Row="1" x:Name="scrl" ZoomMode="Enabled" HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" MinZoomFactor="1">
<Grid x:Name="ImgMainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ScrollViewer>
我怎样才能使画布背景是什么? (不会阻止用户界面)。 如果这是不可能的,我如何处理这个渲染来渲染时添加一个ProgressBar?
你能提供更多的C#代码吗?具体来说,你什么时候调用它(在一个事件处理程序?等) - 会让我们知道你是否在UI线程上运行它,这是延迟的常见原因。 – rikkit 2014-10-06 14:26:50
编辑的第一篇文章提供更多的C#代码 – alvinmeimoun 2014-10-06 14:33:08
所以我的答案将基本上与JaredPar在这个问题中的相同:http://stackoverflow.com/questions/5395264/xamlreader-load-in-a-background-thread-is-它 - 可能 - 您的平板电脑不足以执行此操作(带有2MB文件的XamlReader.Load)而无延迟。不幸的是,必须在UI线程上使用XamlReader.Load。您可以尝试减少矢量的细节,或者可能有第三方替代此问题的地方。 – rikkit 2014-10-06 14:39:11