如何在 Android 特定页面和 Xamarin.Forms 中的共享页面之间导航?
How to Navigate between Android Specific Pages and Shared Pages in Xamarin.Forms?
我正在尝试在 Xamarin Forms 中为我的 android 应用实现相机覆盖。当我将 activity MainLauncher 设置为 True 时,我能够让我的叠加层为 Android 工作。我如何才能从我的 Xamarin.Forms 共享代码导航到 activity 页面,然后再导航回共享页面?
我的AndroidActivity:
namespace CustomCamera.Droid
{
[Activity(Label = "CustomCamera", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class CameraOverlayDroid : IAndroidOverlay
{
public void StartAndroidActivity()
{
var intent = new Intent(Android.App.Application.Context, typeof(CameraPreview));
Android.App.Application.Context.StartActivity(intent);
}
}
public class CameraPreview : AppCompatActivity
{
public CameraPreview() { }
private CustomCameraManager _manager;
private Bitmap _currentBitmap;
private CameraResult _currentResult;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
//LoadApplication(new App());
SetContentView(Resource.Layout.Main);
//Resource.Layout.Main.setTitle("Hello Whosebug");
_manager = new CustomCameraManager(this);
FindViewById<Android.Widget.Button>(Resource.Id.photo_button)
.Click += async (sender, args) => await Capture(new CameraRequest
{
Orientation = SurfaceOrientation.Auto,
UseFrontCamera = true,
InitialMessage = "Info message",
MaxWidthOrHeight = 1080,
CompressionQuality = 92,
Overlay = new Overlay(Resource.Drawable.face_silhouette, 15)
});
}
private async Task Capture(CameraRequest request)
{
try
{
var imageView = FindViewById<ImageView>(Resource.Id.preview_image);
if (_currentBitmap != null)
{
imageView.SetImageDrawable(null);
_currentBitmap.Recycle();
File.Delete(_currentResult.FilePath);
}
_currentResult = await _manager.GetPhoto(request);
_currentBitmap = await imageView.LoadFile(_currentResult.FilePath);
ShowMessage("it works");
}
catch (CameraNotAvailableException)
{
ShowMessage("Camera is not available");
}
catch (PermissionNotGrantedException)
{
ShowMessage("Permissions not granted to access camera or storage");
}
catch (CameraCancelledException)
{
ShowMessage("User has cancelled the operation");
}
catch (CameraException cameraException)
{
ShowMessage($"{cameraException.ErrorType}: {cameraException.Message}");
}
catch (Exception ex)
{
ShowMessage($"Something VERY wrong happened: {ex.Message}");
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
_manager?.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
_manager?.OnActivityResult(requestCode, resultCode, data);
}
private void ShowMessage(string message)
{
Debug.WriteLine(message);
Toast
.MakeText(this, message, ToastLength.Long)
.Show();
}
}
}
我的布局:
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="@+id/preview_image"
android:layout_width="match_parent"
android:layout_height="400dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_marginTop="15dp"
android:background="#dddddd" />
<Button
android:id="@+id/photo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo" />
<Button
android:id="@+id/continue_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</LinearLayout>
上有一个很好的 Forms to Native 和 Native to Forms 导航示例
基本上,在调用本机页面之前,您必须在静态方法中维护 Xamarin.Forms 页面的当前状态:
public static Page GetSecondPage ()
{
var formsPage = new NavigationPage (new MyThirdPage ());
return formsPage;
}
然后从iOS获取你的页面实例并调用以下两行代码如下(ViewController context):
var secondViewController = App.GetSecondPage().CreateViewController();
NavigationController.PushViewController(secondViewController, true);
并且从您的 Android Activity 调用 setpage 方法,另外您 Android activity 应该继承自 Xamarin.Forms.Platform.Android.AndroidActivity
SetPage (App.GetSecondPage ());
祝你好运,
如有疑问请回复
我正在尝试在 Xamarin Forms 中为我的 android 应用实现相机覆盖。当我将 activity MainLauncher 设置为 True 时,我能够让我的叠加层为 Android 工作。我如何才能从我的 Xamarin.Forms 共享代码导航到 activity 页面,然后再导航回共享页面?
我的AndroidActivity:
namespace CustomCamera.Droid
{
[Activity(Label = "CustomCamera", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class CameraOverlayDroid : IAndroidOverlay
{
public void StartAndroidActivity()
{
var intent = new Intent(Android.App.Application.Context, typeof(CameraPreview));
Android.App.Application.Context.StartActivity(intent);
}
}
public class CameraPreview : AppCompatActivity
{
public CameraPreview() { }
private CustomCameraManager _manager;
private Bitmap _currentBitmap;
private CameraResult _currentResult;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
//LoadApplication(new App());
SetContentView(Resource.Layout.Main);
//Resource.Layout.Main.setTitle("Hello Whosebug");
_manager = new CustomCameraManager(this);
FindViewById<Android.Widget.Button>(Resource.Id.photo_button)
.Click += async (sender, args) => await Capture(new CameraRequest
{
Orientation = SurfaceOrientation.Auto,
UseFrontCamera = true,
InitialMessage = "Info message",
MaxWidthOrHeight = 1080,
CompressionQuality = 92,
Overlay = new Overlay(Resource.Drawable.face_silhouette, 15)
});
}
private async Task Capture(CameraRequest request)
{
try
{
var imageView = FindViewById<ImageView>(Resource.Id.preview_image);
if (_currentBitmap != null)
{
imageView.SetImageDrawable(null);
_currentBitmap.Recycle();
File.Delete(_currentResult.FilePath);
}
_currentResult = await _manager.GetPhoto(request);
_currentBitmap = await imageView.LoadFile(_currentResult.FilePath);
ShowMessage("it works");
}
catch (CameraNotAvailableException)
{
ShowMessage("Camera is not available");
}
catch (PermissionNotGrantedException)
{
ShowMessage("Permissions not granted to access camera or storage");
}
catch (CameraCancelledException)
{
ShowMessage("User has cancelled the operation");
}
catch (CameraException cameraException)
{
ShowMessage($"{cameraException.ErrorType}: {cameraException.Message}");
}
catch (Exception ex)
{
ShowMessage($"Something VERY wrong happened: {ex.Message}");
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
_manager?.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
_manager?.OnActivityResult(requestCode, resultCode, data);
}
private void ShowMessage(string message)
{
Debug.WriteLine(message);
Toast
.MakeText(this, message, ToastLength.Long)
.Show();
}
}
}
我的布局: Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="@+id/preview_image"
android:layout_width="match_parent"
android:layout_height="400dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_marginTop="15dp"
android:background="#dddddd" />
<Button
android:id="@+id/photo_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo" />
<Button
android:id="@+id/continue_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</LinearLayout>
基本上,在调用本机页面之前,您必须在静态方法中维护 Xamarin.Forms 页面的当前状态:
public static Page GetSecondPage ()
{
var formsPage = new NavigationPage (new MyThirdPage ());
return formsPage;
}
然后从iOS获取你的页面实例并调用以下两行代码如下(ViewController context):
var secondViewController = App.GetSecondPage().CreateViewController();
NavigationController.PushViewController(secondViewController, true);
并且从您的 Android Activity 调用 setpage 方法,另外您 Android activity 应该继承自 Xamarin.Forms.Platform.Android.AndroidActivity
SetPage (App.GetSecondPage ());
祝你好运,
如有疑问请回复