【发布时间】:2020-11-02 02:25:35
【问题描述】:
我已经在我的应用程序中实现了一个地图,虽然是在一个视图上。在创建了一些布局之后,我想实现一个底部导航视图,这意味着我必须使用片段。我已经将我的活动代码移动到名为 map_fragment 的片段中,起初我不确定在哪里使用 FindFragmentById 但根据findViewById in Fragment 这应该用于 OnCreateView() 生命周期方法。 我现在的主要问题是捕获地图片段,因为我收到错误 The name 'SuportMapFragment' does not exist in the current context when using
var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
// Was able to use FragmentManager, but it returns null...
// var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
mapFragment.GetMapAsync(this);
这是完整的代码
public class map_fragment : Android.Support.V4.App.Fragment, IOnMapReadyCallback
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
// Capture the Map Fragment
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
base.OnCreateView(inflater, container, savedInstanceState);
View view = inflater.Inflate(Resource.Layout.activity_main, container, false);
var mapFragment = (SupportMapFragment)SupportFragmentManager.FindFragmentById(Resource.Id.gmap);
//var mapFragment = (SupportMapFragment)FragmentManager.FindFragmentById(Resource.Id.gmap);
mapFragment.GetMapAsync(this);
return view;
}
public void OnMapReady(GoogleMap _map)
{
// Some other code here
}
}
根据要求,这是我的main_activity
public class MainActivity : AppCompatActivity
{
BottomNavigationView bottomNavigation;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Initialize Xamarin Essentials for the Geolocation Service
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.main_frame);
//var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
//if (toolbar != null)
//{
// SetSupportActionBar(toolbar);
// SupportActionBar.SetDisplayHomeAsUpEnabled(false);
// SupportActionBar.SetHomeButtonEnabled(false);
//}
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
// Load the first fragment on creation
LoadFragment(Resource.Id.profile_view);
}
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.profile_view:
fragment = profile_fragment.NewInstance();
break;
case Resource.Id.map_view:
fragment = map_fragment.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
这些是我用作指南的教程
【问题讨论】:
-
您所指的 SupportMapFragment 和 SuportMapFragment 有区别吗?您能否也分享一下布局文件 activity_main 的内容?你想学习什么教程?
标签: android google-maps xamarin android-fragments xamarin.android