Is It Possible To Embed Android.videoview In Xamarin Forms Contentview
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
Solution 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"?><ContentPagexmlns="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><StackLayoutMargin="20"><androidWidget:TextViewx:Arguments="{x:Static androidContext:MainActivity.Instance}"Text="Welcome to Forms!"TextSize="24"View.HorizontalOptions="Center" ><androidWidget:TextView.Typeface><androidGraphics:Typefacex:FactoryMethod="Create"><x:Arguments><x:String>cursive</x:String><androidGraphics:TypefaceStyle>Normal</androidGraphics:TypefaceStyle></x:Arguments></androidGraphics:Typeface></androidWidget:TextView.Typeface></androidWidget:TextView><ContentViewx:Name="contentView"HorizontalOptions="FillAndExpand"VerticalOptions="Center"HeightRequest="200" ><androidWidget:VideoViewx:Arguments="{x:Static androidContext:MainActivity.Instance}" /></ContentView></StackLayout></ContentPage.Content></ContentPage>
Note: Do not enableXamlCompilation
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 ~~~~
publicclassMainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
internal static MainActivity Instance { get; privateset; }
protectedoverride 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
Post a Comment for "Is It Possible To Embed Android.videoview In Xamarin Forms Contentview"