在视图之间传递数据的最佳方法是什么?

时间:2021-11-13 18:23:35

I have done some research and still didn't found a solution that seems correct, in a "best practices" way.

我做了一些研究,仍然没有找到一个看似正确的解决方案,采用“最佳实践”的方式。

I'm working on a App for iPhone using Xamarin. This app will be initial iPhone only, but there are plans to make versions for Android and Windows Phone in the near future.

我正在使用Xamarin开发适用于iPhone的App。这个应用程序将只是初始iPhone,但有计划在不久的将来为Android和Windows Phone制作版本。

This App crete/saves a "Moment". This moment have some pictures plus some information. Basically, this moment will be used all over the App, been incremented with more data from lots of views. While I do save this moment to some kind of repository (SQL, filesystem, ..., I still have to implement this), I need it to be alive thru the workflow.

这个App克里特/保存了一个“时刻”。这一刻有一些图片加上一些信息。基本上,这个时刻将在整个应用程序中使用,随着来自大量视图的更多数据而增加。虽然我确实将这个时刻保存到某种存储库(SQL,文件系统,......,我仍然必须实现这一点),但我需要它通过工作流程活着。

One way of doing it, would be:

这样做的一种方法是:

var moment = new Moment()
// .. add infos from view to moment
nextView.Moment = moment;
PerformSegue(...);

Is this the right way of doing it? There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?

这是正确的做法吗?没有任何模式可以用来从所有平台解决它并更好地控制如何在视图之间传递此信息(样本会受到赞赏)?

7 个解决方案

#1


2  

You could use the Singleton pattern to get access to your data in every point of your app.

您可以使用Singleton模式在应用的每个点访问您的数据。

Check out the Wiki page(with a sample): http://en.wikipedia.org/wiki/Singleton_pattern

查看Wiki页面(带有示例):http://en.wikipedia.org/wiki/Singleton_pattern

#2


3  

You might have seen even apple is using the singleton pattern to access the current application instance to open the URL its just an example

你甚至可能已经看到苹果使用单例模式来访问当前的应用程序实例以打开URL这只是一个例子

UIApplication.SharedApplication.OpenUrl(urlToSend)

So in your case Singleton is right choice make sure you are not putting everything here because its a performance hit.

所以在你的情况下,Singleton是正确的选择,确保你没有把所有东西放在这里,因为它的性能受到了影响。

so use singleton when you have no other ways of doing it.

所以当你没有其他方法时,请使用singleton。

Here is my sample thread safe code for making the singleton class

这是我制作单例类的示例线程安全代码

using System;  
namespace MySingletonPattern  
{  
   sealed class Singleton  
   {  
      //To make the thread safe access we need an instance
      private static readonly object _lockObj = new object();  

      //Lazy initialisation for the real instance
      private static volatile Singleton _instance;  

      private Singleton()  
      {  
      }  

      static internal Singleton Instance()  
      {  
         if (_instance == null)  
         {  
            //To make the access Thread safe
            lock(_lockObj)  
            {  
               if(_instance == null)  
               {  
    //Creating the instance of singleton class and this will be re-used 
                  _instance = new Singleton();  
               }    
            }  
         }  
         return _instance;  
      }  
   }  
}  

Here is how to use it

这是如何使用它

Singleton obj1 = Singleton.Instance();

#3


2  

I generally pass the data as an argument in the constructor of the view, then inside the view keep a reference to it as a local variable.

我通常将数据作为参数传递给视图的构造函数,然后在视图内部保持对它的引用作为局部变量。

However, if you are using the same piece of data globally throughout your app, then it might be better to use a Singleton, or just a static class.

但是,如果您在整个应用程序中使用相同的数据,那么使用Singleton或仅使用静态类可能更好。

#4


0  

If you create a singleton, you can access it anywhere in the app. Creating a singleton in ios, using a static method:

如果您创建单身,则可以在应用中的任何位置访问它。使用静态方法在ios中创建单例:

+ (Moment *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedObject = [[self alloc] init];
    });

    return sharedObject;
}

#5


0  

You are right, singleton seems the best choice.

你是对的,单身人士似乎是最好的选择。

For future references, my singleton class:

对于将来的引用,我的单例类:

public sealed class CurrentMoment
{
    private static CurrentMoment s_instance;
    //private Moment m_moment;
    private CurrentMoment()
    {
    }

    public static CurrentMoment Instance
    {
        get
        {
            if (s_instance == null)
                s_instance = new CurrentMoment();

            return s_instance; 
        }
    }

    /// <summary>
    /// Gets or sets the moment.
    /// </summary>
    /// <value>The moment.</value>
    public Moment Moment
    {
        get;
        set;
    }
}

#6


0  

There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?

没有任何模式可以用来从所有平台解决它并更好地控制如何在视图之间传递此信息(样本会受到赞赏)?

I'm not sure what your workflow is. Would it be conceivably atomic within one platform? That is, if user Fred has an Android tablet and an iPhone, he's not going to create a Moment on one platform and finish the workflow on the other?

我不确定你的工作流程是什么。在一个平台上可以想象它是原子的吗?也就是说,如果用户Fred拥有Android平板电脑和iPhone,他不打算在一个平台上创建Moment并在另一个平台上完成工作流程?

