I need some help with adding an Android.Views.ViewGroup
to a XAML page.
I have a Xamarin project with a solution structure that looks like this:
- App1
- /ViewModels
- /MyPageViewModel.cs
- /Views
- /MyPageView.xaml
- /MyPageView.xaml.cs
- /MyPageView.xaml
- /ViewModels
- App1.Android
- /MainActivity.cs
- /MainApplication.cs
- App1.iOS
- MyAndroidBindingProject
- /Jars
- /customViewGroup.aar
- /Jars
Note the customViewGroup.aar
that I've added to the solution using a Xamarin Android Binding Library.
The AAR file contains an Android.Views.ViewGroup
class that I'd like to show on MyPage.xaml
but I have no clue how to do it. I can't seem to find a guide or code sample that fits this exact use case (nor can I find one that involves adding a simple Android.Views.View
to a Xamarin XAML page).
I've found examples of adding an Android.Views.ViewGroup
to a native Android application (using Java and XML) but nothing that shows how to add it to a Xamarin XAML page.
Please help!
I'm including some source code so you can see what I've tried:
MyPage.xaml
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.Views.MyPage"
xmlns:vm="clr-namespace:App1.ViewModels;"
xmlns:androidWidget="clr-namespace:Com.CustomAAR;assembly=Com.CustomAAR;targetPlatform=Android"
xmlns:formsAndroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
Title="{Binding Title}">
<ContentPage.Content>
<ContentView x:Name="contentViewParent">
<androidWidget:MyCustomViewGroup x:Arguments="{x:Static formsandroid:Forms.Context}">
</androidWidget:MyCustomViewGroup>
</ContentView>
<!--<ContentView
IsVisible="True"
IsEnabled="True"
BindingContext="{Binding MyCustomViewGroup}">
</ContentView>-->
</ContentPage.Content>
</ContentPage>
MyPage.xaml.cs
public partial class MyPage : ContentPage
{
MyCustomViewGroupModel viewModel;
public MyPage()
{
InitializeComponent ();
}
public MyPage(MyCustomViewGroupModel viewModel)
{
InitializeComponent();
#if __ANDROID__
NativeViewWrapper wrapper = (NativeViewWrapper)contentViewParent.Content;
MyCustomViewGroup myCustomViewGroup = (MyCustomViewGroup)wrapper.NativeView;
//myCustomViewGroup = new MyCustomViewGroup(Android.App.Application.Context);
myCustomViewGroup.SomeAction("");
#endif
BindingContext = this.viewModel = viewModel;
}
}
[XamlCompilation(XamlCompilationOptions.Compile)]
in yourMyPage
. – Eren ShenMyPage
class – Omar Himada