【问题标题】:Adding an "Android.Views.ViewGroup" to a Xamarin XAML page将“Android.Views.ViewGroup”添加到 Xamarin XAML 页面
【发布时间】:2018-01-25 16:09:23
【问题描述】:

在将Android.Views.ViewGroup 添加到 XAML 页面时,我需要一些帮助。

我有一个 Xamarin 项目,其解决方案结构如下所示:

  • 应用程序1
    • /视图模型
      • /MyPageViewModel.cs
    • /查看次数
      • /MyPageView.xaml
        • /MyPageView.xaml.cs
  • App1.Android
    • /MainActivity.cs
    • /MainApplication.cs
  • App1.iOS
  • MyAndroidBindingProject
    • /罐子
      • /customViewGroup.aar

请注意我使用 Xamarin Android 绑定库添加到解决方案中的 customViewGroup.aar

AAR 文件包含一个 Android.Views.ViewGroup 类,我想在 MyPage.xaml 上显示它,但我不知道该怎么做。我似乎找不到适合这个确切用例的指南或代码示例(我也找不到涉及将简单的Android.Views.View 添加到 Xamarin XAML 页面的指南或代码示例)。

我找到了将Android.Views.ViewGroup 添加到本机 Android 应用程序(使用 Java 和 XML)的示例,但没有显示如何将其添加到 Xamarin 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

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

【问题讨论】:

  • 现在试试,谢谢!
  • 嗯,没有骰子。我已经编辑了我的问题以包含我正在使用的代码。该页面只是显示为空白。
  • 确保您的MyPage 中没有[XamlCompilation(XamlCompilationOptions.Compile)]
  • 不,MyPage 类上没有属性

标签: c# xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

若要在 Xamarin.Forms 页面中包含本机控件,您需要创建自定义控件和平台呈现器。请参阅official documentation 了解完整的演练。

总而言之,您首先在共享项目中声明一个新控件:

public class MyCustomViewControl : View
{

}

现在您在 Android 项目中创建一个自定义渲染器,它将使用您的自定义 Android 视图在本地显示:

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);
        }
    }
}

您还将使用渲染器在本机端设置事件等。

Xamarin.Forms 可以识别和注册渲染器,这要归功于该属性,该属性可以与命名空间上方的 Renderer 位于同一文件中:

[assembly: ExportRenderer (typeof(MyCustomViewControl), typeof(MyCustomViewRenderer))]

【讨论】:

  • Xamarin.Forms 自动扫描程序集的 ExportRenderer 属性以将渲染器与自定义视图连接起来(我已更新我的答案以包含此信息,但它在链接中得到了更好的记录)。在 XAML 文件中,您只输入了 &lt;MyCustomViewControl /&gt;
  • 在Renderer中放一些断点,看看代码是否被调用。如果是,请逐步查看viewGroup= new MyCustomViewGroup(Context); 行是否特别崩溃。
  • viewGroup 变量不为空并且有“数据”?另外,控件有大小吗?也许设置 HeightRequestWidthRequest 可能会有所帮助
  • 否则,这种技术应该有效。我已经多次使用它,就像您使用本机绑定库和正确显示组件的情况一样。所以我真的怀疑布局或本机控件本身的问题,方法应该是正确的:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2020-06-15
  • 2018-04-03
  • 2023-02-02
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
相关资源
最近更新 更多