我正在我的UWP应用程序中运行一个短视频,而其他一些任务正在后台完成。如果用户不想观看视频,可以使用“跳过视频”按钮。然而,该按钮在视频中大约30秒后才会激活。
我想也许是我的后台任务阻塞了UI,但它们在5-7秒内就完成了。我还认为可能是视频加载导致了问题,但它只有3分钟长,所以我不认为它需要那么长的缓冲时间。
下面是我在页面中使用的XAML:
<Page
x:Class="Cgs.Ux.Views.MainMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Cgs.Ux"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:userControls="using:Cgs.Ux.UserControls"
xmlns:help="using:Cgs.Ux.UserControls.Help"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<RelativePanel RequestedTheme="Light">
<Image Source="{x:Bind Vm.PlainBackgroundImage, Mode=OneWay, Converter={StaticResource ImageConverter}}"
Stretch="Fill"/>
<Viewbox RelativePanel.AlignBottomWithPanel="True" RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True">
<Grid>
<Image Source="{x:Bind Vm.BackgroundImage, Mode=OneWay, Converter={StaticResource ImageConverter}}"
RelativePanel.AlignVerticalCenterWithPanel="True" RelativePanel.AlignHorizontalCenterWithPanel="True"
Height="1080" Width="1920" Opacity=".5"/>
<!--Media Player-->
<MediaPlayerElement
Name="MediaSimple"
Source="ms-appx:///Assets/Videos/Transfer Items Alpha 2.mp4"
Width="1920" Height="1080" AutoPlay="False" Visibility="Collapsed">
</MediaPlayerElement>
<Button
Name="SkipVideo"
VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="30"
Content="Skip Video" Height="100" Width="400" Background="White"
Visibility="{x:Bind Vm.SkipVideoVisible, Mode=OneWay}"
PointerEntered="OnPointerEntered"
Click="{x:Bind Vm.SkipVideo}"/>
</Grid>
</Viewbox>
<ProgressRing
RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True"
Width="100" Height="100" Foreground="NavajoWhite" IsActive="{x:Bind Vm.IsBusy, Mode=OneWay}" />
</RelativePanel>
</Page>下面是相关的代码:
public void PlayMedia()
{
MediaSimple.MediaPlayer.Play();
MediaSimple.Visibility = Visibility.Visible;
}
public void SkipVideo()
{
View.StopVideo();
_introVideoFinished = true;
if (_soloGameLoaded && _introVideoFinished) GotoGame();
}
private void OnPointerEntered(object sender, PointerRoutedEventArgs e)
{
Debug.WriteLine("Pointer Entered");
}我希望按钮应该立即可用,或者几乎立即可用。
发布于 2019-03-30 19:58:54
我试图创建一个独立的项目来复制这个问题。可悲的是,我的独立项目都运行得很好。我的独立项目和失败的项目之间的不同之处在于我在视频启动时启动的后台任务。
我最初的想法是后台任务阻塞了UI,这是我的目标。我修改了后台任务以在单独的线程上运行,媒体播放器和跳过视频按钮工作得很好。
在将后台任务移动到它们自己的线程后,应用程序开始在编组错误时失败,因为后台任务正在尝试与UI对话。这解释了为什么后台任务阻塞UI长达30秒,而我预计它会在5-7秒内完成。
这个故事的寓意是,不要在你的UI线程上运行后台任务,并期望你的UI行为正常。
https://stackoverflow.com/questions/55386363
复制相似问题