我实施VisualStateManager
以突出显示LongListSelector
.
事件期间选中的项是高亮的SelectionChanged
,但是问题是当事件执行完之后,选中的项还是高亮的。即使我离开页面并返回到原始页面,该项目仍然突出显示。如果我selector.SelectedItem = null;
在事件结束时添加SelectionChanged
,它会再次执行该方法,直到最终引发Object reference not set to instance of an object
异常。
我如何将所选项目的视觉状态重置为Normal
一旦停止使用?
SelectionChanged
事件:
private async void POIS_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<CustomUserControl> userControlList = new List<CustomUserControl>();
GetItemsRecursive<CustomUserControl>(PoiLongListSelector, ref userControlList);
//seleted
if(e.AddedItems.Count > 0 && e.AddedItems[0] != null)
{
foreach (CustomUserControl userControl in userControlList)
{
if (e.AddedItems[0].Equals(userControl.DataContext))
{
VisualStateManager.GoToState(userControl, "Selected", true);
}
}
}
//Unselected
if (e.RemovedItems.Count > 0 && e.RemovedItems[0] != null)
{
foreach (CustomUserControl userControl in userControlList)
{
if (e.RemovedItems[0].Equals(userControl.DataContext))
{
VisualStateManager.GoToState(userControl, "Normal", true);
}
}
}
LongListSelector selector = sender as LongListSelector;
PoiData ld = selector.SelectedItem as PoiData;
string navigateUrl = "";
SystemTray.ProgressIndicator = new ProgressIndicator();
SetProgressIndicator(true);
//CHECK IF RETURNING NULL
SystemTray.ProgressIndicator.Text = "Getting GPS data";
GeoCoordinate coordinate = await GetLocation(ctsPoi.Token);
if (coordinate != null)
{
string passedUrl = GenerateUrl(coordinate, ld.Type);
if (passedUrl != null)
{
SystemTray.ProgressIndicator.Text = "Getting POI data";
string jsonData = await GetJsonDataFromGoogle(passedUrl, ld.Type);
if (jsonData != null)
{
string url = SerializeJsonData(jsonData, ld.Type);
if (url != null)
{
SystemTray.ProgressIndicator.Text = "Done";
navigateUrl = string.Format("/ViewDirection.xaml?serializedData={0}", url);
}
}
}
}
if(navigateUrl != "")
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(navigateUrl, UriKind.RelativeOrAbsolute));
}
CustomUserControl
其中有VisualStateManager
xaml:
<Grid x:Name="LayoutRoot">
<Grid Margin="0,0,0,15" Grid.Row="0">
<Grid Name="MainGrid" Opacity="1" Visibility="Visible" >
<Grid.Background>
<SolidColorBrush>
<Color>#ff00bfff</Color>
</SolidColorBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Background="{StaticResource PhoneAccentBrush}" Grid.Column="0" Width="65" HorizontalAlignment="Left"
Height="65" Margin="0, 0, 0, 0">
</Grid>
<Grid Grid.Column="1">
<TextBlock Text="{Binding Title}"
FontSize="30" Margin="10,0,0,0"
VerticalAlignment="Center"
Foreground="White"/>
</Grid>
</Grid>
<ProgressBar x:Name="ATMBar" Visibility="Visible"
Opacity="0"
VerticalAlignment="Center"
Margin="0,0,0,0"
IsIndeterminate="True"
Style="{StaticResource CustomIndeterminateProgressBar}" />
</Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To="0.2" Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="MainGrid" />
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="ATMBar" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
LongListSelector
xml:
<phone:LongListSelector Name="PoiLongListSelector" Margin="12,-20,0,75"
ItemsSource="{Binding Poi.Items}"
SelectionChanged="POIS_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<myUserControl:CustomUserControl />
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>