简体   繁体   English

如何从 C# 中的另一个 class 调用变量(移动应用程序)

[英]How to call a variable from another class in C# (mobile app)

I'm working on Visual Studio 2019. I have a variable named "batt_lev" which is in BatteryAlert.Android -> BatteryService1.cs I need to call this variable in the shared code in MainPage.xaml.cs in a method named "void Etichetta()" where i have to use the variable.我正在使用 Visual Studio 2019。我有一个名为“batt_lev”的变量,它位于 BatteryAlert.Android -> BatteryService1.cs 我需要在 MainPage.xaml.cs 的共享代码中以名为“void”的方法调用此变量Etichetta()”,我必须使用变量。 I tried to use BatteryService1.batt_lev, as you can see, but the error is "no BatteryService1 in this context".如您所见,我尝试使用 BatteryService1.batt_lev,但错误是“在此上下文中没有 BatteryService1”。

Can someone help me?有人能帮我吗?

MainPage.xaml.cs in the shared code共享代码中的MainPage.xaml.cs

namespace BatteryAlert
{
    public partial class MainPage : ContentPage
    {
        private double batt_lev;
        


        public MainPage()
        {
            
            InitializeComponent();
            DependencyService.Get<IUpLoad>().upLoad();
            //Etichetta();
            //DependencyService.Get<IAndroidService>().StartServicee();
            //Battery.BatteryInfoChanged += Battery_BatteryInfoChanged;
            

        }

        void Etichetta()
        {
            
            //Battery.BatteryInfoChanged += Battery_BatteryInfoChanged;
 
            batt_lev = (float)Battery.ChargeLevel;
            CounterLabel.Text = BatteryService1.batt_lev.ToString("N2");
        }
    }
}

BatteryService1.cs in BatteryAlert.Android BatteryAlert.Android 中的BatteryService1.cs

namespace BatteryAlert.Droid
{
    [Service(Name = "com.companyname.BatteryAlert.BatteryService1")]
    public class BatteryService1 : JobIntentService
    {
        private static int MY_JOB_ID = 1000;
        public double batt_lev;
        List<double> valoreControllo = new List<double>()
        {
            0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25, 0.20, 0.15, 0.10, 0.05
        };


        protected override void OnHandleWork(Intent p0)
        {
            Device.StartTimer(new TimeSpan(0, 0, 10), () =>
            {
                // do something every 10 seconds
                Battery.BatteryInfoChanged += Battery_BatteryInfoChanged;
                //DependencyService.Get<IAudio>().PlayAudioFile("suono_123.wav");
                return true; // runs again, or false to stop
            });
            
        }

        public static void EnqueueWork(Context context, Intent work)
        {
            Java.Lang.Class cls = Java.Lang.Class.FromType(typeof(BatteryService1));

            try
            {
                EnqueueWork(context, cls, MY_JOB_ID, work);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }

        }

        void Battery_BatteryInfoChanged(Object sender, BatteryInfoChangedEventArgs e)
        {
            batt_lev = (double)e.ChargeLevel;
            batt_lev = Math.Round(batt_lev, 2);

            if (valoreControllo.Contains(batt_lev))
            {
                //CounterLabel.Text = batt_lev.ToString("N2");
                DependencyService.Get<IAudio>().PlayAudioFile("suono_123.wav");
            }

        }
    }
}

If you want to access a variable from another class you should have that class being derived for the class you want to use it.如果您想从另一个 class 访问变量,您应该为您想要使用它的 class 派生 class。

public partial class MainPage : ContentPage

If you want to still have the MainPage derived for the ContentPage you should derive the class you want (BatteryService1) to the ContentPage or vice-versa.如果您仍希望为 ContentPage 派生 MainPage,则应将所需的 class (BatteryService1) 派生到 ContentPage,反之亦然。 Like this:像这样:

public class ContentPage : BatteryService1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM