GridView和ListView出错 [英] Error with GridView and ListView

查看:63
本文介绍了GridView和ListView出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为"书签"的用户控件。我填充到GridView(用于普通视图)和ListView(用于快照视图)。书签包含Textblocks和Image,其源是加载时创建的WriteableBitmap。

I have a user control of type "Bookmark" that I which to populate into a GridView (for normal view) and ListView (for snap view). A Bookmark contains Textblocks and an Image whose source is a WriteableBitmap created upon loading.

我在加载应用程序时加载这些书签,如下所示:

I load those bookmarks when the app is loaded as follows :

public void CreateThumbnails(List<BookmarkData> dataList)
{
    _bookmarkList = new List<Bookmark>();

    foreach (BookmarkData data in dataList)
        _bookmarkList.Add(new Bookmark(data));

    BookmarksGrid.ItemsSource = _bookmarkList;
    BookmarksList.ItemsSource = _bookmarkList;
}


一开始,只有两个(GridView / ListView)中的一个可见。当应用程序切换到Snap View或从Snap View切换时,我运行以下代码:

In the beginning, only one of the two (GridView/ListView) is visible. When the app is switched into or out of Snap View, I run the following code :

public void SnapStateChanged(bool snapped)
{
    if (snapped)
    {
        BookmarksGrid.Visibility = Visibility.Collapsed;
        BookmarksList.Visibility = Visibility.Visible;
    }
    else
    {
        BookmarksGrid.Visibility = Visibility.Visible;
        BookmarksList.Visibility = Visibility.Collapsed;
    }
}

然而,这样做(改变可见性)会给我一个"价值不会下降"在预期范围内"例外。有没有人知道为什么会发生这种情况?

However, doing so (changing visibilities) will give me a "Value does not fall within the expected range" exception. Does anyone have an idea why this occurs?

推荐答案

代码中究竟出现了什么异常?

Where exactly in the code does the Exception occur?

有一种更简单的方法来处理捕捉/未捕捉的视觉状态变化:

There's an easier way to handle the snapped/unsnapped visual state change:

<VisualStateManager.VisualStateGroups>        <!-- Visual states reflect the application's view state -->        <VisualStateGroup x:Name="ApplicationViewStates">            <VisualState x:Name="FullScreenLandscape"/>            <VisualState x:Name="Filled"/>            <VisualState x:Name="FullScreenPortrait"/>            <VisualState x:Name="Snapped">                <Storyboard>                    <!--switch the visibilities-->                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BookmarkGrid" Storyboard.TargetProperty="Visibility">                        <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>                    </ObjectAnimationUsingKeyFrames>                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BookmarkList" Storyboard.TargetProperty="Visibility">                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>                    </ObjectAnimationUsingKeyFrames>                </Storyboard>            </VisualState>        </VisualStateGroup></VisualStateManager.VisualStateGroups>

您只需将VisualStateManager放在页面根网格中的某个位置。

You just have to put the VisualStateManager somewhere in your pages root grid.

欢呼,

〜theCake


这篇关于GridView和ListView出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