Xamarin OnLaunched event

Viewed 54

I'm looking to create styles (and eventually some of the pages) using C# code behind. Was looking at the write up on https://docs.microsoft.com/en-us/windows/apps/design/style/xaml-resource-dictionary and when I duplicate that code it gives me an error that LaunchActivatedEventArgs is unknown.

My starting App.xaml.cs file:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace FieldDataTemplateSelectorSample
{
    sealed partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }

        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
           // add styles here
        }
    }
}

It doesn't know the type or namespace for LaunchActivatedEventArgs. If I look at the list of potential fixes, it suggests that I add a using for Windows.ApplicationModel.Activation to the list of usings. If I do that, then it complains about OnLaunched not having a suitable method to override. If I take out the override, and change the signature to

using System;
using Windows.ApplicationModel.Activation;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using FieldDataTemplateSelectorSample.AppStyles;

namespace FieldDataTemplateSelectorSample
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new MainPage();
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }

        protected void OnLaunched(LaunchActivatedEventArgs e)
        {
            Resources = InitStyles.GetAppStyles();
        }
    }
}

The application compiles but the OnLaunched method never gets hit if I put a break point in there. What am I missing? Right now I'm using a UWP project for getting my feet wet, and eventually will add android and ios.

Thanks

1 Answers

OnLaunched is a Windows or UWP API, not a Xamarin API.

Put your code in OnStart instead.

For cross-platform code, find "Windows.Forms" docs, instead of "Windows App Development" docs.


OR sometimes you'll want code in your .UWP or .Windows project - but I don't think this is one of those situations.

Related