【问题标题】:How to filter an observable collection in Windows Phone 8?如何过滤 Windows Phone 8 中的可观察集合?
【发布时间】:2014-01-20 12:10:57
【问题描述】:

我尝试了这个How to: Filter Data in a View,所以我尝试了这种方法,但它对我不起作用,因为 windows phone 8 环境找不到“ListCollectionView”。

我的收藏正在关注。

//My observableCollection
ObservableCollection<ClonedWrapper> dsForProgress = new ObservableCollection<ClonedWrapper >();

//My Function which shows at first time.
private async Task CloudImages()
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
try
{
    List<MainWrapper> serverdata = new List<MainWrappe>();
        serverdata = await ImageUrlAsync();//This function is returning the Deserialize object of images
        if (serverdata.Count != 0)
        {
            foreach (var imgInfo in serverdata)
            {
                string folderPath = "Fagbokforlaget/Books/" + BookInfo.Id.Trim();
                ClonedWrapper item = new ClonedWrapper()
                {
                    //Name, Cover, Info, Title, IsEnableButton, IsVisibleButton, 
                    // IsVisible are the assigned keys to the xaml page
                    Id = imgInfo.Id,
                    Name = imgInfo.Name,
                    Cover = await GetCoverImage(imgInfo),
                    Info = imgInfo.Info,
                    Title = imgInfo.Title,
                    Url = imgInfo.Url,
                    IsEnableButton = "True",
                    IsVisible = "Collapsed",
                    Date = Convert.ToDateTime(imgInfo.Date)
                };
                if (isf.DirectoryExists(folderPath))
                {
                    item.ButtonStatus = "Read";
                        item.IsVisibleBookDeleteButton = "Visible";
                    }
                    else
                    {
                        item.ButtonStatus = "Download";
                        item.IsVisibleBookDeleteButton = "Collapsed";
                    }
                    dsForProgress.Add(item);
                }
            }
            else
            {
                MessageBox.Show("You have no any downloaded books!");
            }
            listCloudImages.ItemsSource = dsForProgress;
    }
        catch (Exception exe)//I am getting exception here on extreme first run in absense of internet.
        {
            MessageBox.Show(exe.Message);
        }
    }

