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; }