滚动时ListView标题闪烁

时间:2014-08-31 14:54:56

标签: c# xaml listview windows-phone-8 windows-phone-8.1

我的ListView遇到了麻烦。我的datatemplate包含图像和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; }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

相关问题