我實現了VisualStateManager
以突出顯示LongListSelector
中的選定項。SelectionChanged事件後無法重置LongListSelector中Grid的可視狀態
所選項目在SelectionChanged
事件中突出顯示,但問題在於事件完全執行時,所選項目仍然突出顯示。即使我離開頁面並返回到原始頁面,該項目仍然突出顯示。如果我在SelectionChanged
事件的末尾添加selector.SelectedItem = null;
,它會再次檢查該方法,直至最終拋出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
XAML:
<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>
我擺脫了並且改變了我想讓事情發揮作用的方式。無論如何感謝您的回答:) –