TestScrollable.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var line = new FormattedText(
  56. "Item " + (i + 1),
  57. TextBlock.GetFontFamily(this),
  58. TextBlock.GetFontSize(this),
  59. Size.Infinity,
  60. TextBlock.GetFontStyle(this),
  61. TextAlignment.Left,
  62. TextBlock.GetFontWeight(this));
  63. context.DrawText(Brushes.Black, new Point(-_offset.X, y), line);
  64. y += _lineSize.Height;
  65. }
  66. }
  67. public bool BringIntoView(IControl target, Rect targetRect)
  68. {
  69. throw new NotImplementedException();
  70. }
  71. public IControl GetControlInDirection(NavigationDirection direction, IControl from)
  72. {
  73. throw new NotImplementedException();
  74. }
  75. protected override Size MeasureOverride(Size availableSize)
  76. {
  77. var line = new FormattedText(
  78. "Item 100",
  79. TextBlock.GetFontFamily(this),
  80. TextBlock.GetFontSize(this),
  81. Size.Infinity,
  82. TextBlock.GetFontStyle(this),
  83. TextAlignment.Left,
  84. TextBlock.GetFontWeight(this));
  85. line.Constraint = availableSize;
  86. _lineSize = line.Measure();
  87. return new Size(_lineSize.Width, _lineSize.Height * itemCount);
  88. }
  89. protected override Size ArrangeOverride(Size finalSize)
  90. {
  91. _viewport = new Size(finalSize.Width, finalSize.Height / _lineSize.Height);
  92. _extent = new Size(_lineSize.Width, itemCount + 1);
  93. InvalidateScroll?.Invoke();
  94. return finalSize;
  95. }
  96. }
  97. }