简体   繁体   English

Xamarin表格-如何获得屏幕亮度和串行端口?

[英]xamarin forms - how get screen brightness and serial ports?

I would like to create a page showing all the hardware of the device, including the brightness and the serial ports. 我想创建一个页面,显示设备的所有硬件,包括亮度和串行端口。 How should I do? 我应该怎么做?

If want to get brightness , using DependencyService will be a good way. 如果要获得亮度 ,使用DependencyService将是一个好方法。

First implement the interface : 首先实现接口:

public interface IBrightnessService
{
    void SetBrightness(float factor);
    float GetBrightness();
}

Then in Forms where you want to get or set methods : 然后在要获取或设置方法的窗体中:

SET: 组:

var brightnessService = DependencyService.Get<IBrightnessService>();
brightnessService.SetBrightness((float)0.5);

GET: 得到:

var brightnessService = DependencyService.Get<IBrightnessService>();
Console.WriteLine("brightness is:" + brightnessService.GetBrightness());

In Android , create AndroidBrightnessService : 在Android中,创建AndroidBrightnessService

[assembly: Xamarin.Forms.Dependency(typeof(AndroidBrightnessService))]
namespace App6.Droid
{
    public class AndroidBrightnessService : IBrightnessService
    {
        [Obsolete]
        public float GetBrightness()
        {
            //throw new NotImplementedException();
            //var window = ((Activity)Forms.Context).Window;

            //var attributesWindow = new WindowManagerLayoutParams();

            //attributesWindow.CopyFrom(window.Attributes);

            var brightness = Android.Provider.Settings.System.GetInt(MainActivity.thisMainActivity.ContentResolver, Android.Provider.Settings.System.ScreenBrightness);
            //MainActivity.thisMainActivity is a isntance from activity
            //The returned brightness is an int type value between 0 and 255.
            return brightness;
        }

        [Obsolete]
        public void SetBrightness(float brightness)
        {
            //throw new NotImplementedException();
            var window = ((Activity)Forms.Context).Window;
            //var window = CrossCurrentActivity.Current.Activity.Window;
            var attributesWindow = new WindowManagerLayoutParams();

            attributesWindow.CopyFrom(window.Attributes);
            attributesWindow.ScreenBrightness = brightness;

            window.Attributes = attributesWindow;
        }
    }
}

Note : MainActivity.thisMainActivity is a isntance from activity: 注意:MainActivity.thisMainActivity是来自活动的一个概念:

public static MainActivity thisMainActivity;

onCreate method: onCreate方法:

thisMainActivity = this;

In IOS , Cretae iOSBrightnessService : 在IOS中,Cretae iOSBrightnessService

[assembly: Dependency(typeof(iOSBrightnessService))]
namespace App6.iOS
{
    public class iOSBrightnessService : IBrightnessService
    {
        public float GetBrightness()
        {
            //throw new NotImplementedException();
            return (float)UIScreen.MainScreen.Brightness;
        }

        public void SetBrightness(float brightness)
        {
            //throw new NotImplementedException();
            UIScreen.MainScreen.Brightness = brightness;
        }
    }
}

Note: App6 is the name of my project. 注意:App6是我的项目的名称。

============================================================================= ================================================== ===========================

About serial ports , I haven't used in my project, however this should exist only in android .I think this needs to be referenced by some libraries (such as .so file) on the basis of DependencyService. 关于串行端口 ,我没有在我的项目中使用过,但是它应该只存在于android中。我认为这需要在DependencyService的基础上由某些库(例如.so文件)引用。

Here is a Nuget Package can be refer ( Xamarin.Android.SerialPort ).And a sample project you can have a look. 这是一个可以参考的Nuget包( Xamarin.Android.SerialPort )。您可以查看一个示例项目

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

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