Sharing information between pages

How do I correctly share the information between different pages in the PCL? Every page contains a few editors/textboxes and I need to save those information to an xml file. I've created a serializable class in PCL with a few objects to save the information that is typed in in the textboxes but I don't know where to call my implementation in which the file is created? Should I put it in App.cs or where exactly?

Another problem is that in PCL System.IO.FileStream is not supported. I need the app to run on UWP (Windows Mobile 10) and I don't get how I can access the System.IO.FileStream in the UWP specific class (where System.IO.FileStream is supported) and somehow bind it with the serializable class in PCL so I can use System.IO.FileStream?

I'm so confused and can't find any easy explanation, please help me.

Answers

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    Pages shouldn't have information. Period.
    ViewModels have information. They are the binding context of the page.

    If two pages need to see the same data just bind them each to the same ViewModel.

    If the MVVM design pattern is new to you then you should read up on it: It is the foundation concept of XAML-based development whether it is WPF or Xamarin. You won't get far without a solid understanding of it.
    https://redpillxamarin.com/2017/01/08/401-revisiting-mvvm/

  • rolfMrolfM ✭✭ ATMember ✭✭

    By "pages" I meant an xaml file (with code behind) created in PCL. In each "page" there is a button (called "next") that leads you to another "page" (xaml file). Is this not allowed? If so, how should I create pages?

    Let's say on page one is an editor and I want to save editor1.text as an element in an xml file. On the second page there is another editor called editor2 and I want editor2.text to be saved in another element in the same xml file. Is that wrong?

    It's a rather simple app but I can't seem to understand how the whole sharing of code works. I followed a tutorial by Mosh Hamedani (iirc) and that's the only education of xamarin I got.

  • rolfMrolfM ✭✭ ATMember ✭✭

    Oh and of course each of my xaml "pages" has a single single view (e.g. Stacklayout) and the editors, etc. are inside of it.

  • HistoryaHistorya ✭✭✭ USMember ✭✭✭

    @rolfM did you find any thing? am looking for that too

  • rolfMrolfM ✭✭ ATMember ✭✭
    edited April 2017

    @RefkaBenmahmoud Indeed, I have found a solution that worked in my case.

    So I have a few data in a page that I want to save into the properties in my serializable class. The information gets stored to those properties everytime I click on a button and the next page is shown.

    This is the "initial" or first page, from where of the data is stored in properties of a serializable class:

    async void SaveButton_Clicked(object sender, EventArgs e)
            {
                SerializableClass sbcls = new SerializableClass
                {
                    RandomPicker = nameOfPicker.Items[nameOfPicker.SelectedIndex], 
                    RandomEditor = editorName.Text
                };
                await Navigation.PushAsync(new NameOfNextPage(sbcls))
        }
    

    On the next page it looks like this:

    namespace MyApp
    {
        public partial class NameOfNextPage : ContentPage
        {
            public SerializableClass sbcls { get; set; }
    
            public NameOfNextPage(SerializableClass data)
            {
                this.sbcls = data;
                InitializeComponent();
            }
    
            async void SaveButton2_Clicked(object sender, EventArgs e)
            {
                this.sbcls.RandomEditor2 = editorName1.Text;
                this.sbcls.RandomEditor3 = editorName2.Text;
                await Navigation.PushAsync(new NameOfAnotherPage(sbcls));
            }
        }
    }
    

    I hope this will help.

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭
    edited April 2017

    I'm sorry... But that is such an extreme violation of good practices and MVVM... Its basically doing things as if it were 1999 WinForms. Its the kind of crap coding we see out of India out-sourcers - or first year interns that are still following 1999 textbooks.

    I deeply urge that nobody including you, ever do this.

    Take the time to learn how to do it right. Use MVVM. Use good practices. Sure it might take a whole day to practice and learn rather than build your app. But that day spent learning good technique will make you a better coder and your apps far easier to work with. A day learning good technique will save you a week of man-hours in the first month of work.

    Learning has the greatest return on investment.

  • rolfMrolfM ✭✭ ATMember ✭✭

    @ClintStLaurent I kinda am worse than a first year intern :smiley: I'm in the last year of an engineering school and this app is just a small part of the graduation process. I don't know anything about programming and as I want to study law later this year I didn't intent to put much effort into learning clean coding. Of course you are absolutely right and this is not the proper way of coding, but you know.. it wasn't that important in this particular case.

    @RefkaBenmahmoud The solution only worked for me and the app wasn't thaaaat important, the only objective was to get it to function and it did, BUT as Clint said, you should really look up the MVVM design pattern and build your app by those standards!

  • HistoryaHistorya ✭✭✭ USMember ✭✭✭

    @rolfM right! you should use MVVM ,
    i am looking for a way to get cookies without webview , so the session keeps open even when closing the application

Sign In or Register to comment.