Is it possible to embed Android.VideoView in Xamar

2019-08-01 20:29发布

问题:

I need to create a mobile app that have to run on android 7.0 - 9.0 and the latest iOS

So in my VS 2017 15.9.6 on Windows 10 I try to use Xamarin.Forms 3.4 in a shared project as a container for a native Android.VideoView.

I try to figure out how to do that, since the Mono.Android examples don't use Xamarin.Forms. So, do I need a kind of #ifdef in the xaml file, to embedd ths Android VideoView? Or am I completely wrong with that approach?

回答1:

Using a Shared Projects, you can define the native views in XAML and then access them in the code behind (which is basically a requirement since native Android|iOS controls are not directly bindable and most have method calls for setting up features that would not available via XAML (i.e. a VideoView has a .SetVideoURI method that has no Xamarin-based wrapper property so you have to execute that method to play a video).

XAML:

<?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">
            <androidWidget:TextView x:Arguments="{x:Static androidContext:MainActivity.Instance}" Text="Welcome to Forms!" TextSize="24" View.HorizontalOptions="Center" >
                <androidWidget:TextView.Typeface>
                    <androidGraphics:Typeface x:FactoryMethod="Create">
                        <x:Arguments>
                            <x:String>cursive</x:String>
                            <androidGraphics:TypefaceStyle>Normal</androidGraphics:TypefaceStyle>
                        </x:Arguments>
                    </androidGraphics:Typeface>
                </androidWidget:TextView.Typeface>
            </androidWidget:TextView>
            <ContentView x:Name="contentView" HorizontalOptions="FillAndExpand" VerticalOptions="Center" HeightRequest="200" >
                <androidWidget:VideoView x:Arguments="{x:Static androidContext:MainActivity.Instance}"  />
            </ContentView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Note: Do not enable XamlCompilation at the global assembly level or on XAML pages that contain native views as it will not work (and there are not errors during compiling or runtime, the views just do not show up as they have been stripped out)...

MainActivity

[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;

        ~~~
    }

Code Behind:

#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

Output:

  • https://msdn.microsoft.com/en-us/magazine/mt790186.aspx