Stefano
Stefano

Reputation: 21

How can I open a new xamarin.forms from a new activity

After starting a new activity I'm trying to open a new xamarin.form but the SetContentView method wants an Android.Views.View input and I do not know how to recover the view from an xamarin.form

Can someone help me? Thanks

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

Upvotes: 1

Views: 2597

Answers (4)

Stefano
Stefano

Reputation: 21

I used the MainViewModel creating various classes passing as MainActivity parameter in order to always have the Activity methods available in any subclass:

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

Thank you all, if you have any other advice tell me as well! ;-)

Stefano

Upvotes: 0

Robbit
Robbit

Reputation: 4358

You can use DependencyService to call the methods, like GetSystemService.

these methods must be in a class inherited from Activity

In Xamarin.Forms, you can use Xamarin.Forms.Forms.Context to achieve it, you can refer to this and this.

Update:

My test codes:

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

Upvotes: 1

Stefano
Stefano

Reputation: 21

Thanks for the reply. The "problem" is that UsbSerailPage uses methods to retrieve information on the USB port and these methods must be in a class inherited from Activity so I thought to create another activity, not to use the MainActivity

for example:

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

Upvotes: 0

Athan CB
Athan CB

Reputation: 29

Because you are working with A Xamarin.Forms project you have to work with the portable project to navigate between pages. You are trying to navigate to A ContentPage(which is a portable xaml page) from you Android Project. Instead of this, you have to go to your first XAML page (MainPage.xaml) and the code behind of this (MainPage.xaml.cs) and you can navigate from there to your UsbSerialPage using PushAsync(new UsbSerailPage());.

Upvotes: 0

Related Questions