TestScrollable.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Input;
  6. using Avalonia.Media;
  7. using Avalonia.VisualTree;
  8. namespace XamlTestApplication
  9. {
  10. public class TestScrollable : Control, ILogicalScrollable
  11. {
  12. private int itemCount = 100;
  13. private Size _extent;
  14. private Vector _offset;
  15. private Size _viewport;
  16. private Size _lineSize;
  17. public bool IsLogicalScrollEnabled => true;
  18. public Action InvalidateScroll { get; set; }
  19. Size IScrollable.Extent
  20. {
  21. get { return _extent; }
  22. }
  23. Vector IScrollable.Offset
  24. {
  25. get { return _offset; }
  26. set
  27. {
  28. _offset = value;
  29. InvalidateVisual();
  30. }
  31. }
  32. Size IScrollable.Viewport
  33. {
  34. get { return _viewport; }
  35. }
  36. public Size ScrollSize
  37. {
  38. get
  39. {
  40. return new Size(double.PositiveInfinity, 1);
  41. }
  42. }
  43. public Size PageScrollSize
  44. {
  45. get
  46. {
  47. return new Size(double.PositiveInfinity, Bounds.Height);
  48. }
  49. }
  50. public override void Render(DrawingContext context)
  51. {
  52. var y = 0.0;
  53. for (var i = (int)_offset.Y; i < itemCount; ++i)
  54. {
  55. using (var line = new FormattedText(
  56. "Item " + (i + 1),
  57. TextBlock.GetFontFamily(this),
  58. TextBlock.GetFontSize(this),
  59. TextBlock.GetFontStyle(this),
  60. TextAlignment.Left,
  61. TextBlock.GetFontWeight(this)))
  62. {
  63. context.DrawText(Brushes.Black, new Point(-_offset.X, y), line);
  64. y += _lineSize.Height;
  65. }
  66. }
  67. }
  68. public bool BringIntoView(IControl target, Rect targetRect)
  69. {
  70. throw new NotImplementedException();
  71. }
  72. public IControl GetControlInDirection(FocusNavigationDirection direction, IControl from)
  73. {
  74. throw new NotImplementedException();
  75. }
  76. protected override Size MeasureOverride(Size availableSize)
  77. {
  78. using (var line = new FormattedText(
  79. "Item 100",
  80. TextBlock.GetFontFamily(this),
  81. TextBlock.GetFontSize(this),
  82. TextBlock.GetFontStyle(this),
  83. TextAlignment.Left,
  84. TextBlock.GetFontWeight(this)))
  85. {
  86. line.Constraint = availableSize;
  87. _lineSize = line.Measure();
  88. return new Size(_lineSize.Width, _lineSize.Height * itemCount);
  89. }
  90. }
  91. protected override Size ArrangeOverride(Size finalSize)
  92. {
  93. _viewport = new Size(finalSize.Width, finalSize.Height / _lineSize.Height);
  94. _extent = new Size(_lineSize.Width, itemCount + 1);
  95. InvalidateScroll?.Invoke();
  96. return finalSize;
  97. }
  98. }
  99. }