我试图创建一个tiktok像视频滚动与xamarin形式.我设法创建一个视频列表和视频列表的数据模型显示,但不是全屏.
我的问题是:在我的xalm代码,我想只显示一个视频在全屏比如果用户的下一个或上一个视频也在全屏(如tiktok)
下面是我的xalm代码:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:videoplayer="clr-namespace:Octane.Xamarin.Forms.VideoPlayer;assembly=Octane.Xamarin.Forms.VideoPlayer"
x:Class="lf.MyPages.Lomy.Discover" x:Name="leafDiscoverPage">
<Grid>
<CollectionView x:Name="VideosCollectionView"
ItemsLayout="VerticalList"
RemainingItemsThreshold="1"
Scrolled="OnScrolled"
RemainingItemsThresholdReached="OnRemainingItemsThresholdReached">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="{Binding Source={x:Reference leafDiscoverPage}, Path=Height}"
WidthRequest="{Binding Source={x:Reference leafDiscoverPage}, Path=Width}">
<!-- Replace 'octane:VideoPlayer' with your actual video player control namespace -->
<videoplayer:VideoPlayer Source="{Binding UserVideoBlobAdress}"
AutoPlay="{Binding IsPlaying}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />
<!-- Add other UI elements as needed -->
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ContentPage>
字符串
我的.cs代码:
public partial class Discover : ContentPage
{
MyUser UserConnected;
ObservableRangeCollection<UserVideoModel> MyListVideo;
private int _videoCount = 0;
public LeafDiscover ()
{
InitializeComponent ();
MyListVideo = new ObservableRangeCollection<UserVideoModel>();
// Initialize your ListVideo with initial video links
UserConnected = (MyUser)App.GetProperties("UserConnected");
OnInitVideoList();
}
public void UpdateVideoUrl(List<UserVideoModel> tmpuservideo)
{
foreach (var tmpvide in tmpuservideo)
{
tmpvide.UserVideoBlobAdress = "https://fo.blob.net/app/"+tmpvide.UserVideoBlobAdress;
}
}
async void OnInitVideoList()
{
var MyInitVideo = await VideosManager.GetVideoAsync(UserConnected.MyId.ToString(), false, "");
UpdateVideoUrl(MyInitVideo);
foreach (var tmpvide in MyInitVideo)
{
Console.WriteLine("mook "+ tmpvide.UserVideoBlobAdress);
}
MyListVideo.AddRange(MyInitVideo);
VideosCollectionView.ItemsSource = MyListVideo;
}
private void OnRemainingItemsThresholdReached(object sender, EventArgs e)
{
if (_videoCount >= 20)
{
// Load more videos or handle the end of the list
_videoCount = 0;
}
}
private void OnScrolled(object sender, ItemsViewScrolledEventArgs e)
{
// Determine the visible item and update playback
var centerIndex = e.CenterItemIndex;
UpdateVideoPlayback(centerIndex);
}
private void UpdateVideoPlayback(int centerIndex)
{
for (int i = 0; i < MyListVideo.Count; i++)
{
MyListVideo[i].IsPlaying = i == centerIndex;
}
}
protected override void OnAppearing()
{
base.OnAppearing();
// Optionally start playing the first video or the video in the center
if (MyListVideo.Count > 0)
{
MyListVideo[0].PlayVideo(); // or another index if needed
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
// Stop all videos
foreach (var video in MyListVideo)
{
video.StopVideo();
}
}
}
型
1条答案
按热度按时间dy2hfwbg1#
老实说,CarouselView比CollectionView更适合你的问题。因为,
CarouselView是一个用于以可滚动布局呈现数据的视图,用户可以在其中滑动以在项目集合中移动。
您可以通过将“方向”设置为“垂直”来垂直显示其项目。
要全屏,可以很容易地将VerticalOption和HorizontalOption设置为Fill或FillAndExpand。
如果你有任何问题,请告诉我。