Daniel
Daniel

Reputation: 487

How to use Dependency Injection to implement Flyoutnavigation in Xamarin.Forms

how would I use Dependency Injection to use the Flyoutnavigation for my iOS project.

I have decided to stick with the default MasterDetailPage for Android as it doesn't look as bad as iOS.

This is what I have done so far:

In PCL

IMainPage.cs:

public interface IMainPage
{
    Page GetMainPage();
}

App.cs:

public partial class App : Application
{
    public App()
    {
        MainPage = DependencyService.Get<IMainPage>().GetMainPage();
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

In Android

MainPage_Android.cs:

[assembly: Xamarin.Forms.Dependency(typeof(MainPage_Android))]
namespace ProjectName.Droid
{
    public class MainPage_Android : MasterDetailPage, IMainPage
    {
        private NavigationPage detail;
        private MasterPage master;

        public Page GetMainPage()
        {
            MasterDetailPage mdp = new MasterDetailPage();

            //Master
            master = new MasterPage();
            MasterBehavior = MasterBehavior.Popover;
            mdp.Master = master;

            //Detail
            detail = new NavigationPage(new Home())
            {
                BarBackgroundColor = Color.FromHex("#01A0E1"),
                BarTextColor = Color.White
            };
            mdp.Detail = detail;

            return mdp;
        }
    }
}

In iOS

Upvotes: 1

Views: 345

Answers (1)

Ax1le
Ax1le

Reputation: 6641

If you want to use default style on Android and custom style on iOS, you can try to make a custom renderer for MasterDetailPage on iOS. Then embed your Flyoutnavigation.

This is my iPad renderer:

[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
namespace FlyoutNavigationDemo.iOS
{
    public class MainPageRenderer : TabletMasterDetailRenderer
    { 
         //put your Flyoutnavigation's code here
    }
}

Since Flyoutnavigation has too much code. I make a small sample for you, you can refer to here for more details.

Upvotes: 1

Related Questions