是否可以在Xamarin Forms ContentView中嵌入Android.VideoView [英] Is it possible to embed Android.VideoView in Xamarin Forms ContentView

查看:109
本文介绍了是否可以在Xamarin Forms ContentView中嵌入Android.VideoView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

由于Mono.Android示例未使用Xamarin.Forms,因此我尝试找出解决方法.因此,在xaml文件中是否需要某种#ifdef来嵌入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?

推荐答案

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

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).

<?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>

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

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)...

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

        ~~~
    }

后面的代码:

#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

输出:

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

    • https://msdn.microsoft.com/en-us/magazine/mt790186.aspx
    • 这篇关于是否可以在Xamarin Forms ContentView中嵌入Android.VideoView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