From 228d2534866ee6d6d8b002f2476edb2982202eb2 Mon Sep 17 00:00:00 2001 From: lindexi Date: Fri, 24 Nov 2023 16:31:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B6=88=E6=81=AF=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=BB=9A=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IpcUno/Presentation/ChatPage.xaml.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs b/demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs index 7cc15b6..be4e0ad 100644 --- a/demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs +++ b/demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs @@ -1,21 +1,42 @@ using System.Reflection; using System.Text; +using IpcUno.Utils; + namespace IpcUno.Presentation { public sealed partial class ChatPage : Page { public ChatPage(ConnectedPeerModel model, string serverName) { - DataContextChanged += ChatPage_DataContextChanged; DataContext = model; this.InitializeComponent(); Model = model; ServerName = serverName; + + Loaded += ChatPage_Loaded; + } + + private void ChatPage_Loaded(object sender, RoutedEventArgs e) + { + ScrollToBottom(MessageListView); + + // 有消息过来,自动滚动到最下 + Model.MessageList.CollectionChanged += (o, args) => + { + _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => + { + ScrollToBottom(MessageListView); + }); + }; } - private void ChatPage_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) + private void ScrollToBottom(ListView listView) { + if (listView.VisualDescendant() is { } scrollViewer) + { + scrollViewer.ChangeView(0.0f, scrollViewer.ExtentHeight, 1.0f, true); + } } public string ServerName { get; }