You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Community Toolkit MediaElement在UWP中无法播放视频及显示控件

Xamarin Community Toolkit MediaElement 在UWP平台无视频/播放控件问题解决

针对你遇到的UWP平台XCT MediaElement仅显示背景色、无视频及播放控件的问题,可按以下步骤排查解决:

1. 完成UWP项目的XCT初始化

UWP平台使用Xamarin Community Toolkit需要额外初始化,在UWP项目的App.xaml.cs的OnLaunched方法中添加初始化代码:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    // 保留原有代码
    Xamarin.CommunityToolkit.UWP.Platform.Init();
    // 保留原有代码
}

2. 确认UWP网络权限

访问网络视频需要开启Internet (Client)权限:

  • 打开UWP项目的Package.appxmanifest
  • 切换到功能选项卡,勾选Internet (Client)

3. 调整布局容器

将MediaElement放入Grid容器中,确保布局空间正确:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="HelloWorldXamarin.MainPage"
             Title="This is a Hello World Practice Project">
    <Grid>
        <xct:MediaElement Source="https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4" 
                          BackgroundColor="LightBlue" 
                          HeightRequest="300"
                          VerticalOptions="FillAndExpand"
                          ShowsPlaybackControls="True"
                          AutoPlay="True"/>
    </Grid>
</ContentPage>

4. 排查版本兼容性

若以上步骤无效,尝试降级Xamarin Community Toolkit到稳定版本(如6.2.0),避免最新版本的兼容性bug。同时确认Xamarin.Forms版本与XCT版本匹配。

5. 测试本地视频

下载测试视频到UWP项目,设置为内容类型,使用本地路径测试,排除网络或视频编码兼容性问题:

<xct:MediaElement Source="ms-appx:///Assets/test.mp4" ... />

内容的提问来源于stack exchange,提问作者ThisQRequiresASpecialist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:46:33