【发布时间】:2018-01-25 16:09:23
【问题描述】:
在将Android.Views.ViewGroup 添加到 XAML 页面时,我需要一些帮助。
我有一个 Xamarin 项目,其解决方案结构如下所示:
- 应用程序1
- /视图模型
- /MyPageViewModel.cs
- /查看次数
- /MyPageView.xaml
- /MyPageView.xaml.cs
- /MyPageView.xaml
- /视图模型
- 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