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

Application.Current.Properties not persisting on WindowsPhone - works fine on iOS

$
0
0

Hi!

So I have this problem where Application.Current.Properties won't persist on Windows Phone, but it works properly on iOS. Android I'm not sure of, because I currently don't have a licence for droid.

This is my code:
Windows Phone project:

//MainPage.xaml.cs
namespace MyFirstFormsApp.WinPhone
{
    public partial class MainPage : global::Xamarin.Forms.Platform.WinPhone.FormsApplicationPage 
    {
        public MainPage()
        {
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new Common.App());
        }
    }
}

Portable project:

public class App : Application
{
    public App()
    {
        MainPage = GetMainPage();

    }
    /// <summary>
    ///   Returns the startup page.
    /// </summary>
    /// <returns>The main page.</returns>
    public static Page GetMainPage()
    {
        var tabbedPage = new TabbedPage();
        var navigationPage = new NavigationPage(new MainPage());
        tabbedPage.Children.Add(navigationPage);
        tabbedPage.Children.Add(new SettingsPage());
        tabbedPage.Children.Add(new AboutPage());
        return tabbedPage;
    }
}


//MainPage.xaml.cs
namespace MyFirstFormsApp.Common.Views
{
    public partial class MainPage
    {
        public MainPage()
        {
            NavigationPage.SetHasNavigationBar(this, false);
            InitializeComponent();
            Title = "MyFirstProject";
            var titleLabel = new Label();

            if (Application.Current.Properties.ContainsKey("awesomeString"))
                titleLabel.Text = Application.Current.Properties["awesomeString"].ToString();
            else
                titleLabel.Text = "Key not found";

            MainPageStackLayout.Children.Insert(0, titleLabel);
        }

        private void TestButton_OnClicked(object sender, EventArgs e)
        {
            Application.Current.Properties["awesomeString"] = "Awesome string!";
        }
    }
}

iOS project:

//AppDelegate.cs
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{

    UIWindow _window;
    //
    // This method is invoked when the application has loaded and is ready to run. In this 
    // method you should instantiate the window, load the UI into it and then make the window
    // visible.
    //
    // You have 17 seconds to return from this method, or iOS will terminate your application.
    //
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        Forms.Init();
        var application = new App();

        _window = new UIWindow(UIScreen.MainScreen.Bounds);

        _window.RootViewController = application.MainPage.CreateViewController();

        _window.MakeKeyAndVisible();
        LoadApplication(application);

        return true;
    }
}

I have no clue as to why this works on iOS and not on Windows Phone. I can swear it used to work, and I have no clue what's different this time around. Am I doing something wrong? I really am stomped here. Any help would be greatly appreciated! Thanks! :)


Viewing all articles
Browse latest Browse all 91519

Trending Articles



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