我要在uwp中创建一个类似instagram的应用,我想在视频播放时,当用户上下滚动时,在视图中未显示控件后,视频会自动暂停(就像instagram)。
我什么也没尝试,因为我不知道从哪里开始。
这是我的xaml:
<Page.Resources>
<DataTemplate x:Key="VideoTemplate">
<MediaElement MinHeight="300" MaxHeight="400" AutoPlay="False" AreTransportControlsEnabled="True" Source="{Binding Uri, Mode=OneWay}" PosterSource="{Binding PosterSourceUri,Mode=OneWay}">
<MediaElement.TransportControls>
<MediaTransportControls IsCompact="True"/>
</MediaElement.TransportControls>
</MediaElement>
</DataTemplate>
</Page.Resources>
<Grid>
<ListView x:Name="LVPosts"
SelectionMode="None"
ScrollViewer.VerticalScrollMode="Enabled"
ItemsSource="{Binding PostsGenerator.Items}"
ContainerContentChanging="LVPostsContainerContentChanging">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Margin"
Value="0" />
<Setter Property="Padding"
Value="5" />
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Setter Property="VerticalContentAlignment"
Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
我想我应该使用ContainerContentChanging事件:
int LatestItemIndex = -1;
object LatestSelected = null;
private void LVPostsContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
try
{
if (LatestItemIndex != -1 && args.ItemIndex != LatestItemIndex && LatestSelected != null
&& LatestSelected is MyModel uc && uc != null)
{
((LVPosts.ItemsPanelRoot.Children[LatestItemIndex] as ListViewItem).ContentTemplateRoot as MediaElement).Stop();
}
}
catch { }
LatestSelected = args.Item;
LatestItemIndex = args.ItemIndex;
}
但是它不起作用。
我应该怎么做?