2

Я собираюсь снять волосы, но прежде чем я это сделаю, я попробую вас, ребята, о помощи ... возможно, у кого-то была такая же проблема и она может мне помочь.Серьезная ошибка с LongListSelector с ItemTemplate или IValueConverter

Так вот сделка. У меня есть LongListSelector имя SourceZoneSelect определяется как таковую:

<phone:LongListSelector x:Name="SourceZoneSelect" Grid.Row="2" 
       LayoutMode="Grid" 
       GridCellSize="160,160" 
       BorderThickness="0" 
       HorizontalContentAlignment="Stretch" 
       VerticalContentAlignment="Stretch" 
       Background="White" 
       Width="480" Foreground="Black" 
       ItemTemplate="{StaticResource DataTemplateContentZone}" 
       Style="{StaticResource LongListSelectorStyleContentZone}" 
       ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="-2,0,0,0"/> 

ItemTemplate является DataTemplateSelector как таковые:

  <DataTemplate x:Key="DataTemplateContentZone"> 
        <local:ContentZoneTemplateSelector Content="{Binding}" VerticalAlignment="Top" HorizontalAlignment="Right"> 
         ... 
         <local:ContentZoneTemplateSelector.PhotoWebSelect> 
          <DataTemplate> 
           <Grid Height="158" Width="158"> 
            <Button BorderThickness="0" Style="{StaticResource ButtonStyleStyle}" Width="158" Height="158" toolkit:TiltEffect.IsTiltEnabled="True" Click="Button_SourceZone_Select_Click"> 
             <Grid HorizontalAlignment="Left" Height="158"> 
              <Rectangle Fill="{StaticResource StayfilmThirdColor}" HorizontalAlignment="Left" Height="158" Stroke="Black" VerticalAlignment="Top" Width="158"/> 
              <Image x:Name="img" Stretch="UniformToFill" Width="158" Height="158" Source="{Binding Thumb}" Tag="{Binding ImgUrl}"/> 
              <Grid x:Name="GridSelected" Tag="{Binding idMedia}" HorizontalAlignment="Right" Height="35" VerticalAlignment="Top" Width="35" Background="#FF0E9B00" Visibility="{Binding selected, Converter={StaticResource PhotoSelectedConverter}}"> 
               <Path Data="F1M0.555,6.585L0.089,6.077C-0.036,5.942,-0.029,5.73,0.107,5.605L1.464,4.344C1.525,4.287 1.606,4.255 1.689,4.255 1.785,4.255 1.871,4.293 1.935,4.361L5.661,8.373 12.07,0.126C12.134,0.047 12.23,0 12.334,0 12.41,0 12.481,0.024 12.54,0.07L14.003,1.206C14.073,1.26 14.117,1.339 14.128,1.429 14.141,1.518 14.116,1.606 14.061,1.674L5.826,12.263z" Fill="White" Margin="0" UseLayoutRounding="False" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
              </Grid> 
             </Grid> 
            </Button> 
           </Grid> 
          </DataTemplate> 
         </local:ContentZoneTemplateSelector.PhotoWebSelect> 
         <local:ContentZoneTemplateSelector.VideoSelect> 
         <DataTemplate> 
          <Grid Height="158" Width="158"> 
           <Button BorderThickness="0" Style="{StaticResource ButtonStyleStyle}" Width="Auto" Height="Auto" toolkit:TiltEffect.IsTiltEnabled="True" Click="Button_SourceZone_Select_Click"> 
            <Grid Height="158" Width="158"> 
             <Image x:Name="videoThumb" Stretch="Fill" Width="158" Height="158" Source="{Binding VideoThumbUrl}"/> 
             <Image HorizontalAlignment="Center" Height="80" VerticalAlignment="Center" Width="80" Source="/Assets/Dark/feature.video.png"/> 
             <Grid x:Name="GridSelected" Tag="{Binding idMedia}" HorizontalAlignment="Right" Height="35" VerticalAlignment="Top" Width="35" Background="#FF0E9B00" Visibility="{Binding Selected, Converter={StaticResource VideoSelectedConverter}}"> 
              <Path Data="F1M0.555,6.585L0.089,6.077C-0.036,5.942,-0.029,5.73,0.107,5.605L1.464,4.344C1.525,4.287 1.606,4.255 1.689,4.255 1.785,4.255 1.871,4.293 1.935,4.361L5.661,8.373 12.07,0.126C12.134,0.047 12.23,0 12.334,0 12.41,0 12.481,0.024 12.54,0.07L14.003,1.206C14.073,1.26 14.117,1.339 14.128,1.429 14.141,1.518 14.116,1.606 14.061,1.674L5.826,12.263z" Fill="White" Margin="0" UseLayoutRounding="False" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
             </Grid> 
            </Grid> 
           </Button> 
          </Grid> 
         </DataTemplate> 
        </local:ContentZoneTemplateSelector.VideoSelect> 