//我的xaml页面如下。它是第一次加载的默认页面。你可以说它是应用程序的面孔。用户下载图像。和 buttonStatus 转换为 Read 按钮。所以现在我在做什么我将在应用程序栏中保留两个按钮,一秒下载云。 ON 下载按钮单击我只想显示下载的图像。

   <ListBox Name="listCloudImages" Visibility="Visible" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Auto" FontFamily="Segoe UI" FontStyle="Normal" FontWeight="Thin" Margin="0,0,0,50">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <tools:WrapPanel Orientation="Horizontal">
                </tools:WrapPanel>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="1" Background="#151414" CornerRadius="3" Margin="3" Width="150" TextOptions.DisplayColorEmoji="True" BorderBrush="#1c1b1b">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">

                            <Image x:Name="imgBookImage" Source="{Binding Cover}" Visibility="Visible" VerticalAlignment="Top" HorizontalAlignment="Center"
                                       Width="80" Height="100" ImageOpened="imgBookImage_ImageOpened"/>
                            <StackPanel Orientation="Horizontal">

                                <TextBlock Visibility="{Binding IsVisible}" Text="{Binding ProgressPercentage}" FontFamily="Segoe UI" FontSize="18" FontWeight="ExtraBold" Foreground="White" Margin="5,0"></TextBlock>
                            </StackPanel>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}" FontFamily="Segoe UI" FontSize="13.5" 
                                       Foreground="White" TextTrimming="WordEllipsis"
                                       VerticalAlignment="Top" HorizontalAlignment="Left" 
                                       TextWrapping="Wrap" 
                                       Width="300" Padding="2"/>
                            <TextBlock Text="{Binding Info}" FontSize="13.5" FontFamily="Segoe UI" 
                                       Foreground="White"
                                       VerticalAlignment="Top" HorizontalAlignment="Left" 
                                       TextWrapping="Wrap" 
                                       Width="300" Padding="2"/>
                            <ProgressBar x:Name="downloadProgressBar" Foreground="Green" IsIndeterminate="True" VerticalAlignment="Center"  Width="120" TextOptions.TextHintingMode="Animated" Visibility="{Binding IsVisible}"  CharacterSpacing="2"/>
                            <Button Content="{Binding ButtonStatus}" x:Name="btnDownload" IsEnabled="{Binding IsEnableButton, Converter={StaticResource ButtonVisibilityIsEnableConverter}}" 
                                       Click="btnDownload_Click" Tag="{Binding}" Width="120" BorderThickness="1" FontSize="13.5" Margin="0,5" 
                                       FontFamily="Segoe UI" tools:SlideInEffect.LineIndex="2" HorizontalAlignment="Left" VerticalAlignment="Top" 
                                       Foreground="White">
                            </Button>

                            <Image x:Name="imgCancelImage" Source="/Assets/Tiles/CancelImage.png" Visibility="{Binding IsVisible}" VerticalAlignment="Center" Margin="97,-66,0,0"
                                         Tap="imgCancelImage_Tap"  HorizontalAlignment="Right" Width="25" Height="25" Tag="{Binding}"/>
                            <Button x:Name="btnDeleteImage" Click="btnDeleteImage_Click"
                                        Tag="{Binding}" BorderThickness="1" Margin="97,-66,0,0" 
                                        Height="55" Width="55"
                                        Visibility="{Binding ButtonStatus, Converter={StaticResource DeleteButtonVisibilityConverter}}">
                                <Button.Background>
                                    <ImageBrush ImageSource="/Images/delete.png" Stretch="Fill"></ImageBrush>
                                </Button.Background>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

  • 至少,给我们看看你的收藏
  • @SeeSharp 我现在已经展示了我的收藏。请帮忙。

标签: windows-phone-8 filter listbox collectionview listcollectionview


【解决方案1】:

根据你的代码,你可以做一些很简单的事情,如果你想只显示下载的书试试这个:

private void ApplicationBarButtonShowAllDownLoaded(object sender, EventArgs e)
    {        
        var c = (Application.Current as App).dsForProgress.Where(x => x.ButtonStatus.Equals("Download"));
        listCloudImages.ItemsSource = c;
    }

确保ObservableCollection&lt;ClonedWrapper&gt; dsForProgress 是公开的

【讨论】:

  • 谢谢你,亲爱的,我想再说一件事,你们是如何解决这些问题的。你有伟大的头脑。 :) 我希望上帝保佑我有这种想法...... :) 无论如何,再次感谢你。
【解决方案2】:

在您的图书模型中,您可以传递一个布尔属性 IsDownload,当您下载图书时将此属性设置为 true。对于您的过滤器,只需使用 linQ 下载所有图书或不下载,如下所示:

var noDownloadedBooks = 
            from b in yourObservableCollection
            where b.IsDownload == false 
            select b; 
var downloadedBooks = 
                from b in yourObservableCollection
                where b.IsDownload == true
                select b; 

【讨论】:

    【解决方案3】:

    你需要做的是使用一个 CollectionViewSource,它可以指向你的原始集合,它可以处理过滤! Scott Hanselman 在这里有一篇很好的示例博客文章...http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx

    【讨论】:

    • 我无法创建 //Following line 已经写在我的代码中。 xmlns:d="schemas.microsoft.com/expression/blend/2008" //需要写入以下行,但它继续显示 //错误,这意味着在路径中找不到我的 HomePage.xaml。虽然//我的路径是正确的。 d:DataContext="{d:DesignData MyWpPhone/HomePage.xaml}"
    • 仅凭此评论很难检查错误!但是您可以检查以下运行示例 code.msdn.microsoft.com/wpapps/… 也许您可以根据需要对其进行调整
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多