如何从新活动中打开新的xamarin.forms

时间:2018-02-08 08:51:03

标签: android xamarin xamarin.android

在开始一个新的活动之后,我正在尝试打开一个新的xamarin.form,但是SetContentView方法想要一个Android.Views.View输入,我不知道如何从xamarin.form中恢复视图

有人能帮助我吗? 感谢

[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);
    }
}

4 个答案:

答案 0 :(得分:1)

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

  

这些方法必须在从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;
        }
    }
}

答案 1 :(得分:0)

因为您正在使用A Xamarin.Forms项目,所以您必须使用可移植项目在页面之间导航。您正尝试从Android Project导航到A ContentPage(可移植的xaml页面)。而不是这个,你必须转到你的第一个XAML页面(MainPage.xaml)和它背后的代码(MainPage.xaml.cs),你可以使用PushAsync(新的UsbSerailPage());从那里导航到你的UsbSerialPage。

答案 2 :(得分:0)

感谢您的回复。 "问题"是UsbSerailPage使用方法来检索USB端口上的信息,这些方法必须在一个继承自Activity的类中,所以我想创建另一个活动,而不是使用MainActivity

例如:

Android.Hardware.Usb.UsbManager usbManager = GetSystemService(Context.UsbService) as UsbManager;
var permissionGranted = await usbManager.RequestPermissionAsync(selectedPort.Driver.Device, this);

答案 3 :(得分:0)

我使用MainViewModel创建了各种类作为MainActivity参数传递,以便始终在任何子类中都有Activity方法:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    /// <summary>
    /// Restituisce la MainActivity.
    /// </summary>
    public static MainActivity Instance { get; private set; }

    /// <summary>
    /// Classe per la gestione del device.
    /// </summary>
    public DeviceBase deviceScelto = null;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Instance = this;
        // other...
    }
}

public class DeviceService : IDeviceService
{
    /// <summary>
    /// Restituisce il device istanziato.
    /// </summary>
    /// <returns>Device.</returns>
    public IDeviceApp GetDeviceApp()
    {
        switch (App.DeviceScelto)
        {
            case App.EnumDeviceType.UsbSerial:
                MainActivity.Instance.deviceScelto = new UsbSerialDevice(MainActivity.Instance);
                break;

            case App.EnumDeviceType.Bluetooth:
                MainActivity.Instance.deviceScelto = new BluetoothDevice(MainActivity.Instance);
                break;

            default:
            case App.EnumDeviceType.NFC:
                MainActivity.Instance.deviceScelto = new NfcDevice(MainActivity.Instance);
                break;
        }

        return MainActivity.Instance.deviceScelto;
    }
}

public class UsbSerialDevice : DeviceBase, IDeviceApp
{
    public Activity mainActivity;

    /// <summary>
    /// Classe per la gestione del device USB-Seriale ereditata da <see cref="DeviceBase"/> e <see cref="IDeviceApp"/>.
    /// </summary>
    /// <param name="mainActivity">MainActivity.</param>
    public UsbSerialDevice(Activity mainActivity)
    {
        this.mainActivity = mainActivity;
        this.usbManager = this.mainActivity.GetSystemService(Context.UsbService) as UsbManager;
    }
}

谢谢大家,如果您有任何其他建议,请告诉我! ; - )

斯特凡诺