... 
</local:ContentZoneTemplateSelector> 
     </DataTemplate> 

А также 2 преобразователей значения:

in .xaml file 

<phone:PhoneApplicationPage.Resources> 
     <local:PhotoSelectedConverter x:Name="PhotoSelectedConverter"/> 
     <local:VideoSelectedConverter x:Name="VideoSelectedConverter"/> 


in .cs file 

    public class PhotoSelectedConverter : IValueConverter 
    { 
     public object Convert(object value, Type typeName, object parameter, CultureInfo language) 
     { 
      bool selected = (bool)value; 
      if (!selected) 
      { 
       return "Collapsed"; 
      } 
      else 
      { 
       return "Visible"; 
      } 
     } 

     public object ConvertBack(object value, Type typeName, object parameter, CultureInfo language) 
     { 
      return DependencyProperty.UnsetValue; 
     } 
    } 

    public class VideoSelectedConverter : IValueConverter 
    { 
     public object Convert(object value, Type typeName, object parameter, CultureInfo language) 
     { 
      double selected = (double)value; 
      if (selected == 0.0) 
      { 
       return "Collapsed"; 
      } 
      else 
      { 
       return "Visible"; 
      } 
     } 

     public object ConvertBack(object value, Type typeName, object parameter, CultureInfo language) 
     { 
      return DependencyProperty.UnsetValue; 
     } 
    } 

И это код который непосредственно манипулирует Grid с именем GridSelected, который определен на datatemplate, как если бы объект привязанный -> выбранное свойство false, GridSelected должен быть свернут a го, если это правда, то это должно быть видно ...

вот код:

  ...  List<StayClassLibrary.Media> medias = await Media.GetMediasByAlbum(albmLst, App.Current.AppUser, App.Current.WSConfig); 

        if (selectedSourceZone.Count > 0) 
        { 
         selectedSourceZone.Clear(); 
        } 
        selectedSourceZone = null; 
        selectedSourceZone = new ObservableCollection<IDisposable>(); 

        SourceZoneSelect.ClearValue(LongListSelector.DataContextProperty); 
        SourceZoneSelect.ClearValue(LongListSelector.ItemsSourceProperty); 

        SourceZoneSelect.UpdateLayout(); 
        popupSourceZone_Select.UpdateLayout(); 
        GridSourceZoneSelect.UpdateLayout(); 

        if (medias.Count == 0) 
        { 
         NoImagesFound.Visibility = System.Windows.Visibility.Visible; 
        } 
        else 
        { 
         NoImagesFound.Visibility = System.Windows.Visibility.Collapsed; 

         //TODO criar PictureItems 
         foreach (StayClassLibrary.Media item in medias) 
         { 
          BitmapImage b = new BitmapImage(); 
          b.UriSource = new Uri(item.thumbnail, UriKind.RelativeOrAbsolute); 
          b.ImageFailed += b_ImageFailed; 

          bool found = false; 
          foreach (var media in selectedContentZone) 
          { 
           if(media.GetType() == typeof(PictureItemWeb)) 
           { 
            if(((PictureItemWeb)media).idMedia == item.idMidia) 
            { 
             if (!selectedSourceMedias.Contains(item.idMidia.ToString())) 
             { 
              found = true; 
             } 
             break; 
            } 
           } 
           else if(media.GetType() == typeof(VideoItem)) 
           { 
            if (((VideoItem)media).idMedia == item.idMidia) 
            { 
             if (!selectedSourceMedias.Contains(item.idMidia.ToString())) 
             { 
              found = true; 
             } 
             break; 
            } 
           } 
          } 

          if (item.type == "image") 
          { 
           selectedSourceZone.Insert(0, new PictureItemWeb 
           { 
            Thumb = b, 
            Pic = new BitmapImage(new Uri(item.source, UriKind.RelativeOrAbsolute)), 
            dateBinary = DateTime.Now.ToBinary(), 
            idAlbum = item.idAlbum, 
            idMedia = item.idMidia, 
            ImgUrl = item.source, 
            ThumbUrl = item.thumbnail, 
            selected = false, 
            selectable = true, 
            added = found, 
            cancellationToken = new CancellationTokenSource() 
           }); 
          } 
          else if (item.type == "video") 
          { 
           selectedSourceZone.Insert(0, new VideoItem 
           { 
            Thumb = b, 
            dateBinary = DateTime.Now.ToBinary(), 
            idAlbum = item.idAlbum, 
            idMedia = item.idMidia, 
            VideoUrl = item.source, 
            VideoThumbUrl = item.thumbnail, 
            Selected = 0.0, 
            selectable = true, 
            added = found, 
            cancellationToken = new CancellationTokenSource() 
           }); 
          } 
          b = null; 
         } 

         if (selectedSourceZone.Count > 0) 
         { 
          Storyboard sb = new Storyboard(); 
          DoubleAnimation fadein = new DoubleAnimation(); 
          fadein.From = 0.0; 
          fadein.To = 1.0; 
          fadein.Duration = new Duration(TimeSpan.FromSeconds(0.35)); 
          Storyboard.SetTarget(fadein, SourceZoneSelect); 
          Storyboard.SetTargetProperty(fadein, new PropertyPath("Opacity")); 
          sb.Children.Add(fadein); 
          sb.Begin(); 

          SourceZoneSelect.ItemsSource = null; 
          SourceZoneSelect.ClearValue(LongListSelector.DataContextProperty); 
          SourceZoneSelect.ClearValue(LongListSelector.ItemsSourceProperty); 
          SourceZoneSelect.ItemsSource = selectedSourceZone; 
// *1 

          foreach (var item in selectedSourceZone) 
          { 
           if(item.GetType() == typeof(PictureItemWeb)) 
           { 
            if(selectedSourceMedias.Contains(((PictureItemWeb)item).idMedia.ToString())) 
            { 
             ((PictureItemWeb)item).selected = true; 
            } 
           } 
           else if (item.GetType() == typeof(VideoItem)) 
           { 
            if (selectedSourceMedias.Contains(((VideoItem)item).idMedia.ToString())) 
            { 
             ((VideoItem)item).Selected = 1.0; 
            } 
           } 
           UpdateSelectedState(SourceZoneSelect, item); // *2 
          } 
         } 
         else 
         { 
          NoImagesFound.Visibility = System.Windows.Visibility.Visible; 
          Storyboard sb = new Storyboard(); 
          DoubleAnimation fadein = new DoubleAnimation(); 
          fadein.From = 0.0; 
          fadein.To = 1.0; 
          fadein.Duration = new Duration(TimeSpan.FromSeconds(0.35)); 
          Storyboard.SetTarget(fadein, NoImagesFound); 
          Storyboard.SetTargetProperty(fadein, new PropertyPath("Opacity")); 
          sb.Children.Add(fadein); 
          sb.Begin(); 
         } 
        } 

