1

I want to create calculator with history and I need to share data between history page and calculating page.

I know about two ways of sharing data between pages in MAUI.

  1. Passing data in parameter of Shell.Current.GoToAsync();
  2. Saving data to .txt file and read from it when class initializes

But in my case first way would redirect me everytime I calculate something and second one seems way to bloated to me. Is there any simpler way to share data?

Grimalkin
  • 107
  • 12
  • 1
    This is called Data Binding .net MAUI uses MVVM pattern. Read this on [Data Binding](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/?view=net-maui-7.0). Also if you want to share data between each instance create a class which is a service and then use it as a dependency which you can call at any point and it will keep state of a `singleton`. [Register a Service](https://learn.microsoft.com/en-us/dotnet/architecture/maui/dependency-injection#:~:text=the%20same%20instance.-,Registration,-Before%20dependencies%20can) – ThisQRequiresASpecialist Dec 13 '22 at 18:58
  • Both of the solutions OP came up with are valid - most apps that maintain a history (like web browsers) use a DB to do it, so SQLite is another option. There are a LOT of ways you could approach it - if you're just learning I would suggest doing the simplest thing and get it to work first, and don't worry about if it's efficient or the "best" approach – Jason Dec 13 '22 at 19:00
  • @Jason I don't do .net MAUI but I do `Xamarin.Forms` which is basically the same so my assumptions are that he isn't using dependency services to their advantage. He can always save the class with property values to a text for history if needed at each `=` press and re-write it after `=` is pressed again if the value has changed or store maximum of 3-5 previous entries as and example. – ThisQRequiresASpecialist Dec 13 '22 at 19:03
  • @ThisQRequiresASpecialist I think that for now I will use `.txt` way but I will definetly experiment with your way later – Grimalkin Dec 13 '22 at 19:33
  • @Grimalkin I'm now writing up a answer for you. It's not .net MAUI but in Xamarin but you can get context of whats goin on. I will try my best to explain everything in depth for you. – ThisQRequiresASpecialist Dec 13 '22 at 19:34

2 Answers2

5

Following up on my thinking... I've done this is Xamarin.Forms but it can transfer to .net MAUI I'm confident. This is just an example so the properties are definitely are not correct

First thing you do is create an Interface that you can speak to.

public interface ICalculatorData
{
    //Here you add all he properties that you want to have. What ever you store in the .txt
    //transfer them here and make them into properties
    List<double> Inputs { get; set; }
    List<string> Operators { get; set; }
    double Answer { get; set; }
}

Then create a class which consumes that Interface

public class CalculatorData : ICalculatorData
{
    //Implementing the interface here.
    public List<double> Inputs { get; set; } = new List<double>();
    public List<string> Operators { get; set; } = new List<string>();
    public double Answer { get; set; } = 0.0;
}

In you entry class of .net MAUI create a DependencyService. Use the link above I've sent to change the code to work for you.

public App()
{
    InitializeComponent();
    
    //Creating a singleton of that class
    DependencyService.Register<ICalculatorData, CalculatorData>();

    MainPage = new AppShell();
}

I don't know how you implement your Calculator class but this is how you can access that singleton instance and have the data persistent as long as the app is running.

public class Calculator
{
    //You can now have a singleton at your disposal at any point allowing you to access all of its properties with the set values
    ICalculatorData CalculatorData = DependencyService.Get<ICalculatorData>();

    //Your code for the calculator...
}

You can have as many ICalculatorData CalculatorData = DependencyService.Get<ICalculatorData>(); as you want and with any class.

If you want to go a step further you can create a class for all your ViewModels to inherit from BaseClass I believe it comes with Shell App anyways. You can create that there and have any ViewModel have access to it without having that extra line of code in all your classes, I personally do this to have persistent data I want to have access to at all times during the runtime of the application.

With this you no longer need to pass data around Shell App and just use that Singleton. You can now have some code to tell your Save() method to save on Answer PropertyChanged and write it to a file.

Hope this helps. Please ask more question if needed in the comments I can do some research into .net MAUI if you get stuck.

0

I want to share with you my point of view about your problem and how to solve it.

The first thing that comes to your mind, will be to use shared memory. You call this "global structure", "static class", "Singleton", or whatever.

You can implement this idea by many ways, even use some fancy new technology (DependencyInjection), and name it "Service".

However, this is not a Service. It is Storage. You are not making this because you want to execute some code. All you want from this is sharing memory. It is quick, it is easy to implement, it is simple to understand.

As your product evolves however, so does the requirement of more classes in this shared memory. More threads start to write and read from this shared memory. More programmers start to work on that project. At the end, the stability suffers.

Your initial idea of passing data as query parameter, is not bad. Message communication is not that bad in general.

Storing data and reading it from Databases is decent option too.

H.A.H.
  • 2,104
  • 1
  • 8
  • 21
  • *At the end, the stability suffers* - My solution is not for mega data resolvers. It is a decent fast solution that will be sufficient for any mobile app out there. If you're making a 3D game then `Unity` will be the technology to use. I doubt an app can get that big for this to suffer. Passing data gets messy and confusing the bigger the project grows. With this, you're able to keep the project clean and optimize certain parts for performance. Also if you're using `DB` yes, of course, don't use this method but without a `DB` personally best solution. – ThisQRequiresASpecialist Dec 14 '22 at 13:34
  • I've yet to run into any problems with a large project. Performance is at its best actually. – ThisQRequiresASpecialist Dec 14 '22 at 13:35
  • I am creating mostly POS applications (with communication to other devices), The count of models is in the range of 30 to 60 usually. The records in the database are 500k to 1m. The first versions, written ~20 years ago, were written using static structures in C++. I have replaced the initial creator of this project, and if I have learned anything from working on it and fixing its problems, is to not use this design, for anything but school projects. – H.A.H. Dec 14 '22 at 13:49
  • Very interesting view on that, my boss worked on the Tesco club card which processes an average of hundreds of thousands of processes a second through POS, he also has 50 years of coding experience on top of that. His point of view on this is this is actually a very reasonable approach if `DB` isn't involved. IDK about school projects but I've yet to see a student come to this approach. This service can connect to a `DB` it doesn't have to be internal to memory. This solution can be modified to support `DB Connections` and store data there instead of memory. – ThisQRequiresASpecialist Dec 14 '22 at 13:58