Skip to content

Commit

Permalink
修复消息没有滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Nov 24, 2023
1 parent 797bb6b commit 228d253
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -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<ScrollViewer>() is { } scrollViewer)
{
scrollViewer.ChangeView(0.0f, scrollViewer.ExtentHeight, 1.0f, true);
}
}

public string ServerName { get; }
Expand Down

0 comments on commit 228d253

Please sign in to comment.