繁体   English   中英

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

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

我正在将 Xamarin Forms 与 I2C 设备和 Raspberry Pi 结合使用。 我用 C# 编程,Raspberry Pi 安装了 Windows IoT。 而且我遇到了一个关于参数访问的问题。

我在 UWP 项目中有一个微定时器,我想每 100 毫秒从模拟输入读取数据。 在 OnTimedEvent 中,有一个计算需要在 PCL 项目中设置的一些参数,命名空间为“I2CADDA.MainPage.xaml.cs”。 我试图将这些参数设置为公共静态。

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

而在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

        }

看来UWP项目无法正常访问PCL项目。 请问我该如何解决这个问题? 非常感谢!!!

在 C# 中,要调用静态字段,您应该使用类名来调用它,

在您的代码中,静态字段位于 I2CADDA.MainPage.xaml.cs 中,例如,它们位于I2CADDA.MainPage类中,您可以将该字段称为

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

所以你上面的代码应该是这样的:

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; 
}

还请确保您在 UWP 项目中引用了 PCL。

暂无
暂无

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

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