.net Xamarin形成像视频滚动一样的tiktok

g9icjywg  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(153)

我试图创建一个tiktok像视频滚动与xamarin形式.我设法创建一个视频列表和视频列表的数据模型显示,但不是全屏.
我的问题是:在我的xalm代码,我想只显示一个视频在全屏比如果用户的下一个或上一个视频也在全屏(如tiktok)
下面是我的xalm代码:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <ContentPage
  3. xmlns="http://xamarin.com/schemas/2014/forms"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:videoplayer="clr-namespace:Octane.Xamarin.Forms.VideoPlayer;assembly=Octane.Xamarin.Forms.VideoPlayer"
  5. x:Class="lf.MyPages.Lomy.Discover" x:Name="leafDiscoverPage">
  6. <Grid>
  7. <CollectionView x:Name="VideosCollectionView"
  8. ItemsLayout="VerticalList"
  9. RemainingItemsThreshold="1"
  10. Scrolled="OnScrolled"
  11. RemainingItemsThresholdReached="OnRemainingItemsThresholdReached">
  12. <CollectionView.ItemTemplate>
  13. <DataTemplate>
  14. <Grid HeightRequest="{Binding Source={x:Reference leafDiscoverPage}, Path=Height}"
  15. WidthRequest="{Binding Source={x:Reference leafDiscoverPage}, Path=Width}">
  16. <!-- Replace 'octane:VideoPlayer' with your actual video player control namespace -->
  17. <videoplayer:VideoPlayer Source="{Binding UserVideoBlobAdress}"
  18. AutoPlay="{Binding IsPlaying}"
  19. HorizontalOptions="FillAndExpand"
  20. VerticalOptions="FillAndExpand" />
  21. <!-- Add other UI elements as needed -->
  22. </Grid>
  23. </DataTemplate>
  24. </CollectionView.ItemTemplate>
  25. </CollectionView>
  26. </Grid>
  27. </ContentPage>

字符串
我的.cs代码:

  1. public partial class Discover : ContentPage
  2. {
  3. MyUser UserConnected;
  4. ObservableRangeCollection<UserVideoModel> MyListVideo;
  5. private int _videoCount = 0;
  6. public LeafDiscover ()
  7. {
  8. InitializeComponent ();
  9. MyListVideo = new ObservableRangeCollection<UserVideoModel>();
  10. // Initialize your ListVideo with initial video links
  11. UserConnected = (MyUser)App.GetProperties("UserConnected");
  12. OnInitVideoList();
  13. }
  14. public void UpdateVideoUrl(List<UserVideoModel> tmpuservideo)
  15. {
  16. foreach (var tmpvide in tmpuservideo)
  17. {
  18. tmpvide.UserVideoBlobAdress = "https://fo.blob.net/app/"+tmpvide.UserVideoBlobAdress;
  19. }
  20. }
  21. async void OnInitVideoList()
  22. {
  23. var MyInitVideo = await VideosManager.GetVideoAsync(UserConnected.MyId.ToString(), false, "");
  24. UpdateVideoUrl(MyInitVideo);
  25. foreach (var tmpvide in MyInitVideo)
  26. {
  27. Console.WriteLine("mook "+ tmpvide.UserVideoBlobAdress);
  28. }
  29. MyListVideo.AddRange(MyInitVideo);
  30. VideosCollectionView.ItemsSource = MyListVideo;
  31. }
  32. private void OnRemainingItemsThresholdReached(object sender, EventArgs e)
  33. {
  34. if (_videoCount >= 20)
  35. {
  36. // Load more videos or handle the end of the list
  37. _videoCount = 0;
  38. }
  39. }
  40. private void OnScrolled(object sender, ItemsViewScrolledEventArgs e)
  41. {
  42. // Determine the visible item and update playback
  43. var centerIndex = e.CenterItemIndex;
  44. UpdateVideoPlayback(centerIndex);
  45. }
  46. private void UpdateVideoPlayback(int centerIndex)
  47. {
  48. for (int i = 0; i < MyListVideo.Count; i++)
  49. {
  50. MyListVideo[i].IsPlaying = i == centerIndex;
  51. }
  52. }
  53. protected override void OnAppearing()
  54. {
  55. base.OnAppearing();
  56. // Optionally start playing the first video or the video in the center
  57. if (MyListVideo.Count > 0)
  58. {
  59. MyListVideo[0].PlayVideo(); // or another index if needed
  60. }
  61. }
  62. protected override void OnDisappearing()
  63. {
  64. base.OnDisappearing();
  65. // Stop all videos
  66. foreach (var video in MyListVideo)
  67. {
  68. video.StopVideo();
  69. }
  70. }
  71. }

dy2hfwbg

dy2hfwbg1#

老实说,CarouselView比CollectionView更适合你的问题。因为,
CarouselView是一个用于以可滚动布局呈现数据的视图,用户可以在其中滑动以在项目集合中移动。
您可以通过将“方向”设置为“垂直”来垂直显示其项目。
要全屏,可以很容易地将VerticalOptionHorizontalOption设置为Fill或FillAndExpand。
如果你有任何问题,请告诉我。

相关问题