* 1 здесь, когда я поставил SourceZoneSelect.ItemsSource = selectedSourceZone; он должен активировать IValueConverter, и он не ... У меня там точка останова, и она никогда не проходит там.

* 2 эта функция UpdateSelectedState() Я создал пытается вручную исправить и видимость GridSelected для каждого носителя

private void UpdateSelectedState(DependencyObject targetElement, object item) 
    { 
     var count = VisualTreeHelper.GetChildrenCount(targetElement); 
     if (count == 0) 
      return; 

     for (int i = 0; i < count; i++) 
     { 
      var child = VisualTreeHelper.GetChild(targetElement, i); 
      if (child is Button) 
      { 
       Button targetItem = (Button)child; 

       if (item.GetType() == typeof(PictureItemWeb)) 
       { 
        if (((PictureItemWeb)targetItem.DataContext).idMedia == ((PictureItemWeb)item).idMedia) 
        { 
         var g = targetItem.FindName("GridSelected"); 
         if (((Guid)((Grid)g).Tag) == ((PictureItemWeb)item).idMedia) 
         { 
          if (((PictureItemWeb)item).selected) 
          { 
           ((Grid)g).Visibility = System.Windows.Visibility.Visible; 
          } 
          else 
          { 
           ((Grid)g).Visibility = System.Windows.Visibility.Collapsed; 
          } 
         } 
         return; 
        } 
       } 
       else if (item.GetType() == typeof(VideoItem)) 
       { 
        if (((VideoItem)targetItem.DataContext).idMedia == ((VideoItem)item).idMedia) 
        { 
         var g = targetItem.FindName("GridSelected"); 
         if (((VideoItem)item).Selected == 1.0) 
         { 
          ((Grid)g).Visibility = System.Windows.Visibility.Visible; 
         } 
         else 
         { 
          ((Grid)g).Visibility = System.Windows.Visibility.Collapsed; 
         } 
         return; 
        } 
        return; 
       } 
      } 
      else 
      { 
       UpdateSelectedState(child, item); 
      } 
     } 
    } 

