2014-08-31 103 views
0

我有我的ListView的麻煩。我的數據模板由圖像和2個文本塊組成。但有些圖像沒有源代碼,因此不會顯示。我認爲閃爍的原因是有些項目沒有圖像(只有文本塊),所以它們有不同的大小。那我該如何解決呢?ListView標題在滾動時閃爍

這是我的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; } 
} 
+0

在ColumnDefinition中使用*大小而不是Auto。 – Vishal 2014-08-31 15:10:30

+0

@Vishal,那不能解決問題 – FruitDealer 2014-08-31 15:14:40

+0

可以請你發佈圖片嗎? – Vishal 2014-08-31 15:19:30

回答

0

我有同樣類型的問題。當我導航到新屏幕時,我發生了一些事情,我瀏覽了一些文檔和博客文章,發現如果我將不透明度設置爲列表的xaml標記中的99%(0.99)或它所去除的整個xaml頁面。我知道這是一個非常冒險的解決方案,但最初的問題本身沒有意義。試試這個,讓我知道它是否有效。上面的代碼在將其部署到我的設備時不會閃爍。我正在使用諾基亞Lumia 1520進行測試。

另外我會建議使用MVVM模式,所以你可以讓你的邏輯超出你的UI相關的代碼。會更容易管理和發現問題。只是一個建議:)

+0

我試圖改變不透明度,但它沒有幫助我。這是我目前的應用程序的視頻,我遇到了這個麻煩。您可以從頭開始看到頂部標題閃爍:https://onedrive.live.com/redir?resid=65A15980B252B4BA!19089&authkey=!AMVH0LPJeOfwcHs&ithint=video%2cmp4 – FruitDealer 2014-09-01 16:33:23

0

我在我的應用程序中有多個ListViews完全相同的行爲。 唯一的解決方案(就我所知)是將項目內容高度設置爲固定值。

另外: 使用最小高度可以減少閃爍,如果只有少數物品的高度不同。