将对象传递给多个页面作为共享选项卡式页面变量的方法是否存在任何性能或编码问题?

时间:2017-07-06 13:52:06

标签: xamarin xamarin.forms

我正在使用这样的标签页:

public partial class MainPage : TabbedPage
{

    public MainPage()
    {
        InitializeComponent();

        var phrasesPage = new NavigationPage(new PhrasesPage())
        {
            Title = "Play",
            Icon = "play.png"
        };
        ....
        Children.Add(phrasesPage);
        Children.Add(categoriesPage);
        Children.Add(favoritesPage);
        Children.Add(settingsPage);
        Children.Add(aboutPage);
    }

我想在我的网页之间共享变量。例如,将在所有页面和一些其他变量上显示的分数。

我想对我的应用程序进行编码,以便于维护。

public partial class MainPage : TabbedPage
{

   public class Person
   {
       public string Name { get; set; }
       public int Age { get; set; }
       public Person(){}
       public Person(string name, int age)
       {
           Name = name;
           Age = age;
       }
    }

    public MainPage()
    {
        InitializeComponent();

        var person = Person p=new Person()
        {
           Name = "Han Solo",
           Age = 39
        };

        var phrasesPage = new NavigationPage(new PhrasesPage(person))
        {
            Title = "Play",
            Icon = "play.png"
        };
        ....
        ....
        Children.Add(phrasesPage);
        Children.Add(categoriesPage);
        Children.Add(favoritesPage);
        Children.Add(settingsPage);
        Children.Add(aboutPage);
    }

有人可以建议我创建一个包含所有共享变量的对象是否合适,然后在使用"创建页面时将该对象作为参数传递给每个页面的构造函数。新&#34 ;?

2 个答案:

答案 0 :(得分:2)

但是你每次都可以将它传递给构造函数,我认为最好创建一个静态的Singleton类来存储'共享'值。比你不必总是传递它但你只需要调用类来填充/获取值

有关单身人士/静态课程的更多信息:https://www.dotnetperls.com/singleton-static

一个例子:

我们要将值保存在的静态类:  公共静态类测试     {

    public static Person person;

    static Test()
    {

    }

    public static void ResetValues() {
        person= new Person();
    }
}

比你的主页(不要忘记添加使用静态测试(=类的名称)):

public MainPage()
    {
        InitializeComponent();
//person --> comes from the static class 

     person=new Person()
    {
       Name = "Han Solo",
       Age = 39
    };

    var phrasesPage = new NavigationPage(new PhrasesPage())
    {
        Title = "Play",
        Icon = "play.png"
    };
    ....
    ....
    Children.Add(phrasesPage);
    Children.Add(categoriesPage);
    Children.Add(favoritesPage);
    Children.Add(settingsPage);
    Children.Add(aboutPage);
}

你可以从任何方面访问han solo(只需从静态类中调用人员)

答案 1 :(得分:2)

我们创建了一个类来存储和检索静态对象字典中的对象,它位于我们的PCL中,但可以在Forms中使用,因为它是通用的可编译代码。

创建课程:

public static class ApplicationState
{
    private static Dictionary<string, object> _values =
               new Dictionary<string, object>();

    /// <summary>
    /// Sets a value in the dictionary with the entered values.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void SetValue(string key, object value)
    {
        if (_values.ContainsKey(key))
        {
            _values.Remove(key);
        }
        _values.Add(key, value);
    }

    /// <summary>
    /// Gets the object with the associated entered key.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public static T GetValue<T>(string key)
    {
        if (_values.ContainsKey(key))
        {
            return (T)_values[key];
        }
        else
        {
            return default(T);
        }
    }

    /// <summary>
    /// Clears all values from the ApplicationState
    /// </summary>
    public static void ClearValues()
    {
        _values.Clear();
    }

    /// <summary>
    /// Checks if the dictionary contains a value with a key equal to the entered string
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static bool ContainsValue(string key)
    {
        if (_values.ContainsKey(key))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Gets all values currently saved within the ApplicationState, in the form of a dictionary(string, object)
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string, object> GetAllValues()
    {
        return _values;
    }

    /// <summary>
    /// Removes a KeyValuePair from the dictionary where the key equals the entered string
    /// </summary>
    /// <param name="key"></param>
    public static void Remove(string key)
    {
        _values.Remove(key);
    }

    /// <summary>
    /// Removes all KeyValuePairs from the dictionary where the key starts with entered string
    /// </summary>
    /// <param name="startsWith"></param>
    public static void RemoveStartsWith(string startsWith)
    {
        var toRemove = _values.Where(x => x.Key.StartsWith(startsWith))
                     .Select(pair => pair.Key)
                     .ToList();

        foreach (var key in toRemove)
        {
            _values.Remove(key);
        }
    }
}

然后使用以下内容设置数据:

 ApplicationState.SetValue("MyKey", ObjectToStore);

要检索它,您只需使用:

 ApplicationState.GetValue<ObjectToStore>("MyKey");

编辑:

所以你可以将对象/变量拉出字典,更改它,添加到它等等,然后简单地将它放回到同一个键下,你可以在页面更改,按钮点击时执行此操作,无论你想要什么。它非常简单和多功能。

所需的简单int和对象示例

存储int:

int i = 10

ApplicationState.SetValue("ThisIsANumber", i);

ApplicationState.SetValue("ThisIsANumber", 10);

检索Int

int variable = ApplicationState.GetValue<int>("ThisIsANumber");

存储对象

Object obj;

ApplicationState.SetValue("ThisIsAnObject", obj);

检索对象

Object theObject = ApplicationState.GetValue<Object>("ThisIsAnObject");