Windows 8 系列(十一):开发小技巧分享
临时记录、分享一些小的开发技巧:
1. 在数据绑定时,如果数据源需要被清空:collection.Clear(); 建议在语句后把集合置为null:collection.Clear(); collection= null;(开发过程中因为没有置为null而报错,Windows.UI.Xaml.dll的错误)
2. async void 函数与async Task函数不能混为一谈。
async void FUNCTIONNAME1(){ ... await ...; Debug.WriteLine("FUNCTIONNAME1 over")};
async Task FUNCTIONNAME2(){ ... await ...; Debug.WriteLine("FUNCTIONNAME2 over")};
section1: FUNCTIONNAME1(); Debug.WriteLine("FUNCTIONNAME1");
section2: await FUNCTIONNAME2(); Debug.WriteLine("FUNCTIONNAME2");
以上两段代码中section1的代码在执行时会异步执行,有可能先输出FUNCTIONNAME1,然后输出FUNCTIONNAME1 over。第二段代码则能保证在输出FUNCTIONNAME2 over后再输出FUNCTIONNAME2。
3. 你的代码是不是真的实现了虚拟化?不能单单看你的代码是不是写了相关控件,而需要用VirtualizingStackPanel的CleanUpVirtualizedItemEvent事件来确定是不是真正的实现了虚拟化。我们在gridview的style中修改了样式,导致上述事件不触发:
<Style x:Key="PaddingGridViewStyle" TargetType="GridView"> <Setter Property="Padding" Value="0,0,0,10"/> <Setter Property="IsTabStop" Value="False"/> <Setter Property="TabNavigation" Value="Once"/> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/> <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Enabled"/> <Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="False"/> <Setter Property="ScrollViewer.VerticalScrollMode" Value="Disabled"/> <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="False"/> <Setter Property="ScrollViewer.ZoomMode" Value="Disabled"/> <Setter Property="IsSwipeEnabled" Value="True"/> <Setter Property="ItemContainerTransitions"> <Setter.Value> <TransitionCollection> <AddDeleteThemeTransition/> <ContentThemeTransition/> <ReorderThemeTransition/> <EntranceThemeTransition IsStaggeringEnabled="False"/> </TransitionCollection> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="GridView"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1"> <ScrollViewer x:Name="ScrollViewer" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsHorizontalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsHorizontalScrollChainingEnabled}" IsVerticalScrollChainingEnabled="{TemplateBinding ScrollViewer.IsVerticalScrollChainingEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" TabNavigation="{TemplateBinding TabNavigation}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"> <Grid Margin="{TemplateBinding Padding}"> <ItemsPresenter HeaderTemplate="{TemplateBinding HeaderTemplate}" Header="{TemplateBinding Header}" HeaderTransitions="{TemplateBinding HeaderTransitions}" /> </Grid> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
代码中红色的部分导致虚拟化被破坏,修改后
<ScrollViewer x:Name="ScrollViewer" ...> <ItemsPresenter HeaderTemplate="{TemplateBinding HeaderTemplate}" Header="{TemplateBinding Header}" Margin="{TemplateBinding Padding}" HeaderTransitions="{TemplateBinding HeaderTransitions}" /> </ScrollViewer>