Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Shell: Register routes when app starts (before you start AppShell

$
0
0

Hi there
in my main AppShell I register routes like this
[code]
public partial class MainPage : Xamarin.Forms.Shell
{
public MainPage()
{
InitializeComponent();
RegisterRoutes(); //Register page route so we can navigate to the page
}

    //Register page route so we can navigate to the page
    public void RegisterRoutes()
    {
        Routing.RegisterRoute("showImage", typeof(ShowImagePage));
    }
}

[/code]
if I first start the app so it goes to a home page I can navigate to here fine
my App.xaml.cs looks like this
[code]
public partial class App : Application
{
public App()
{
InitializeComponent();

        MainPage = new MainPage();
    }

    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
    }

    //Show image that user wants to share with our app
    public void ShowImage() //this method couses problems if I call this method direcly from native part b4 my app shows home page my app will crash
    {
        Task.Run(async () => await Shell.Current.GoToAsync("showImage")); //Go to the route we registered
    }

[/code]

but in my Native part I hook insto share dialog and want to show this showImage page when user shares a file
[code]
[Activity(Label = "clip2url", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = @image/jpeg)] //Declaration to handle incoming files via share dialog
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.SetFlags("Shell_Experimental", "Visual_Experimental", "CollectionView_Experimental", "FastRenderers_Experimental");
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        //Init Xmarin Plugins
        ImageCircle.Forms.Plugin.Droid.ImageCircleRenderer.Init(); //Init native Image library

        //To get acsess to Xamarin Forms part
        var mainForms = new App();
        LoadApplication(mainForms);

        //Here we tell out app what do we want to do with this share dialog data
        if (Intent.Action == Intent.ActionSend)
        {
            mainForms.ShowImage(); //here I call my ShowImage method and I want the page to be displayed but now my home page a seperate page and I want to then navigate back to home page
        }
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

[/code]

now if I start the app from home screen then try to share file it works fine, but if I start my app from share dialog, then my app crashes as routes for some reason don't get registered and I cannot navigate to my ShowImage page if I didn't first open my MainPage

Hope someone can shed some light to this and help me figure out whats is wrong :smile:

Thanks


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>