【问题标题】:How to get acess to variables from other classes in UWP [duplicate]如何从 UWP 中的其他类访问变量 [重复]
【发布时间】:2020-07-22 11:40:06
【问题描述】:

我有两节课。 这是第一个:

namespace Working_Times.Klassen
{
    class calcHours
    {
        //Right here i need the value of startTime from MainPage
    }
}

第二个叫做MainPage

我已经尝试过 calcHours

MainPage mP = new MainPage(); 
mP.startTime = 3; //for example, if startTime was an integer
   //it says: mP.startTime not available in the current context 
              startTime not available in the current context 

非常感谢 :) 很多人一直在回答我,问题是访问权限。 就我而言,我想,这不是问题。我已经尝试将每个变量都更改为公共变量。还是不跑。 顺丰

【问题讨论】:

  • Nobody ever read exception messages :( "mP.startTime not available in the current context " 表示 mP 对象中名为 startTime 的字段在调用方法的上下文。
  • MainPage 类中必须有一个字段/变量/属性,名称为startTime,它应该是public。像这样public int startTime;
  • 确保该属性是公开的。
  • 问题Public Fields versus Automatic Properties 可能有您正在寻找的答案

标签: c# visual-studio xaml uwp


【解决方案1】:

startTime 在当前上下文中不可用

问题是您没有在 MainPage 类中声明 startTime 属性。您需要添加startTime,如下所示。

public class MainPage : Page
{
    public int StartTime { get; set; }

    public MainPage()
    {
       
    }
}

如何在 UWP 中访问其他类的变量

你有很多方法可以做到这一点,首先是为like声明一个属性并使用上面的实例访问属性,另一种是声明静态变量并使用类名访问。

public class MainPage : Page
{    
    public MainPage()
    {
       
    }
    public static int EndTime;

}

访问

 MainPage.EndTime = 4;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多