Xamarin 将对象传递到多个页面作为共享选项卡式页面变量的一种方式,是否存在性能或编码问题?

Xamarin 将对象传递到多个页面作为共享选项卡式页面变量的一种方式,是否存在性能或编码问题?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我使用的是如下选项卡式页面: public partial class MainPage : TabbedPage { public MainPage() { InitializeComponent(); var phrasesPage = new NavigationPage(new PhrasesPage()) { Title = "Play", Icon = "play.pn

我使用的是如下选项卡式页面:

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);
    }

有人能告诉我,创建一个包含所有共享变量的对象,然后在使用“new”创建页面时,将该对象作为参数传递给每个页面的构造函数是否合适

但是每次都可以将其传递给构造函数,我认为最好创建一个静态单例类来存储“共享”值。然后,您不必总是传递它,只需调用类来填充/获取值

关于单例/静态类的更多信息:

例如:

静态类,我们将在其中保存值: 公共静态类测试 {

而不是在主页中(不要忘记使用静态测试(=类名))添加:


无论如何,你都可以访问han solo(只需从静态类调用person)

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

关于单例/静态类的更多信息:

例如:

静态类,我们将在其中保存值: 公共静态类测试 {

而不是在主页中(不要忘记使用静态测试(=类名))添加:


无论如何,您都可以访问han solo(只需从静态类调用person)

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

创建类:

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.GetValue<ObjectToStore>("MyKey");

检索整型

int variable = ApplicationState.GetValue<int>("ThisIsANumber");
检索对象

Object obj;

ApplicationState.SetValue("ThisIsAnObject", obj);
Object theObject = ApplicationState.GetValue<Object>("ThisIsAnObject");
objecttheobject=ApplicationState.GetValue(“ThisIsAnObject”);

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

创建类:

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.GetValue<ObjectToStore>("MyKey");

检索整型

int variable = ApplicationState.GetValue<int>("ThisIsANumber");
检索对象

Object obj;

ApplicationState.SetValue("ThisIsAnObject", obj);
Object theObject = ApplicationState.GetValue<Object>("ThisIsAnObject");
objecttheobject=ApplicationState.GetValue(“ThisIsAnObject”);

如果可能的话,你能用5-6行代码编写一个例子来帮助我理解你的意思吗?顺便说一句,我在这个问题上加了一点,这样也许现在更有意义了。@Alan我将用一个静态类来做一个例子,因为我更习惯于这样做。谢谢你的回答。我投了赞成票,希望其他人也投赞成票。我决定接受另一个答案它更容易实现,但非常感谢您的建议,因为我认为这也很好。再次感谢。如果可能,您可以用5-6行代码编写一个示例,以帮助我理解您的意思。顺便说一句,我在问题中添加了一些内容,所以现在可能更有意义了。@Alan我将使用静态类制作一个示例,因为我更习惯使用静态类谢谢你的回答。我投了赞成票,希望其他人也投赞成票。我决定接受另一个答案,因为它更容易实现,但非常感谢你的建议,因为我认为这也很好。再次感谢。这看起来很好。那么我说你刚刚把代码放在共享项目的一个文件中,它准备好了吗和被使用。@Alan yep它可以按原样使用,我在大多数跨平台应用程序中使用它,因为它非常通用且易于实现。别误会,它不会在任何情况下都是完美的,但它确实解决了许多在页面/视图之间传递数据等问题。如果你不介意,你能举一个存储和恢复的例子吗尝试一个int和一个object。只需要几行。你能想到的最简单的一行。希望它也能帮助其他人,就像我之前发布的关于这一点的文章一样,但直到我改变了一点问题,才得到一个很好的答案。@Alan行吗?我喜欢你的建议,并会标记为正确。你的答案只是比其他答案更容易实现而已虽然两者都有点帮助。你正在开发的是一个大型Xamarin forms应用程序吗?看起来不错。那么,我说你刚刚把代码放在共享项目的一个文件中,它就可以使用了吗?Alan yep它就可以使用了,我在我们的大多数跨平台应用程序中都使用它,因为它非常通用且易于导入lement.别误会我的意思,它不会在任何情况下都是完美的,但它确实解决了很多在页面/视图之间传递数据的问题。如果你不介意的话,你可以举一个存储和检索一个int和一个对象的例子。就几行。你能想到的最简单的一行。希望在我发布关于t的文章时,它也能帮助其他人以前是他的,但直到我把问题改了一点,才得到一个很好的答案。@Alan行吗?我喜欢你的建议,并会认为是正确的。你的答案比另一个答案更容易实现,尽管两者都有点帮助。你正在处理的是一个大型Xamarin表单应用程序吗?