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



我正在使用这样的选项卡式页面:

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"创建页面时将该对象作为参数传递给每个页面的构造函数是否好?

我们创建了一个类来存储和检索对象在静态对象字典中,它位于我们的 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.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");

但是,您可以每次都将其传递给构造函数,我认为最好创建一个静态的 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);
}

比你可以从任何情况下访问汉独奏(只需从静态类中调用人)

最新更新