Android fragments xamarin表单:向xaml添加片段
我试图使用本文中介绍的模式在Xamarin Forms项目中向UI添加一个本地android片段 用于承载片段的布局容器显示在屏幕上没有问题,但片段组件除外。在Android项目中看到的结果视图层次结构显示了嵌入的片段,但似乎没有实际渲染任何内容 一些代码:Android fragments xamarin表单:向xaml添加片段,android-fragments,xamarin.forms,Android Fragments,Xamarin.forms,我试图使用本文中介绍的模式在Xamarin Forms项目中向UI添加一个本地android片段 用于承载片段的布局容器显示在屏幕上没有问题,但片段组件除外。在Android项目中看到的结果视图层次结构显示了嵌入的片段,但似乎没有实际渲染任何内容 一些代码: using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Ut
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
namespace NativeFeatures.Droid
{
public class TestNativeFragmentViewGroup : ViewGroup
{
Context context = Forms.Context; // Android.App.Application.Context; // this is supposed to be the equivalent replacement but a crash occurs
Android.Widget.Button buttonTest;
public TestNativeFragmentViewGroup() : base(Forms.Context)
{
context = Forms.Context; // ? Android.App.Application.Context;
this.Id = 43434;
int minWidth = 300;
int minHeight = 800;
SetMinimumHeight(minHeight);
SetMinimumWidth(minWidth);
SetBackgroundColor(Android.Graphics.Color.Firebrick);
buttonTest = new Android.Widget.Button(context);
buttonTest.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
buttonTest.SetBackgroundColor(new Android.Graphics.Color(Android.Graphics.Color.ForestGreen));
buttonTest.BringToFront();
buttonTest.Text = "Button Test";
buttonTest.Id = 12347;
AddView(buttonTest); // this works
addTestFragment(); // layout appears to be there but no fragment
}
LinearLayout ll = null;
private void addTestFragment()
{
ll = new LinearLayout(context);
ll.Orientation = Orientation.Horizontal;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent);
ll.LayoutParameters = layoutParams;
ll.SetBackgroundColor(Android.Graphics.Color.LightGreen);
ll.Id = 234567;
((Activity)context).FragmentManager.BeginTransaction().Add(ll.Id, TestFragment.newInstance("I am frag 1"), "someTag1").Commit();
((Activity)context).FragmentManager.BeginTransaction().Add(ll.Id, TestFragment.newInstance("I am frag 2"), "someTag2").Commit();
AddView(ll);
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
if (buttonTest != null)
{
buttonTest.Layout(left, top, (right - left) / 5, (bottom - top) / 5);
}
if (ll != null)
{
ll.Layout(left, top, right, bottom);
}
}
void UpdateUi()
{
}
}
public class TestFragment : Fragment
{
public static TestFragment newInstance(String text)
{
TestFragment f = new TestFragment();
Bundle b = new Bundle();
b.PutString("text", text);
f.Arguments = b;
return f;
}
// @Override
public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Android.Views.View v = inflater.Inflate(Resource.Layout.layout1, container, false);
string val = Arguments.GetString("text");
((TextView)v.FindViewById(Resource.Id.tvFragText)).SetText(val.ToCharArray(), 0, val.Length);
return v;
}
}
}
还有AndroidView.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:androidWidget="clr-namespace:Android.Widget;assembly=Mono.Android;targetPlatform=Android"
xmlns:formsandroid="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.Platform.Android;targetPlatform=Android"
xmlns:NativeFeatures="clr-namespace:NativeFeatures.Droid;assembly=NativeFeatures.Android;targetPlatform=Android"
mc:Ignorable="d"
x:Class="NativeFeatures.AndroidView">
<ContentView.Content>
<StackLayout>
<Label Text="Hello Xamarin.Forms Should just be Android Content!" />
<NativeFeatures:TestNativeFragmentViewGroup/>
</StackLayout>
</ContentView.Content>
我错过什么了吗?
或者xamarin不以这种方式支持本机片段吗?我不确定您为什么需要这里的片段?在XF中无法直接处理的特殊情况是什么,这使您选择了此路线,请添加更多类似的详细信息。我们可能会为您提供更好的解决方案!好的,最终目标是使用第三方片段,特别是heremapssdk。.aar文件的Xamarin绑定已成功。我可以通过将片段添加到Android项目的根视图中来获得屏幕上的地图控件。这是不令人满意的,因为(正如预期的那样)该片段掩盖了Xamarin库中传递的任何UI组件,或者如果我将Xamarin组件向前推(在其中一个布局上使用BringToFront())然后,地图片段没有得到手势,因为它们被Xamarin项目UI吸收。应该澄清的是,现在地图提供了Android和iOS库,但没有直接支持Xamarin。您链接到的文章包括一个可下载的示例。您下载了该示例并成功构建和运行了吗?那么,为什么不将控制权传递给Android项目,即导航到Android,然后在完成本机操作后返回XF呢