2012-04-25 94 views
1

因此,我有一個簡單的RSS閱讀器,它有一個在應用程序啓動時得到更新的訂閱源。如何添加使新未讀項目保持不同顏色的功能?我想讓用戶可以看到自上次打開應用以來哪些帖子是新的。RSS閱讀器,保持未讀項目

+0

歸結爲兩件事:以不同顏色顯示項目並記住所讀內容。更具體的問題會很好。 – Thilo 2012-04-25 06:39:27

回答

3

假設你有一個類似的模型;

public class RSSItem { 
    public bool IsUnread { get; set; } 
    public string Title { get; set; } 
} 

你要使用IValueConverter,需要一個bool並返回一個Color一個TextBlockForegroundColor綁定到IsUnread財產。所以你的XAML可能看起來像;

<phone:PhoneApplicationPage.Resources> 
    <converters:UnreadForegroundConverter x:Key="UnreadForegroundConverter" /> 
</phone:PhoneApplicationPage.Resources> 

<ListBox x:Name="RSSItems"> 
    <DataTemplate> 
    <TextBlock Text="{Binding Title}" Foreground="{Binding IsUnread, Converter={StaticResource UnreadForegroundConverter}}" /> 
    </DataTemplate> 
</ListBox> 

不要忘了將xmlns:converters屬性添加到Page的標記中。

你會再想要實現你的IValueConverter做布爾顏色轉換;

public class UnreadForegroundConverter : IValueConverter { 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
    if ((bool)value == true) { 
     return Application.Current.Resources["PhoneAccentColor"]; 
    } 

    return Application.Current.Resources["PhoneForegroundColor"]; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { 
    throw new NotImplementedException(); 
    } 

} 

而且很明顯,你需要在列表框,RSSItems結合,給RSSItem集合。例如。

ObservableCollection<RSSItem> items = new ObservableCollection<RSSItem>(); 
// populate items somehow 
RSSItems.ItemsSource = items; 

希望有幫助。

+0

優秀的MrDavidson! – 2012-04-26 05:42:34