Win10 UWP xaml 延迟加载元素

陌路散爱 提交于 2020-03-06 00:11:14
原文:Win10 UWP xaml 延迟加载元素

xaml新增x:DeferLoadStrategy里面只有Lazy,查询了百度看到MSP_甄心cherish大神说的
xaml使用x:DeferLoadStrategy="Lazy"延迟加载元素
我写了代码

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image x:Name="ximg1" Source="/assets/1.jpg" Grid.Row="0" Margin="10,10,10,10"/>
        <Image x:Name="ximg2" Source="/assets/2.jpg" Grid.Row="1" Margin="10,10,10,10" x:DeferLoadStrategy="Lazy"/>
        <Button Content="显示" Grid.Row="2" Margin="10,10,10,10" HorizontalAlignment="Right" Click="Button_Click"/>
    </Grid>

ximg1显示,ximg2不显示
点击按钮就显示ximg2
1.jpg和2.jpg都是随意的图片

这样和原先的Visibility="Collapsed"没有显示可是有加载好在不浪费资源,可以到用到才加载。

点击button

        private void Button_Click(object sender , RoutedEventArgs e)
        {
            FindName(nameof(ximg2));
        }

如果写FindName("ximg2");容易写错

程序启动
程序启动
点击显示
点击显示

这样做对于要加载大量的图片,而不是在用户需要显示,可以先延迟,到了需要再加载,这样加快了速度。

参考:http://blog.csdn.net/zmq570235977/article/details/47404437

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!