If so, store it in the model (the part of the application that is independent of the platforms iOS, Windows Mobile, Android). This is basic Model-View separation, if your views change with different platforms. The singleton for CurrentMoment proposed by Giusepe makes sense to me.

如果是这样,请将其存储在模型中(应用程序中独立于iOS,Windows Mobile,Android平台的部分)。如果您的视图随不同平台而变化,这是基本的模型 - 视图分离。 Giusepe提出的CurrentMoment单例对我来说很有意义。

If the workflow isn't "interruptable" between platforms, then don't bother with a persistent CurrentMoment.

如果工作流在平台之间不是“可中断的”,那么不要打扰持久的CurrentMoment。

#7


0  

You can also extend the Application class

您还可以扩展Application类

public class HelloApplication : Application 
{
    public static int GlobalVariable = 1;
}

#1


2  

You could use the Singleton pattern to get access to your data in every point of your app.

您可以使用Singleton模式在应用的每个点访问您的数据。

Check out the Wiki page(with a sample): http://en.wikipedia.org/wiki/Singleton_pattern

查看Wiki页面(带有示例):http://en.wikipedia.org/wiki/Singleton_pattern

#2


3  

You might have seen even apple is using the singleton pattern to access the current application instance to open the URL its just an example

你甚至可能已经看到苹果使用单例模式来访问当前的应用程序实例以打开URL这只是一个例子

UIApplication.SharedApplication.OpenUrl(urlToSend)

So in your case Singleton is right choice make sure you are not putting everything here because its a performance hit.

所以在你的情况下,Singleton是正确的选择,确保你没有把所有东西放在这里,因为它的性能受到了影响。

so use singleton when you have no other ways of doing it.

所以当你没有其他方法时,请使用singleton。

Here is my sample thread safe code for making the singleton class

这是我制作单例类的示例线程安全代码

using System;  
namespace MySingletonPattern  
{  
   sealed class Singleton  
   {  
      //To make the thread safe access we need an instance
      private static readonly object _lockObj = new object();  

      //Lazy initialisation for the real instance
      private static volatile Singleton _instance;  

      private Singleton()  
      {  
      }  

      static internal Singleton Instance()  
      {  
         if (_instance == null)  
         {  
            //To make the access Thread safe
            lock(_lockObj)  
            {  
               if(_instance == null)  
               {  
    //Creating the instance of singleton class and this will be re-used 
                  _instance = new Singleton();  
               }    
            }  
         }  
         return _instance;  
      }  
   }  
}  

Here is how to use it

这是如何使用它

Singleton obj1 = Singleton.Instance();

#3


2  

I generally pass the data as an argument in the constructor of the view, then inside the view keep a reference to it as a local variable.

我通常将数据作为参数传递给视图的构造函数,然后在视图内部保持对它的引用作为局部变量。

However, if you are using the same piece of data globally throughout your app, then it might be better to use a Singleton, or just a static class.

但是,如果您在整个应用程序中使用相同的数据,那么使用Singleton或仅使用静态类可能更好。

#4


0  

If you create a singleton, you can access it anywhere in the app. Creating a singleton in ios, using a static method:

如果您创建单身,则可以在应用中的任何位置访问它。使用静态方法在ios中创建单例:

+ (Moment *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedObject = [[self alloc] init];
    });

    return sharedObject;
}

#5


0  

You are right, singleton seems the best choice.

你是对的,单身人士似乎是最好的选择。

For future references, my singleton class:

对于将来的引用,我的单例类:

public sealed class CurrentMoment
{
    private static CurrentMoment s_instance;
    //private Moment m_moment;
    private CurrentMoment()
    {
    }

    public static CurrentMoment Instance
    {
        get
        {
            if (s_instance == null)
                s_instance = new CurrentMoment();

            return s_instance; 
        }
    }

    /// <summary>
    /// Gets or sets the moment.
    /// </summary>
    /// <value>The moment.</value>
    public Moment Moment
    {
        get;
        set;
    }
}

#6


0  

There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?

没有任何模式可以用来从所有平台解决它并更好地控制如何在视图之间传递此信息(样本会受到赞赏)?

I'm not sure what your workflow is. Would it be conceivably atomic within one platform? That is, if user Fred has an Android tablet and an iPhone, he's not going to create a Moment on one platform and finish the workflow on the other?

我不确定你的工作流程是什么。在一个平台上可以想象它是原子的吗?也就是说,如果用户Fred拥有Android平板电脑和iPhone,他不打算在一个平台上创建Moment并在另一个平台上完成工作流程?

If so, store it in the model (the part of the application that is independent of the platforms iOS, Windows Mobile, Android). This is basic Model-View separation, if your views change with different platforms. The singleton for CurrentMoment proposed by Giusepe makes sense to me.

如果是这样,请将其存储在模型中(应用程序中独立于iOS,Windows Mobile,Android平台的部分)。如果您的视图随不同平台而变化,这是基本的模型 - 视图分离。 Giusepe提出的CurrentMoment单例对我来说很有意义。

If the workflow isn't "interruptable" between platforms, then don't bother with a persistent CurrentMoment.

如果工作流在平台之间不是“可中断的”,那么不要打扰持久的CurrentMoment。

#7


0  

You can also extend the Application class

您还可以扩展Application类

public class HelloApplication : Application 
{
    public static int GlobalVariable = 1;
}