简体   繁体   English

如何使我的存储变量可供所有类访问? (UWP Windows.Storage,C#)

[英]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.

相关问题 UWP。 如何找出uwp-c#Windows.Storage元素的大小? - UWP. How to find out size of uwp-c# Windows.Storage element? 如何访问 Windows.Storage 命名空间? - How do I get access to Windows.Storage namespace? 如何在带有 Windows.Storage 的 UWP 中使用 System.IO Serializers - How to use System.IO Serializers in UWP with Windows.Storage 如何在C#中声明一个应该可以被项目中的所有类访问的公共变量? - How do I declare a public variable in C# that should be accessible to ALL classes in the project? Unity C#-如何做到这一点,以便可以通过一个类访问所有其他类,变量和方法? - Unity c# - How can I make it so that I can access all my other classes, variables and methods via a single class? C#:如何使类中的数组可从其他类访问? - C#: How to make array in class accessible from other classes? 如何定义可在 C# 中的所有类中访问的 Class? - How to define a Class which is accessible in all classes in C#? C# WPF | 如何在类之间传递变量 - C# WPF | How Do I Pass Variables Between Classes 如何使用Windows.Storage API创建路径? - How to create a path using the Windows.Storage API? C#我可以在我的点网网站中的任何地方都可以访问命名空间来获取代码隐藏和类吗 - C# Can I make a namespace accessible everywhere in my dot net web site for code-behind and classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM