是否可以在Xamarin Forms ContentView中嵌入Android.VideoView

Is it possible to embed Android.VideoView in Xamarin Forms ContentView

我需要创建一个必须在android 7.0-9.0和最新的iOS上运行的移动应用

因此,在Windows 10上的VS 2017 15.9.6中,我尝试在共享项目中使用Xamarin.Forms 3.4作为原生Android.VideoView的容器。

我尝试找出方法,因为Mono.Android示例不使用Xamarin.Forms。 因此,在xaml文件中是否需要一种#ifdef来嵌入Android VideoView? 还是我对这种方法完全错误?


使用共享项目,您可以在XAML中定义本机视图,然后在后面的代码中访问它们(这是基本要求,因为本机Android | iOS控件不可直接绑定,并且大多数方法调用用于设置无法使用的功能 通过XAML(即VideoView的.SetVideoURI方法没有基于Xamarin的包装器属性,因此您必须执行该方法才能播放视频)。

XAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
    xmlns:androidGraphics="clr-namespace:Android.Graphics;assembly=Mono.Android;targetPlatform=Android"
    xmlns:androidContext="clr-namespace:Forms40Shared.Droid;assembly=Forms40Shared.Android;targetPlatform=Android"
    x:Class="Forms40Shared.NativeEmbedPage">
    <ContentPage.Content>
        <StackLayout Margin="20">
           
               
                   
                        <x:Arguments>
                            <x:String>cursive</x:String>
                            Normal</androidGraphics:TypefaceStyle>
                        </x:Arguments>
                    </androidGraphics:Typeface>
                </androidWidget:TextView.Typeface>
            </androidWidget:TextView>
            <ContentView x:Name="contentView" HorizontalOptions="FillAndExpand" VerticalOptions="Center" HeightRequest="200">
               
            </ContentView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

注意:请勿在全局程序集级别或包含本机视图的XAML页面上启用XamlCompilation,因为它不起作用(并且在编译或运行时没有错误,这些视图仅会被删除而不会显示 )...

主要活动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Activity(Label ~~~~
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static MainActivity Instance { get; private set; }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        Instance = this;

        ~~~
    }

背后的代码:

1
2
3
4
5
6
7
#if __ANDROID__
            var videoView = (contentView as NativeViewWrapper).NativeView as VideoView;
            videoView.SetVideoURI(Android.Net.Uri.Parse($"android.resource://{Android.App.Application.Context.PackageName}/raw/fireplace"));
            videoView.Start();
#elif __IOS__
            ~~~
#endif

enter image description here输出:

  • https://msdn.microsoft.com/zh-CN/magazine/mt790186.aspx