c# - 如何删除 UWP 中 ListViewItem 的破损背景?

标签 c# listview uwp

enter image description here

我创建了一个包含一些项目的 ListView 。 当我点击其中一个时,我正在导航到新页面。 当我按返回键时,我将返回到旧页面(主菜单页面 -> 项目页面 -> 通过 Frame.GoBack() 返回主菜单)和 我看到所有最后点击的项目都有灰色背景。 我尝试将背景设置为透明,但没有用。

在桌面上不存在这个问题,背景是黑色的。 我正在 Windows 10 RS2 和 Windows 10 移动版 L640XL 上测试它。

ListView :

<ListView Grid.Row="
          Name="LineSecondTrackListView"
          ItemsSource="{x:Bind _LineSecondTrackBusStops}"
          ContainerContentChanging="SetBusStopViewAttribute"
          ItemTemplate="{StaticResource BusStopListViewStyle}"
          SelectionMode="Single"
          SelectionChanged="LineTrackListView_SelectionChangedAsync">
              <ListView.ItemsPanel>
                  <ItemsPanelTemplate>
                      <ItemsWrapGrid HorizontalAlignment="Center"
                                     Orientation="Horizontal" 
                                     MaximumRowsOrColumns="1"/>
                  </ItemsPanelTemplate>
              </ListView.ItemsPanel>
</ListView>

我的支持方式:

public static void BackButtonPressed(object sender, BackRequestedEventArgs e)
{
    Frame mainAppFrame = MainFrameHelper.GetMainFrame();
    Type currentPageType = mainAppFrame.CurrentSourcePageType;
    bool goBack = IsGoBackFromPageAllowed(currentPageType);
    if (goBack)
    {
        mainAppFrame.GoBack();
        e.Handled = true;
        return;
    }
    App.Current.Exit();
}

private static bool IsGoBackFromPageAllowed(Type currentPageType)
{
    if (currentPageType == typeof(Pages.Lines.LinesViewPage))
        return true;
    if (currentPageType == typeof(Pages.Lines.LinePage))
        return true;
    if (currentPageType == typeof(Pages.Lines.LineBusStopPage))
        return true;
    return false;
}

如何避免这种影响?

编辑

我试过

foreach (ListViewItem item in LineSecondTrackListView.Items) 
    VisualStateManager.GoToState(item, "Normal", false); //in the OnNavigatedTo 

它不起作用

编辑2 在主菜单页面中,当我单击按钮并返回时,这种效果仍然存在。所有页面都有 NavigationCachePage=Required main menu

edit3

ButtonListGridView.InvalidateMeasure();
ButtonListGridView.UpdateLayout();
ButtonListGridView.InvalidateArrange();

这些都没有解决这个问题。

最佳答案

您可以在这里尝试一些事情。我相信您看到的灰色背景PressedBackgroundListViewItemPresenter .

ListView 中的项目时,这很可能是 UWP 中的计时错误。选择/按下时,PressedBackground/PointerOverBackground显示,同时 第 1 页 被缓存并从 Frame 中清除然后可以显示page 2,但是在缓存之前应该发生的是page 1需要按顺序完成pointer up/exit action清除 PressedBackground/PointerOverBackground颜色,它没有这样做。

要测试这是否是问题的颜色,您只需应用以下默认值 Style给你的ItemContainerStyle你的ListView , 并设置 PressedBackground="Transparent"或/和 PointerOverBackground="Transparent" .

<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Padding" Value="12,0,12,0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="ListViewItem">
      <ListViewItemPresenter
          ContentTransitions="{TemplateBinding ContentTransitions}"
          SelectionCheckMarkVisualEnabled="True"
          CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
          CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
          DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
          DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
          FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
          FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
          PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
          PointerOverBackground="Transparent"
          PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
          SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"
          SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
          SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
          PressedBackground="Transparent"
          SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
          DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
          DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
          ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
          ContentMargin="{TemplateBinding Padding}"
          CheckMode="Inline"/>
    </ControlTemplate>
  </Setter.Value>
</Setter>
</Style>

但即使这可能会解决问题,它也会删除有意义的视觉指示。所以让我们尝试别的东西 -

在你的LineTrackListView_SelectionChangedAsync方法,不是在延迟后立即导航,而是先取消选择项目,给它相同的延迟时间以完成取消选择操作,然后再进行导航。您将需要有一个标志来避免通过更新 SelectedItem 重新调用此方法。 .

private bool _isWorking;

private async void LineTrackListView_SelectionChangedAsync(object sender, SelectionChangedEventArgs e)
{
    if (_isWorking)
    {
        return;
    }

    _isWorking = true;

    // Removed some of your code here.

    listView.SelectedItem = -1;
    await Task.Delay(100);

    ChangePageToBusPage(selectedBusStopInListView.BusStop, selectedTrack);

    _isWorking = false;
}

我没有 Windows Phone,所以我无法真正测试代码,但希望它能给你一个想法。祝你好运!

关于c# - 如何删除 UWP 中 ListViewItem 的破损背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42988498/

相关文章:

c# - 更新绑定(bind)到 dataSet dataTable 的 listView,它从 SQL c# wpf 中查找数据

android - 在getView中获取ImageView宽度

visual-studio - 用于SQLite的UWP数据库设计器

sqlite - 无法在 UWP 应用程序中为 EF Core Sqlite 创建迁移

c# - Azure WebJob 在同一主机上调用 WebAPI

c# - 使用 JS 调用 C# 方法

c# - 确定 Clickonce 应用程序在 Clickonce 缓存中的位置

c# - 当鼠标经过标签并随后恢复正常时,如何使标签变粗

android - 如何逐项显示 ListView 滚动

windows - 在没有 Windows 应用商店的情况下交付 UWP 应用程序的首选方式