如何调暗 UWP 应用程序列表中的阅读项目

How To Dim Read Items In A UWP App List

我有一个simple Master/Detail app called Daily Bible Reading.

我的 TODO 项目之一是通过使任何 ListView 项目变暗来跟踪您阅读的内容。

<ListView Name="Chapters" ItemsSource="{x:Bind ChapterCollection}" IsItemClickEnabled="True" ItemClick="Chapter_ItemClick">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:ChapterItem">
            <StackPanel>
                <TextBlock Text="{x:Bind date}" Foreground="White"/>
                <TextBlock Text="{x:Bind ChapterReference}" Foreground="White"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我的想法是,当每天加载内容或您 select 其中一项时,它会在用户的 RoamingSettings 中设置一个变量。我正在为设置 FontSize.

做类似的事情
public ApplicationDataContainer roamingsettings = ApplicationData.Current.RoamingSettings;

private void FontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var newfontsize = FontSizeComboBox.SelectedIndex + 10;

    // store the user preferences
    // get the device name
    var deviceinfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation();
    var devicename = deviceinfo.FriendlyName;
    // set the preference
    roamingsettings.Values[devicename + "_FontSize"] = newfontsize.ToString();
}

如果用户确实设置了它,则尊重该偏好。

// set TextBlock FontSize and then change if the user has preferences
private void SetFontSize(object sender, RoutedEventArgs e)
{
    var textblock = (TextBlock)sender;
    // set a default
    textblock.FontSize = 20;

    // load user preferences
    // get the device name
    var deviceinfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation();
    var devicename = deviceinfo.FriendlyName;
    // check to see if the preference is there
    if (roamingsettings.Values.ContainsKey(devicename + "_FontSize"))
    {
        var fontsize = Convert.ToInt32(roamingsettings.Values[devicename + "_FontSize"]);
        textblock.FontSize = fontsize;
    }
}

我想我可以对项目的 Foreground 做类似的事情(只需将 Foreground=White 更改为 Foreground=Grey 或其他)。问题是我不知道如何在每个项目的基础上进行更改。当应用程序加载时,字体更改是全局的。我的项目正在从 ObservableCollection.

加载
public sealed partial class MainPage : Page
{
    public ObservableCollection<ChapterItem> ChapterCollection { get; set; }

    public MainPage()
    {
        this.InitializeComponent();

        ChapterCollection = new ObservableCollection<ChapterItem>();
    }

    // what to do when the page is navigated to
    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        await MainPageViewModel.PopulateCollections(ChapterCollection);
    }
}

我认为答案就在转换器的黑暗领域中的某个地方。我这么说是因为我无法理解它们。

如果你愿意,我很乐意postViewand/orViewModel代码。我不确定这是否对问题有任何好处。

作为次要观点,我可能以错误的方式思考所有这些。我对编程有足够的了解,可以使事情正常进行。我不知道什么是最佳实践。我会说该应用程序的代码比几年前要好得多。除了具体问题,我欢迎任何一般性指导。

您将不得不使用一个转换器,并且您需要在模型中添加一个额外的 属性 以将其与 StackPanel 的 background/opacity 绑定。您需要在页面级别或应用级别添加此转换器资源

假设您有 (bool) "HasBeenRead" 那么:

<ListView Name="Chapters" ItemsSource="{x:Bind ChapterCollection}" IsItemClickEnabled="True" ItemClick="Chapter_ItemClick">
<ListView.ItemTemplate>
    <DataTemplate x:DataType="data:ChapterItem">
        <StackPanel Background="{x:Bind HasBeenRead, Converter={StaticResource CustomColorConverter}, Mode=TwoWay}">
            <TextBlock Text="{x:Bind date}" Foreground="White"/>
            <TextBlock Text="{x:Bind ChapterReference}" Foreground="White"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

public sealed class CustomColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value != null && (bool)value)
        {
            return new SolidColorBrush(Colors.Red);
        }
        else
        {
            return new SolidColorBrush(Colors.Yellow);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}