[英]How do I make my stored variables accessible to all classes? (UWP Windows.Storage, C#)
I am using ApplicationData.LocalSettings to save my variables between forms and for when the user closes and reopens the application. 我正在使用ApplicationData.LocalSettings来保存表单之间以及用户关闭并重新打开应用程序时的变量。 When I try to read the saved variables in another form, I need to insert the code into a method to use
localSettings.Values
; 当我尝试以另一种形式读取保存的变量时,需要将代码插入方法中以使用
localSettings.Values
; otherwise, I get the error "A field initializer cannot reference the non-static field, method, or property FormName.localSettings": 否则,我将收到错误消息“字段初始化器无法引用非静态字段,方法或属性FormName.localSettings”:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.ApplicationDataCompositeValue composite =
(Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["nutritionSettings"]; //this line brings the error
When I insert these lines of code into a method, it works, like this: 当我将这些代码行插入一个方法时,它的工作原理如下:
static void Storage()
{
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.ApplicationDataCompositeValue composite =
(Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["nutritionSettings"];
}
However, I have not been able to figure out how to make the saved variables accessible outside of this method. 但是,我无法弄清楚如何使已保存的变量可以在此方法之外访问。 Whenever I try to access them, I get the error, "The name 'composite' does not exist in this context':
每当我尝试访问它们时,都会收到错误消息“在此上下文中不存在名称'composite'”:
public LogFood()
{
this.InitializeComponent();
int calorieMin = Convert.ToInt32(composite["calorieMin"]);
int calorieMax = Convert.ToInt32(composite["calorieMax"]);
int gramsFatMin = Convert.ToInt32(composite["gramsFatMin"]);
int gramsFatMax = Convert.ToInt32(composite["gramsFatMax"]);
int gramsCarbsMin = Convert.ToInt32(composite["gramsCarbsMin"]);
int gramsCarbsMax = Convert.ToInt32(composite["gramsCarbsMax"]);
int gramsProteinMin = Convert.ToInt32(composite["gramsProteinMin"]);
int gramsProteinMax = Convert.ToInt32(composite["gramsProteinMax"]);
}
What should I change about the method so that I can access these variables anywhere in the code for this form? 我应该对方法进行哪些更改,以便可以在此表单的代码中的任何位置访问这些变量? I'm a bit new to C# as a whole, and methods as a whole tend to confuse me.
总体而言,我对C#有点陌生,而整个方法往往会使我感到困惑。 Any recommended reading or documentation would be helpful.
任何推荐的阅读材料或文档都将对您有所帮助。
"The name 'composite' does not exist in this context'
“在这种情况下,名称'composite'不存在'”
The problem is that you did not declare composite
object in LogFood
class. 问题是您没有在
LogFood
类中声明composite
对象。 If you want to make variables accessible to all classes. 如果要使变量可用于所有类。 You could declare public static in
App
class and John Wu is correct. 您可以在
App
类中声明public static,而John Wu是正确的。 For more please refer the following steps. 有关更多信息,请参考以下步骤。
public static ApplicationDataCompositeValue composite;
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
composite = (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["nutritionSettings"];
}
Usage 用法
public LogFood()
{
this.InitializeComponent();
int calorieMin = Convert.ToInt32(App.composite["calorieMin"]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.