如何在Xamarin.forms中自动向下滚动ListView(MVVM设计模式)

马坦·马恰诺(Matan Marciano)

我使用Xamarin.Forms(MVVM设计模式)开发了一个聊天应用程序。发送消息后,我需要自动向下滚动ListView(聊天消息列表)。

我的看法:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         ...>


<ContentPage.Content>
    <StackLayout Style="{StaticResource MainLayoutStyle}">

        ...

        <Frame
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand"
            CornerRadius="5"
            BackgroundColor="White"
            Padding="5">
            <ScrollView>
                <StackLayout>

                    <ListView
                        x:Name="MainScreenMessagesListView"
                        ItemTemplate="{StaticResource MessageTemplateSelector}"
                        HasUnevenRows="True"
                        BackgroundColor="#e5ddd5"
                        ItemsSource="{Binding Messages}">
                    </ListView>

                </StackLayout>
            </ScrollView>
        </Frame>

        ...

    </StackLayout>
</ContentPage.Content>

由于我的设计模式,我不能使用ScrollTo方法(对吗?),并且xaml中没有ScrollTo属性。

那么该问题的解决方案是什么?

谢谢!

杰拉尔德·韦斯卢瓦

解决此问题的一种方法是使用MessagingCenter

从您的PageModel发送信号,即

MessagingCenter.Send<object> (this, "MessageReceived");

然后,在页面的代码隐藏中,您可以订阅它并向下滚动或执行任何操作。

MessagingCenter.Subscribe<object> (this, "MessageReceived", (sender) => {
    MainScreenMessagesListView.ScrollTo(..., ScrollToPosition.End, true);
});

您必须将最后一项确定为对象中的对象,而不是点ListView您可以通过两种方式执行此操作,或者通过投射的ItemsSource属性在页面中确定它ListView但是,最好将其作为参数与MessagingCenter调用一起提供。

在您的PageModel中,您可以将其更改为: MessagingCenter.Send<object, object> (this, "MessageReceived", lastReceivedMessage);

并像这样检索值:

MessagingCenter.Subscribe<object, object> (this, "MessageReceived", (sender, arg) => {
    MainScreenMessagesListView.ScrollTo(arg, ScrollToPosition.End, true);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在div中自动向下滚动?

如何在我的页面中自动向下滚动?

如何在div中自动向下滚动添加的内容?

如何自动向下滚动

如何自动向下滚动div

用户向上滚动时如何停止自动向下滚动?

如何使WPF文本框自动向下滚动

追加后如何自动向下滚动JTextArea?

如何在Android中自动向上滚动Recycler视图?

如何在 Xamarin Forms 中使用 MVVM 在 ListView 中实现增量加载(ItemAppearing)项目?

如何在 Xamarin 中过滤

如何在Xamarin.Forms中基于ListView滚动方向显示和隐藏StackLayout?

如何在Xamarin.Forms中强制使用灯光模式?

如何在Xamarin.Forms中检查暗模式

如何在Xamarin.Forms中的ListView内绑定列表

如何在xamarin.forms中获得ListView的子级?

Xamarin Forms-如何在ListView中显示/绑定列表?

Xamarin Forms - 如何在 xaml 中创建水平 ListView?

如何在 Xamarin 中创建无限的 ListView?

添加文本后,如何使我的滚动视图自动向下滚动?

使用ItemAppearing事件在ListView中向下滚动会跳过Xamarin.Forms中的记录

如何阻止此CSS手风琴自动向下滚动?

如何在 MVVM Xamarin Forms 中将参数传递给 Singleton

如何在 Xamarin Forms (MVVM) 中键入时删除空格

如何在Xamarin Forms中将BackgroundImage添加到ListView?

如何在 Xamarin Forms 中的 CarouselView 中滚动后检测滚动完成或结束?

如何在 Xamarin 中创建页面?

如何在Xamarin iOS中绘制文本?

如何在 Xamarin 中创建 ResourceDictionary?