两个班级之间如何进行交流? 我有两个课程,一个是秒表课程,第二个是我的生命周期事件课程“应用程序”
1级:
public partial class Stopwatch : ContentPage
{
Stopwatch stopwatch;
bool isOn = false;
public Stopwatch()
{
InitializeComponent();
stopwatch = new Stopwatch();
labeltimer.Text = "00:00:00.00";
}
private void btnTimerReset(object sender, EventArgs e)
{
btnStart.Text = "Start";
stopwatch.Reset();
}
private void btnTimerstop(object sender, EventArgs e)
{
if (labeltimer.Text != "00:00:00.00")
btnStart.Text = "Resume";
stopwatch.Stop();
}
private void btnTimerStart(object sender, EventArgs e)
{
Timerstart();
isOn = true;
}
private void Timerstart()
{
stopwatch.Start();
Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
{
labeltimer.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss\\.ff");
return true; // return true to repeat counting, false to stop timer
});
}
public bool isStopwatchEnabled()
{
if(isOn)
{
return true;
}
else
return false;
}
}
}
应用程序类
public partial class App : Application
{
bool isenableSW = false;
public App()
{
InitializeComponent();
DependencyService.Register<MockDataStore>();
MainPage = new AppShell();
{
};
}
protected override void OnSleep()
{
Stoppuhr tp = new Stoppuhr();
isenableSW = tp.isStopwatchEnabled();
if (isenablesw)
{
tp.Timerstart();
}
}
}
我现在的问题是,当我启动秒表时,我的 bool 变量从 false 切换为 true。这就是我想要的......当我现在触发 onSleep() 函数时,它会创建秒表类的一个新实例,接下来它应该检查秒表是否在 onSleep() 触发之前启用,但我的 bool 变量始终为 false因为它有它的标准值(= false)。有道理......但是我如何才能在这些类之间正确通信?
我想我找到了解决方案。当我这样做时,该值不会改变:
bool isOn = false -> static bool isOn = false;