0

我的 uwp 社区工具包的adaptivegridview 出现以下异常

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

XAML

<controls:AdaptiveGridView Name="AllVideosGridView" 
                                           OneRowModeEnabled="True"
                                           MaxHeight="260"
                                           ScrollViewer.HorizontalScrollMode="Enabled"
                                           ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                           ItemClick="AllVideosGridView_ItemClick"
                                           Style="{StaticResource MainGridView}"
    <...data template and other stuff...>
</controls.........>

该错误是由于属性 OneRowModeEnabled 设置为 True 而发生的,如果我删除该属性它工作正常,并且在应用程序运行后我将此属性设置为 true,当应用程序运行时它不会显示任何异常并且 gridview 转到一行应有的模式。

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

4

1 回答 1

1

这里的问题是您没有ItemHeightAdaptiveGridView.

形成源代码AdaptiveGridView,可以找到如下代码(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时,它将绑定MaxHeightPropertyItemHeight属性。但是,由于您没有为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>
于 2017-06-08T10:55:15.733 回答