我需要從類中獲取一個非靜態欄位App.xaml.cs,但我不知道如何。
這是我App.xaml.cs包含該IsUserLoggedIn欄位的代碼:
public partial class App : Application
{
internal bool IsUserLoggedIn { get; private set; }
}
這是從我的班級呼叫該IsUserLoggedIn欄位的代碼:EventTable.cs
internal class EventTable
{
private void UpdateEvents()
{
if (App.Current.IsUserLoggedIn) // here is an error: "Applcation"
{ // does not contain the definition
// of "IsUserLoggedIn"
}
}
}
uj5u.com熱心網友回復:
App.CurrentApplication.Current與宣告Application型別相同的靜態屬性。您需要將其轉換為App. 我建議這樣做:
public partial class App : Application
{
internal bool IsUserLoggedIn { get; private set; }
public static App Instance
{
get { return Current as App; }
}
}
然后在條件下使用它:
internal class EventTable
{
private void UpdateEvents()
{
if (App.Instance.IsUserLoggedIn)
{
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489997.html
