如何在UWP中删除ListViewItem的破碎背景?

时间:2017-03-23 22:58:12

标签: c# listview uwp

enter image description here

我创建了包含一些项目的Listview。 当我点击一个时,我正在导航到新页面。 当我按下时,我将回到旧页面(主菜单页面 - >项目页面 - >通过Frame.GoBack()支持主菜单)和 我看到所有最后点击的项目都是灰色背景。 我试图将背景设置为透明,但它不起作用。

在桌面上此问题不存在,背景为黑色。 我正在Windows 10 RS2& Windows 10 Mobile是L640XL的最新内部版本。

列表视图:

<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 

并且它不起作用

EDIT2 在主菜单页面中,当我点击按钮并返回时,此效果保持不变。所有网页都有NavigationCachePage=Required main menu

EDIT3

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

其中任何一个都没有解决这个问题。

2 个答案:

答案 0 :(得分:2)

你可以在这里尝试一些事情。我相信您看到的灰色背景PressedBackground的{​​{1}}。

最有可能是UWP中的时间错误,当ListViewItemPresenter中的某个项目被选中/按下时,ListView / PressedBackground显示,同时时间第1页被缓存并从PointerOverBackground中清除,然后可以显示第2页,但在缓存之前应该发生的是页面1 需要完成指针向上/退出操作才能清除Frame / PressedBackground颜色,但未能这样做。

要测试问题的颜色是否正确,您只需将以下默认PointerOverBackground应用于Style的{​​{1}},然后设置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

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

答案 1 :(得分:0)

在列表视图中处理onclick / onselect事件并将项目设置为Unselected。