gpt4 book ai didi

android - 如何从新 Activity 中打开新的 xamarin.forms

转载 作者:行者123 更新时间:2023-11-30 00:07:48 24 4
gpt4 key购买 nike

开始新 Activity 后,我尝试打开一个新的 xamarin.form,但 SetContentView 方法需要 Android.Views.View 输入,我不知道如何从 xamarin.form 恢复 View

有人可以帮助我吗?谢谢

[Activity(Label = "SecondActivity", Icon = "@drawable/icon", MainLauncher = true, NoHistory = true)]
public class SecondActivity : Activity
{
public SecondActivity() { }

protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

UsbSerialPage page = new UsbSerialPage(); // This is a ContentPage

// Don't work because UsbSerialPage is a ContentPage (xaml) and not an axml page
SetContentView(Resource.Layout.UsbSerialPage);

// Don't work because page is not a Android.Views.View
SetContentView(page);
}
}

最佳答案

您可以使用 DependencyService调用方法,例如 GetSystemService

these methods must be in a class inherited from Activity

在Xamarin.Forms中,可以使用Xamarin.Forms.Forms.Context来实现,可以引用thisthis .

更新:

我的测试代码:

using Android.Content;
using Xamarin.Forms;
using App2.Droid;
using Android.Hardware.Usb;

[assembly:Dependency(typeof(SendImpl))]
namespace App2.Droid
{
class SendImpl : ISend
{
public void Send()
{
//Intent intent = new Intent("com.worldgn.connector.HR_MEASUREMENT");
//intent.PutExtra("HR_MEASUREMENT","value");
//Forms.Context.SendBroadcast(intent);
UsbManager usbManager= Forms.Context.GetSystemService(Context.UsbService) as UsbManager;
}
}
}

关于android - 如何从新 Activity 中打开新的 xamarin.forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com