滚动时ListView标题闪烁



我的ListView有问题。我的数据模板由图像和2个文本块组成。但有些图像没有来源,因此无法显示。我认为闪烁的原因是有些项目没有图像(只有文本块),所以它们有不同的大小。那么我该怎么解决呢?

这是我的xaml代码:

<Page.Resources>
    <CollectionViewSource x:Name="phts" IsSourceGrouped="True"/>
    <DataTemplate x:Key="AddrBookItemTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="{Binding Image}" Name="image" MaxHeight="90" MaxWidth="90" Stretch="Fill" Tag="{Binding Url}"/>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Name="txt1" Text="{Binding Title}" Tag="{Binding Url}"/>
                <TextBlock Grid.Row="1" Name="txt2" Text="{Binding Title2}" Tag="{Binding Url}"/>
            </Grid>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="AddrBookGroupHeaderTemplate">
        <Border Background="Transparent" Margin="0,5,0,5" Tag="{Binding Key}">
            <Border Background="#E0E0E0" 
                    Width="400" Height="30" Margin="0,0,0,0" HorizontalAlignment="Left" Tag="{Binding Group_ID}">
                <TextBlock Text="{Binding Key}" Foreground="Black" FontSize="18" Padding="6"
                           FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            </Border>
        </Border>
    </DataTemplate>
</Page.Resources>
<Grid x:Name="grid">
    <ListView Background="White"
              Foreground="Black"
              ItemsSource="{Binding Source={StaticResource phts}}"
              ItemTemplate="{StaticResource AddrBookItemTemplate}">
        <ListView.GroupStyle>
            <GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"/>
        </ListView.GroupStyle>
    </ListView>
</Grid>

这是我的代码:

public sealed partial class TestPage : Page
{
    public TestPage()
    {
        this.ManipulationMode = ManipulationModes.All;
        this.InitializeComponent();
        loadContent();
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
    private void loadContent()
    {
        NewsGroup group1 = new NewsGroup("Test 1");
        NewsGroup group2 = new NewsGroup("Test 2");
        NewsGroup group3 = new NewsGroup("Test 3");
        NewsGroup group4 = new NewsGroup("Test 4");
        NewsGroup group5 = new NewsGroup("Test 5");
        NewsGroup group6 = new NewsGroup("Test 6");
        List<NewsGroup> groups = new List<NewsGroup>(new NewsGroup[] {group1, group2, group3, group4, group5, group6});
        Random rand = new Random();
        foreach(NewsGroup group in groups)
        {
            for (int i = 0; i < 10; i++)
            {
                ArticleHeader hdr = new ArticleHeader();
                if (rand.Next() % 5 == 2)
                {
                    hdr.Image = "https://pbs.twimg.com/profile_images/1885929594/Nomad_watermark_white.jpg";
                }
                hdr.Title = "Short title";
                hdr.Title2 = "Very big title here should be. Bla bla bla bla bla! Bla bla bla? Blablabl blab bla bla lab bbalblablalb blablalbal";
                group.Add(hdr);
            }
        }
        ((CollectionViewSource)Resources["phts"]).Source = groups;
    }
}
public class ArticleHeader
{
    public string Time { get; set; }
    public string Title { get; set; }
    public string Url { get; set; }
    public string Image { get; set; }
    public string Title2 { get; set; }
}
public class NewsGroup : List<ArticleHeader>
{
    public NewsGroup(string name)
    {
        Key = name;
    }
    public string Key { get; set; }
    public string GroupUrl { get; set; }
    public string Group_ID { get; set; }
}

我遇到了同样类型的问题。当我导航到一个新屏幕时,我浏览了周围的一些文档和博客文章,发现如果我在列表或整个xaml页面的xaml标记中将不透明度设置为99%(0.99),它就会消失。我知道这是一个非常棘手的解决方案,但最初的问题本身毫无意义。试试这个,让我知道它是否有效。当我将上面的代码部署到我的设备上时,它不会闪烁。我正在使用诺基亚Lumia 1520进行测试。

此外,我建议使用MVVM模式,这样您就可以在UI相关代码之外使用逻辑。会更容易管理和发现问题。只是一个建议:)

我在应用程序的多个ListView中有完全相同的行为。唯一的解决方案(据我所知)是将Item内容的高度设置为固定值。

此外:如果只有少数物品具有不同的高度,则使用最小高度可以减少闪烁。

最新更新