【问题标题】:Maui ScrollView - Scrollbar length not updating when content dynamically changedMaui ScrollView - 内容动态更改时滚动条长度不更新
【发布时间】:2022-10-18 03:56:50
【问题描述】:

我用双向模式尝试了this content
我使用了 ObservableCollection,当我修改内容时更新了,但是滚动长度没有更新。

右滚动条太长。
如何更新滚动条长度?

<ScrollView BackgroundColor="LightGray" x:Name="InfoStack">
        <StackLayout
            BindableLayout.ItemsSource="{Binding Source={x:Static local:InfoHelpers.Ints}, Mode=TwoWay}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <.../>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>

【问题讨论】:

    标签: c# maui


    【解决方案1】:

    更改内容后,这可能修理它:

    (InfoStack as IView).InvalidateArrange();
    

    根据评论更新

    也试试:

    (InfoStack as IView).InvalidateMeasure();
    

    HACK:如果这不起作用,请尝试稍微延迟:

    Dispatcher.Dispatch(async() =>
    {
        await Task.Delay(100);
        (InfoStack as IView).InvalidateArrange();
    }
    

    (虽然涉及时间延迟的黑客攻击不可靠,但如果第一个不起作用,但确实如此,这可能有助于某人识别“真正的”问题。)

    【讨论】:

    • 我不得不使用InvalidateMeasure,但它起作用了。
    • 很高兴知道。这本身是否有效,还是需要同时测量/安排(如果是,按什么顺序)?
    【解决方案2】:

    它在不同平台上的行为不同。

    我确实在 Windows desktopiOSAndroid 模拟器上测试了您的代码。

    结果是它只适用于视窗桌面, ScrollView 不会像预期的那样在 iOS 和 Android 上动态扩展。

    这似乎是一个 potential issue ,考虑在 github 上解决这个问题:https://github.com/dotnet/maui/issues

    解决方法

    使用CollectionView 而不是ScrollView

    <CollectionView ItemsSource="{Binding Source={x:Static local:InfoHelpers.Ints}, Mode=TwoWay}">
       <CollectionView.ItemTemplate>
          <DataTemplate>      
              <Label Text="{Binding .}" HeightRequest="40"/>             
          </DataTemplate>
       </CollectionView.ItemTemplate>
    </CollectionView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 2023-01-25
      • 2014-07-20
      • 2011-12-31
      • 1970-01-01
      • 2018-11-21
      相关资源
      最近更新 更多