uwp adaptiveGridView在OneRowMode上抛出异常

时间:2017-06-03 12:21:19

标签: c# xaml binding uwp windows-community-toolkit

我在uwp社区工具包的自适应网格视图中遇到以下异常

  

" System.ArgumentException:值不在预期范围内。    \ r \ n位于Microsoft.Toolkit的Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.DetermineOneRowMode()\ r \ n的Windows.UI.Xaml.FrameworkElement.SetBinding(DependencyProperty dp,BindingBase绑定)\ r \ n。 Uwp.UI.Controls.AdaptiveGridView.OnLoaded(Object sender,RoutedEventArgs e)"

XAML

let userProfilePicture: UIImageView = {

    let statusBarHeight = UIApplication.shared.statusBarFrame.height

    let profileImageFrame = CGRect(x: statusBarHeight / 2, y: statusBarHeight, width: 50, height: 50)
    let profileImageView = UIImageView(frame: profileImageFrame)

    //set backgroundcolor (placeholder as sort of a place holder)
    profileImageView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)

    let imageUrl = NSURL(string: "https://like.to.myImageWith50pixels.jpg")!

    let cache = MemoryCacheLevel().compose(DiskCacheLevel()).compose(NetworkFetcher())

    cache.get((imageUrl as NetworkFetcher.KeyType) as NetworkFetcher.KeyType)
        .onSuccess { value in
            let profileImage = UIImage(data: value as Data)


            profileImageView.image = profileImage
        }
        .onFailure { error in
            print("An error occurred :( \(error)")
    }

    profileImageView.translatesAutoresizingMaskIntoConstraints = false

    //round picture
    profileImageView.layer.borderWidth = 1
    profileImageView.layer.masksToBounds = false
    profileImageView.layer.borderColor = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 0.75).cgColor
    profileImageView.layer.cornerRadius = profileImageView.frame.height/2
    profileImageView.clipsToBounds = true

    return profileImageView

}()

由于属性OneRowModeEnabled为True而发生错误,并且如果我删除该属性它工作正常并且在应用程序运行之后我将此属性设置为true,而app正在运行,那么它不会显示任何异常, gridview按原样进入一行模式。

后面的代码也没关系,因为我试图推荐初始化itemsource的代码,但是这个异常仍然会发生。

1 个答案:

答案 0 :(得分:1)

此处的问题是您未在ItemHeight中设置AdaptiveGridView属性。

形成AdaptiveGridView的源代码,您可以找到the following code (L155-L186)

private void OnLoaded(object sender, RoutedEventArgs e)
{
    _isLoaded = true;
    DetermineOneRowMode();
}

private void DetermineOneRowMode()
{
    if (_isLoaded)
    {
        var itemsWrapGridPanel = ItemsPanelRoot as ItemsWrapGrid;

        if (OneRowModeEnabled)
        {
            var b = new Binding()
            {
                Source = this,
                Path = new PropertyPath("ItemHeight")
            };

            if (itemsWrapGridPanel != null)
            {
                _savedOrientation = itemsWrapGridPanel.Orientation;
                itemsWrapGridPanel.Orientation = Orientation.Vertical;
            }

            SetBinding(MaxHeightProperty, b);
            ...

OneRowModeEnabled设置为True时,它会将MaxHeightProperty绑定到ItemHeight属性。但是,由于您未将值设置为ItemHeight属性,因此其值为NaN。这就是你遇到Value does not fall within the expected range.错误的原因。

要解决此问题,您可以设置ItemHeight属性,如下所示:

 <controls:AdaptiveGridView Name="AllVideosGridView"
                                   OneRowModeEnabled="True"
                                   DesiredWidth="260"
                                   ItemHeight="200"
                                   SelectionMode="None"
                                   StretchContentForSingleRow="False"
                                   IsItemClickEnabled="True"
                                   ItemsSource="{x:Bind ViewModel.Videos, Mode=OneWay}">
    ...
</controls:AdaptiveGridView>