[英]How to remove broken background of ListViewItem In UWP?
我用一些項目創建了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
EDIT3
ButtonListGridView.InvalidateMeasure();
ButtonListGridView.UpdateLayout();
ButtonListGridView.InvalidateArrange();
其中任何一個都沒有解決這個問題。
你可以在這里嘗試一些事情。 我相信你看到的灰色背景是ListViewItemPresenter
的PressedBackground
。
UWP中的定時錯誤很可能是當選擇/按下ListView
的項目時, PressedBackground
/ PointerOverBackground
顯示,同時頁面1被緩存並從Frame
中清除,然后可以顯示第2頁 ,但是在緩存之前應該發生的是頁面1需要完成指針向上/退出操作才能清除PressedBackground
/ PointerOverBackground
顏色,但是它沒有這樣做。
要測試這是否是問題的顏色,您只需將以下默認Style
應用於ListView
的ItemContainerStyle
,並設置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,所以我無法真正測試代碼,但希望它會給你一個想法。 祝好運!
在列表視圖中處理onclick / onselect事件並將項目設置為Unselected。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.