2

我实施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其中有VisualStateManagerxaml:

<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>

LongListSelectorxml:

<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>
4

1 回答 1

0

你为什么不尝试一些不同的东西来突出显示元素?,你可以在选择更改方法之后绑定背景颜色属性或类似的东西,如果你很好地使用了 mvvm 模式,你应该有 INotifypropertyChange,所以,它必须改变这个值给其他人做绑定到那个属性

于 2013-11-20T22:51:32.717 回答