C# 如何在特定于Android的页面和Xamarin.Forms中的共享页面之间导航?
我正试图在Xamarin表单中为我的android应用程序实现一个摄像头覆盖。当我将ActivityMainLauncher设置为True时,我就能够让我的覆盖在Android上工作。如何从Xamarin.Forms共享代码导航到“活动”页面,然后再导航回共享页面 我的Android活动:C# 如何在特定于Android的页面和Xamarin.Forms中的共享页面之间导航?,c#,xamarin,android-activity,xamarin.forms,custom-renderer,C#,Xamarin,Android Activity,Xamarin.forms,Custom Renderer,我正试图在Xamarin表单中为我的android应用程序实现一个摄像头覆盖。当我将ActivityMainLauncher设置为True时,我就能够让我的覆盖在Android上工作。如何从Xamarin.Forms共享代码导航到“活动”页面,然后再导航回共享页面 我的Android活动: namespace CustomCamera.Droid { [Activity(Label = "CustomCamera", Icon = "@mipmap/icon", Theme = "@style/
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 StackOverflow");
_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();
}
}
}
namespace CustomCamera.Droid
{
[活动(Label=“CustomCamera”,Icon=“@mipmap/Icon”,Theme=“@style/MainTheme”,MainLauncher=true,ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
公共级摄影机:IAndroidOverlay
{
公共无效开始和恢复活动()
{
var intent=新意图(Android.App.Application.Context,typeof(CameraPreview));
Android.App.Application.Context.StartActivity(intent);
}
}
公共类CameraPreview:AppCompative活动
{
公共CameraPreview(){}
私人CustomCameraManager(U经理);
私有位图currentBitmap;
私人摄影师结果_currentResult;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
全局::Xamarin.Forms.Forms.Init(这个,savedInstanceState);
//加载应用程序(新应用程序());
SetContentView(Resource.Layout.Main);
//Resource.Layout.Main.setTitle(“Hello StackOverflow”);
_manager=新的CustomCameraManager(此);
FindViewById(Resource.Id.photo_按钮)
。单击+=异步(发送方,参数)=>等待捕获(新摄影机请求)
{
方向=表面方向。自动,
UseFrontCamera=true,
InitialMessage=“Info message”,
最大宽度或高度=1080,
压缩质量=92,
覆盖层=新覆盖层(Resource.Drawable.face_剪影,15)
});
}
专用异步任务捕获(CameraRequest请求)
{
尝试
{
var imageView=findviewbyd(Resource.Id.preview\u image);
如果(_currentBitmap!=null)
{
imageView.SetImageDrawable(空);
_currentBitmap.Recycle();
File.Delete(_currentResult.FilePath);
}
_currentResult=wait_manager.GetPhoto(请求);
_currentBitmap=Wait imageView.LoadFile(_currentResult.FilePath);
ShowMessage(“它起作用”);
}
捕获(CameraNotAvailableException)
{
ShowMessage(“摄像机不可用”);
}
捕获(PermissionNotGrantedException)
{
ShowMessage(“未授予访问摄像头或存储器的权限”);
}
捕获(CameraCanceledException)
{
ShowMessage(“用户已取消操作”);
}
捕获(CameraException CameraException)
{
ShowMessage($“{cameraException.ErrorType}:{cameraException.Message}”);
}
捕获(例外情况除外)
{
ShowMessage($“发生了非常错误的事情:{ex.Message}”);
}
}
public override void OnRequestPermissionsResult(int-requestCode,字符串[]权限,权限[]grantResults)
{
base.OnRequestPermissionsResult(请求代码、权限、GrantResult);
_经理?.OnRequestPermissionsResult(请求代码、权限、授予结果);
}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、意图数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
_manager?.OnActivityResult(请求代码、结果代码、数据);
}
私有void ShowMessage(字符串消息)
{
Debug.WriteLine(消息);
干杯
.MakeText(此,消息,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上下文):
从您的Android活动调用setpage方法,您的Android活动也应该继承自Xamarin.Forms.Platform.Android.Android活动
SetPage (App.GetSecondPage ());
祝你好运
在查询时还原在导航上有一个很好的示例,即表单到本机和本机到表单 基本上,在调用本机页面之前,您必须以静态方法维护Xamarin.Forms页面的当前状态,如下所示:
public static Page GetSecondPage ()
{
var formsPage = new NavigationPage (new MyThirdPage ());
return formsPage;
}
然后从iOS获取页面实例并调用以下两行代码(ViewController上下文):
从您的Android活动调用setpage方法,您的Android活动也应该继承自Xamarin.Forms.Platform.Android.Android活动
SetPage (App.GetSecondPage ());
祝你好运
如有疑问,请回复