简体   繁体   English

如何从 Xamarin.Forms 中的 UWP 访问 PCL 中的参数

[英]How to get access to the parameters in PCL from UWP in Xamarin.Forms

I am working with the Xamarin Forms in combination with the I2C devices and the Raspberry Pi.我正在将 Xamarin Forms 与 I2C 设备和 Raspberry Pi 结合使用。 I programmed in C# and the Raspberry Pi was installed with Windows IoT.我用 C# 编程,Raspberry Pi 安装了 Windows IoT。 And I have encountered a problem about the parameter access.而且我遇到了一个关于参数访问的问题。

I have a microtimer in the UWP project and I want to read the data from the analog input every 100ms.我在 UWP 项目中有一个微定时器,我想每 100 毫秒从模拟输入读取数据。 In the OnTimedEvent, there is a caculation which requires the some parameters which are set in the PCL project,the namespace is "I2CADDA.MainPage.xaml.cs".在 OnTimedEvent 中,有一个计算需要在 PCL 项目中设置的一些参数,命名空间为“I2CADDA.MainPage.xaml.cs”。 I tried to set these parameters as public static.我试图将这些参数设置为公共静态。

public static double gainFactor = 1;
public static double gainVD = 1;

And in the UWP project , I use the dependency service because I have to use the micro timer, so the realization of the interface is done in "I2CADDA.UWP.MainPage.xaml.cs", in the function OnTimedEvent, I tried to get the parameters from PCL project file.而在UWP项目中,我使用了依赖服务,因为我必须使用微定时器,所以接口的实现是在“I2CADDA.UWP.MainPage.xaml.cs”中完成的,在函数OnTimedEvent中,我试图得到PCL 项目文件中的参数。

public void OnTimedEvent(object sender, MicroLibrary.MicroTimerEventArgs timerEventArgs)
        {

            byte[] readBuf = new byte[2];
            I2CDevice.ReadI2C(chan, readBuf); //read voltage data from analog to digital converter
            sbyte high = (sbyte)readBuf[0];
            int mvolt = high * 16 + readBuf[1] / 16;
            val = mvolt / 204.7 + inputOffset;
            val = val / gainFactor / gainVD; //gainFactor and gainVD shows not exist in current context

        }

It seems that the UWP project can not have access to the PCL project in normal way.看来UWP项目无法正常访问PCL项目。 May I ask how can I solve this problem?请问我该如何解决这个问题? Thank you very much!!!非常感谢!!!

In C#, to call a static Field, you should use the class name to call it,在 C# 中,要调用静态字段,您应该使用类名来调用它,

In your code, the static Fields are in the I2CADDA.MainPage.xaml.cs, and for instance, they are in the I2CADDA.MainPage class, you can call the field as在您的代码中,静态字段位于 I2CADDA.MainPage.xaml.cs 中,例如,它们位于I2CADDA.MainPage类中,您可以将该字段称为

double Factor = I2CADDA.MainPage.gainFactor;
double VD = I2CADDA.MainPage.gainVD;

So your above code should be like this:所以你上面的代码应该是这样的:

public void OnTimedEvent(object sender, MicroLibrary.MicroTimerEventArgs timerEventArgs)
{

    byte[] readBuf = new byte[2];
    I2CDevice.ReadI2C(chan, readBuf); //read voltage data from analog to digital converter
    sbyte high = (sbyte)readBuf[0];
    int mvolt = high * 16 + readBuf[1] / 16;
    val = mvolt / 204.7 + inputOffset;
    val = val / I2CADDA.MainPage.gainFactor / I2CADDA.MainPage.gainVD; 
}

Please also make sure you have reference the PCL in your UWP project.还请确保您在 UWP 项目中引用了 PCL。

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

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