123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model.PageEdit;
- using Prism.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace PDF_Office.Views.PageEdit
- {
- /// <summary>
- /// PageEditContent.xaml 的交互逻辑
- /// </summary>
- public partial class PageEditContent : UserControl
- {
- /// <summary>
- /// 用于通知刷新图片的事件
- /// 因为目前的图片刷新策略依赖很多UI事件判断,因为将主要判断逻辑放在UI层,VM负责图片刷新,非必要情况尽量不要采用这种形式
- /// </summary>
- private IEventAggregator eventor;
- /// <summary>
- /// 暂存的唯一索引值,用于区分多页签
- /// </summary>
- private string unicode;
- /// <summary>
- /// 是否是加载时第一次触发滚动
- /// </summary>
- private bool isFirstScrollChange = true;
- /// <summary>
- /// 用于判断滚轮停止的计时器
- /// </summary>
- private DispatcherTimer timer = new DispatcherTimer();
- /// <summary>
- /// 记录当前滑块的状态
- /// </summary>
- private ScrollEventType scrolltype = ScrollEventType.EndScroll;
- public PageEditContent()
- {
- InitializeComponent();
- }
- public PageEditContent(IEventAggregator eventAggregator) :this()
- {
- eventor = eventAggregator;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- timer.Interval = TimeSpan.FromSeconds(0.3);
- timer.Tick += Timer_Tick;
- //订阅页面刷新事件
- eventor.GetEvent<PageEditNotifyEvent>().Subscribe(OneNotifyEvent, e => e.Unicode == unicode);
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- PulishEvent();
- timer.Stop();
- }
- private void OneNotifyEvent(PageEditNotifyEventArgs e)
- {
- PulishEvent();
- }
- #region UI事件
- /// <summary>
- /// 每次显示的时候就触发事件,刷新所有图片
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void PageEdit_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if((bool)e.NewValue)
- {
- //当前页面没有发生变化时,刷新图片 这种情况下会拿两次图,需要留意
- PulishEvent();
- //当前页面发生变化时通过ScrollChanged事件来刷新图片
- isFirstScrollChange = true;
- }
- }
- /// <summary>
- /// 鼠标滚轮滚动时触发的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListPageEdit_ScrollChanged(object sender, ScrollChangedEventArgs e)
- {
- if (isFirstScrollChange)
- {
- PulishEvent();//第一次加载时触发的Scollchange 直接刷新界面,减少白板显示时间
- isFirstScrollChange = false;
- return;
- }
- else
- {
- if (e.VerticalChange != 0)
- timer.Start();//暂时找不到比较好的 判断Scroller停止的方法,先用计时器粗略判断
- }
- }
- /// <summary>
- /// 拖动右侧滑块时触发的事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListPageEdit_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
- {
- scrolltype = e.ScrollEventType;
- if (scrolltype != System.Windows.Controls.Primitives.ScrollEventType.EndScroll)
- {
- timer.Stop();
- return;
- }
- PulishEvent();
- }
- #endregion
- #region 方法
- /// <summary>
- /// 发布事件
- /// </summary>
- private void PulishEvent()
- {
- var itemSize = (ListPageEdit.Items[0] as PageEditItem).ItemSize;
- var range = GetRoughFromView(ListPageEdit, itemSize, new Thickness(5, 10, 5, 10));
- eventor.GetEvent<PageEditRefreshEvent>().Publish(new PageEditRefreshEventArgs() { Unicode = unicode, PageRange = range });
- }
- /// <summary>
- /// 获取滑轨的垂直偏移量,结合item总数和Item尺寸以及间隔,来估算实际展示的item范围
- /// 返回值为页面范围 从1开始
- /// </summary>
- /// <param name="view"></param>
- /// <param name="itemSize"></param>
- /// <param name="itemMargin"></param>
- /// <returns></returns>
- private Tuple<int, int, int> GetRoughFromView(ListBox view, Size itemSize, Thickness itemMargin)
- {
- //var scrollViewer = GetScrollHost(view);
- var scrollViewer = CommonHelper.FindVisualChild<ScrollViewer>(view);
- if (scrollViewer == null || scrollViewer.ActualHeight == 0 || scrollViewer.ActualWidth == 0)//视图展开
- return new Tuple<int, int, int>(0, 0, 0);
- try
- {
- var currentHeight = scrollViewer.ActualHeight - view.Padding.Top;
- var currentWidth = scrollViewer.ActualWidth;
- //计算当前窗口大小能显示的行数和列数
- var columnCount = (int)(currentWidth / (itemSize.Width + itemMargin.Left));
- var rowCount = (int)Math.Ceiling(currentHeight / (itemSize.Height + itemMargin.Bottom));
- var preItemCount = (int)((scrollViewer.VerticalOffset / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));//滑动百分比*行数 = 大概的垂直位置
- preItemCount = preItemCount * columnCount;
- var preEnd = (int)(((scrollViewer.VerticalOffset + scrollViewer.ActualHeight) / scrollViewer.ExtentHeight) * ((view.Items.Count + columnCount - 1) / columnCount));
- preEnd = preEnd * columnCount + columnCount - 1;
- var middle = (int)Math.Ceiling(preItemCount + preEnd / 2d);
- return new Tuple<int, int, int>(
- Math.Max(preItemCount, 0),
- Math.Min(view.Items.Count, preEnd),
- middle);
- }
- catch { }
- return new Tuple<int, int, int>(0, 0, 0);
- }
- /// <summary>
- /// 获取listbox里的ScrollViewer对象
- /// 留意如果有重写ListBox对象,该方法可能无效
- /// </summary>
- /// <param name="listBox"></param>
- /// <returns></returns>
- private ScrollViewer GetScrollHost(ListBox listBox)
- {
- if (VisualTreeHelper.GetChildrenCount(listBox) > 0)
- {
- int s = VisualTreeHelper.GetChildrenCount(listBox);
- Border border = VisualTreeHelper.GetChild(listBox, 0) as Border;
- if (border != null)
- {
- return VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
- }
- }
- return null;
- }
- #endregion
- /// <summary>
- /// 输入框显示时,自动获取焦点
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void TextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- if((bool)e.NewValue)
- {
- (sender as Control).Focus();
- }
- }
- }
- }
|