Это код, когда я нажимаю на изображении. (Все это делает изменить выбранное свойство истина/ложь и показать/скрыть GridSelected для пункт щелкнул:.

private void Button_SourceZone_Select_Click(object sender, RoutedEventArgs e) 
    { 
     var media = ((Button)sender).DataContext; 
     if (media.GetType() == typeof(PictureItemWeb)) 
     { 
      int index = selectedSourceZone.IndexOf(((PictureItemWeb)((Button)sender).DataContext)); 
      if (index != -1) 
      { 
       ((PictureItemWeb)selectedSourceZone.ElementAt(index)).selected = !((PictureItemWeb)selectedSourceZone.ElementAt(index)).selected; 
      } 

      var g = ((Button)sender).FindName("GridSelected"); 

      if (((PictureItemWeb)selectedSourceZone.ElementAt(index)).selected) 
      { 
       ((Grid)g).Visibility = System.Windows.Visibility.Visible; 
      } 
      else 
      { 
       ((Grid)g).Visibility = System.Windows.Visibility.Collapsed; 
      } 
     } 
     else if (media.GetType() == typeof(VideoItem)) 
     { 
      int index = selectedSourceZone.IndexOf(((VideoItem)((Button)sender).DataContext)); 
      if (index != -1) 
      { 
       ((VideoItem)selectedSourceZone.ElementAt(index)).Selected = (((VideoItem)selectedSourceZone.ElementAt(index)).Selected == 0.0) ? 1.0 : 0.0; 
      } 

      var g = ((Button)sender).FindName("GridSelected"); 

      if (((VideoItem)selectedSourceZone.ElementAt(index)).Selected == 1.0) 
      { 
       ((Grid)g).Visibility = System.Windows.Visibility.Visible; 
      } 
      else 
      { 
       ((Grid)g).Visibility = System.Windows.Visibility.Collapsed; 
      } 
     } 
    } 

Ok весь код теперь здесь

Единственное, что я Ждут» т пост является fucntion, когда я нажимаю выбор подтверждения .. что все это делает добавить идентификатор массовой информации в список, как:.

selectedSourceMedias.Add(((PictureItemWeb)item).idMedia.ToString()); 

затем закрывает всплывающее окно и устанавливает longlistselector обнулить

Итак, когда я нажимаю кнопку, чтобы снова отобразить список. он воссоздает весь список! как показано в первом коде, который я опубликовал (он запрашивает медианы, снова устанавливает longlistselector и воссоздает элементы Longlist (какие PictureWeb и VideoItem оба IDisposable).

Наконец-то! Это то, что заставляет меня сходить с ума. Я отлаживаю и настраиваю, и он должен работать отлично !!! Но вот ситуация:

  1. В первый раз, когда все они не выбраны, это все.( ValueConverter включается здесь в первый раз)
  2. Тогда, если я закрывать и открывать снова они воссозданы и до сих пор все они невыбранные (GridSelected все Collapsed) (но теперь ValueConverter не активирован)
  3. Теперь я выбираю первые 3 медиа, элемент 3 сетки 3 элемента интерфейса, который я нажал, изменяется на Видимый, а затем подтверждаю выбор. (Я отлаживал, что правильные идентификаторы были сохранены на выбранных источниках источников)
  4. Затем я нажимаю, чтобы снова открыть список. (все создается снова и прямо перед // * 1 Я устанавливаю ItemsSource только что созданному выбранномуSourceZone, а ValueConverted не вызывается)
  5. после // * 1 Я вручную установил выбранные и UpdateSelectedStatus() в каждый из них (это также при отладке нужного носителя, который был выбран (от первого до третьего), проходит через линию ((Grid) g) .Visibility = System.Windows.Visibility.Visible; и все остальные, которые не были на selectedSourceMedias ID-лист проходит через свернутую строку ...
  6. СЕЙЧАС, когда весь код запускается и возвращается в эмулятор, 3 фотографии сверху (те, которые должны были видеть видимые GridSelected, свертываются и 3 фотографии съедают конец списка отображается как выбранный !!! (это ошибка, которая сводит меня с ума)
  7. И есть еще !!! Если я нажму на другую кнопку, на которой загружаются совершенно новые медийные средства (ValueConverted активируется, и все они проходят через свернутый (что хорошо)), он показывает эмулятор, а верхние 3 фотографии отображаются как выбранные !!!

Так что я думаю, что где-то есть внутренняя ошибка, которая хранит шаблоны данных, которые имеют такое же имя («GridSelected»)

Я знаю, что это, вероятно, даст вам немного головной боли, но .. пожалуйста попытайтесь помочь мне!

Сообщите мне, если у вас была подобная проблема и как вы ее исправили.

Спасибо вам все заранее!

ответ

1

Ну, я нашел решение своей проблемы ... Если кто-то из вас справится с этой проблемой ... Имейте в виду, что есть ошибка где-то в рамках, которая не очищает его кеш или что-то еще ... решение было для меня, чтобы добавить эти строки как раз перед я установить SourceZoneSelect.ItemsSource:

SourceZoneSelect.ClearValue(LongListSelector.ItemTemplateProperty); 
SourceZoneSelect.ItemTemplate = null; 
SourceZoneSelect.ItemTemplate = this.Resources["DataTemplateContentZone"] as DataTemplate; 

Так что, если вы хотите запустить конвертер каждый раз, вам необходимо вручную очистить шаблон и применить его снова