【问题标题】:How to auto scroll down ListView in Xamarin.forms (MVVM design pattern)如何在 Xamarin.forms 中自动向下滚动 ListView(MVVM 设计模式)
【发布时间】:2017-08-23 19:34:01
【问题描述】:

我使用 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 属性。

那么这个问题有什么解决办法呢?

谢谢!

【问题讨论】:

    标签: listview mvvm xamarin.forms


    【解决方案1】:

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

    从您的 PageModel 发送一个信号,即

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

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

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

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

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

    然后像这样检索值:

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

    【讨论】:

    • 谢谢,但我需要放什么而不是“????” MainScreenMessagesListView.ScrollTo(?????, ScrollToPosition.End, true);
    • 谢谢!它不工作..我在调试模式下,我把断点放在这一行: MainScreenMessagesListView.ScrollTo(...) 而且应用程序不会停在那里。我在 viewModel 构造函数的末尾发送信号
    • 算了..我从构造函数中删除了信号的发送。我把它放在我的 onMessage 函数中。我得到了这个异常: System.Reflection.TargetInvocationException 我正在处理它。如果你有一个想法,我会非常高兴
    • InitializeComponent(); BindingContext = new ViewModels.ChatViewModel(Navigation); MessagingCenter.Subscribe(this, "MessageReceived", (sender, arg) => { try { MainScreenMessagesListView.ScrollTo(arg, ScrollToPosition.End, true); // arg == 0 } catch (Exception exp) { int size = MainScreenMessagesListView.ItemsSource.Cast().Count(); // size == 1 } }); OutOfRange 异常 - 为什么?
    • 请注意我在示例代码中是如何使用 -1 的。计数为您提供对象总数,ListView 将从 0 开始计数。
    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多