我尝试实现弹出窗口,这会阻止用户离开页面而不保存。 这意味着我必须处理两种情况:当用户使用工具栏中的硬件后退按钮和后退按钮时。 我有MasterDetails页面。 作为细节我推送一个由NavigationPage包装的Details1页面。 从Details1我打了详细信息2页。
这是问题开始的地方。 当我点击硬件后退按钮 - 在活动上调用OnBackPressed时我可以成功处理它。 但是,如果我使用工具栏后退按钮 - 方法OnOptionsItemSelected根本不会被调用。 这对我来说很奇怪,因为没有MasterDetails页面(只是从Details1推送Details2)它按预期工作: 调用了OnOptionsItemSelected,我可以检查这是否是主页按钮。 它甚至以最新的形式复制。 难道我做错了什么?或者这是Xamarin.Forms的错误?
答案 0 :(得分:2)
看了Xamarin.Forms来源似乎我找到了解决方案。
您必须为func takeSnapShot() {
let mapSnapshotOptions = MKMapSnapshotOptions()
// Set the region of the map that is rendered. (by one specified coordinate)
// let location = CLLocationCoordinate2DMake(24.78423, 121.01836) // Apple HQ
// let region = MKCoordinateRegionMakeWithDistance(location, 1000, 1000)
// Set the region of the map that is rendered. (by polyline)
// var yourCoordinates = [CLLocationCoordinate2D]() <- initinal this array with your polyline coordinates
let polyLine = MKPolyline(coordinates: &yourCoordinates, count: yourCoordinates.count)
let region = MKCoordinateRegionForMapRect(polyLine.boundingMapRect)
mapSnapshotOptions.region = region
// Set the scale of the image. We'll just use the scale of the current device, which is 2x scale on Retina screens.
mapSnapshotOptions.scale = UIScreen.main.scale
// Set the size of the image output.
mapSnapshotOptions.size = CGSize(width: IMAGE_VIEW_WIDTH, height: IMAGE_VIEW_HEIGHT)
// Show buildings and Points of Interest on the snapshot
mapSnapshotOptions.showsBuildings = true
mapSnapshotOptions.showsPointsOfInterest = true
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start() { snapshot, error in
guard let snapshot = snapshot else {
return
}
self.imageView.image = snapshot.image
}
}
创建渲染器并覆盖NavigationPage
。
OnAttachedToWindow
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
Element page = Element.Parent;
MasterDetailPage masterDetailPage = null;
while (page != null)
{
if (page is MasterDetailPage)
{
masterDetailPage = page as MasterDetailPage;
break;
}
page = page.Parent;
}
if (masterDetailPage == null)
{
return;
}
var renderer = Platform.GetRenderer(masterDetailPage) as MasterDetailPageRenderer;
if (renderer == null)
{
return;
}
var drawerLayout = (DrawerLayout) renderer;
Toolbar toolbar = null;
for (int i = 0; i < ChildCount; i++)
{
var child = GetChildAt(i);
toolbar = child as Toolbar;
if (toolbar != null)
{
break;
}
}
toolbar?.SetNavigationOnClickListener(new MenuClickListener(Element, drawerLayout));
}
private class MenuClickListener : Java.Lang.Object, IOnClickListener
{
readonly NavigationPage navigationPage;
private DrawerLayout layout;
public MenuClickListener(NavigationPage navigationPage, DrawerLayout layout)
{
this.navigationPage = navigationPage;
this.layout = layout;
}
public void OnClick(View v)
{
var page = navigationPage.CurrentPage as BasePage;
if (navigationPage.Navigation.NavigationStack.Count <= 1)
{
layout.OpenDrawer((int) GravityFlags.Left);
}
if (page != null)
{
if (page.OnNavigationBackButtonPressed())
{
navigationPage?.PopAsync();
}
}
else
{
navigationPage?.PopAsync();
}
}
}
- 来自BasePage
,并且方法ContentPage
可以处理特殊逻辑。