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 8c498a0 commit f04035a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
12 changes: 2 additions & 10 deletions demo/UnoDemo/IpcUno/IpcUno/Presentation/ChatPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,18 @@ public ChatPage(ConnectedPeerModel model, string serverName)

private void ChatPage_Loaded(object sender, RoutedEventArgs e)
{
ScrollToBottom(MessageListView);
MessageListView.ScrollToBottom();

// 有消息过来,自动滚动到最下
Model.MessageList.CollectionChanged += (o, args) =>
{
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
ScrollToBottom(MessageListView);
MessageListView.ScrollToBottom();
});
};
}

private void ScrollToBottom(ListView listView)
{
if (listView.VisualDescendant<ScrollViewer>() is { } scrollViewer)
{
scrollViewer.ChangeView(0.0f, scrollViewer.ExtentHeight, 1.0f, true);
}
}

public string ServerName { get; }

public ConnectedPeerModel Model { get; }
Expand Down
11 changes: 1 addition & 10 deletions demo/UnoDemo/IpcUno/IpcUno/Presentation/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,10 @@ private void ViewModel_AddedLogMessage(object? sender, string message)
// 延迟一下,防止界面还没刷新就执行滚动
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
{
ScrollToBottom(LogTextBox);
LogTextBox.ScrollToBottom();
});
}

private void ScrollToBottom(TextBox textBox)
{
//textBox.Spy();
if(textBox.VisualDescendant<ScrollViewer>() is { } scrollViewer)
{
scrollViewer.ChangeView(0.0f, scrollViewer.ExtentHeight, 1.0f, true);
}
}

public MainViewModel ViewModel => (MainViewModel) DataContext;

private void ConnectedPeerListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
Expand Down
23 changes: 23 additions & 0 deletions demo/UnoDemo/IpcUno/IpcUno/Utils/ScrollViewerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace IpcUno.Utils
{
static class ScrollViewerExtensions
{
public static void ScrollToBottom(this TextBox textBox)
{
ScrollToBottomInner(textBox);
}

public static void ScrollToBottom(this ListView listView)
{
ScrollToBottomInner(listView);
}

private static void ScrollToBottomInner(UIElement element)
{
if (element.VisualDescendant<ScrollViewer>() is { } scrollViewer)
{
scrollViewer.ChangeView(0.0f, scrollViewer.ExtentHeight, 1.0f, true);
}
}
}
}

0 comments on commit f04035a

Please sign in to comment.