【问题标题】:How to get access to the parameters in PCL from UWP in Xamarin.Forms如何从 Xamarin.Forms 中的 UWP 访问 PCL 中的参数
【发布时间】:2018-10-17 02:02:02
【问题描述】:

我正在将 Xamarin 表单与 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# xamarin.forms uwp


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多