添加"Android.Views.ViewGroup"到Xamarin XAML页面 [英] Adding an "Android.Views.ViewGroup" to a Xamarin XAML page
问题描述
在向XAML页面添加Android.Views.ViewGroup
时,我需要一些帮助.
I need some help with adding an Android.Views.ViewGroup
to a XAML page.
我有一个Xamarin项目,其解决方案结构如下:
I have a Xamarin project with a solution structure that looks like this:
- App1
- /ViewModels
- /MyPageViewModel.cs
- App1
- /ViewModels
- /MyPageViewModel.cs
- /MyPageView.xaml
- /MyPageView.xaml.cs
- /MyPageView.xaml
- /MyPageView.xaml.cs
- /MainActivity.cs
- /MainApplication.cs
- /瓶
- /customViewGroup.aar
- /Jars
- /customViewGroup.aar
请注意我使用Xamarin Android绑定库添加到解决方案中的
customViewGroup.aar
.Note the
customViewGroup.aar
that I've added to the solution using a Xamarin Android Binding Library.AAR文件包含一个我想在
MyPage.xaml
上显示的Android.Views.ViewGroup
类,但是我不知道该怎么做.我似乎找不到适合此确切用例的指南或代码示例(也找不到涉及在Xamarin XAML页面上添加简单的Android.Views.View
的指南或代码示例.)The AAR file contains an
Android.Views.ViewGroup
class that I'd like to show onMyPage.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 simpleAndroid.Views.View
to a Xamarin XAML page).我找到了将
Android.Views.ViewGroup
添加到本机Android应用程序(使用Java和XML)的示例,但是没有显示如何将其添加到Xamarin XAML页面的示例.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.请帮助!
我包含一些源代码,因此您可以看到我尝试过的内容:
I'm including some source code so you can see what I've tried:
MyPage.xaml
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
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; } }
推荐答案
要将本地控件包含在Xamarin.Forms页面中,您需要创建一个自定义控件和平台渲染器.有关完整的演练,请参见官方文档
To include native controls in your Xamarin.Forms page you will need to create a custom control and platform renderer. See the official documentation for full walkthrough.
首先,您首先要在共享项目中声明一个新控件:
In a gist, you first you declare a new control in your shared project:
public class MyCustomViewControl : View { }
现在,在Android项目中,您将创建一个自定义渲染器,该渲染器将使用您的自定义Android视图进行本机显示:
Now in the Android project you create a custom renderer, that will use your custom Android view to display natively:
public class MyCustomViewRenderer : ViewRenderer<MyCustomViewControl, MyCustomViewGroup> { MyCustomViewGroup viewGroup; public MyCustomViewRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<MyCustomViewControl> e) { base.OnElementChanged(e); if (Control == null) { viewGroup= new MyCustomViewGroup(Context); SetNativeControl(viewGroup); } } }
您还将使用渲染器在本机端设置事件等.
You will also use the renderer to setup events, etc. on the native side.
渲染器可以被Xamarin.Forms识别和注册,这要归功于该属性,该属性可以与
Renderer
在同一文件中,位于名称空间上方:The renderer is recognized and registered by Xamarin.Forms thanks to the attribute, that can be in the same file as the
Renderer
, above the namespace:[assembly: ExportRenderer (typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]
这篇关于添加"Android.Views.ViewGroup"到Xamarin XAML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- /ViewModels
- /ViewModels