uwp adaptiveGridView 在 OneRowMode 上抛出异常

uwp adaptiveGridView throws exception on OneRowMode

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

"System.ArgumentException: Value does not fall within the expected range. \r\n at Windows.UI.Xaml.FrameworkElement.SetBinding(DependencyProperty dp, BindingBase binding)\r\n at Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.DetermineOneRowMode()\r\n at 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后面的代码,但仍然出现这个异常。

这里的问题是你没有在AdaptiveGridView中设置ItemHeight属性。

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>