如何以 xamarin 形式更改屏幕亮度 [英] How to change screen brightness in xamarin forms

查看:30
本文介绍了如何以 xamarin 形式更改屏幕亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 xamarin 形式的 qr 页面,我想要的是当 qr 出现时屏幕亮度会更亮,我找不到解决方案.我在互联网上找到了一些代码,但它返回了一些错误代码消息

I have qr page in xamarin forms, and what i want is when the qr is shows up the screen brightness will be brighter, and i cant find solutions for that. i found some code in internet but it return with some error code message

我删除了源代码,因为它看起来让一些人感到困惑,我尝试的代码适用于 xamarin android,这就是为什么它对我不起作用(我认为 xamarin.android 和 xamarin.forms 代码几乎相同,这就是我复制的原因代码并将其粘贴到 xamarin 表单中并收到一些错误消息).现在我真正的问题是如何通过 xamarin Forms 更改屏幕亮度我们可以这样做吗?如果是的话,我可以尝试什么链接,谢谢

EDITED : I deleted the source code because its look like make some people confused, the code that i tried is for xamarin android and thats why it didnt work for me (I thought the xamarin.android and xamarin.forms code is almost same thats why i copy the code and paste it in xamarin forms and got some error message). And now my Real question is How to Change the screen brightness via xamarin Forms can we do that ? if yes how any link that can i try thanks

推荐答案

Xamarin.Forms 不是平台抽象,而是 UI 抽象.因此无法访问屏幕亮度等系统服务.我也没有找到 NuGet 来实现这一点,因此您必须实现平台特定的类来调整屏幕亮度并通过 DependencyService.

Xamarin.Forms is not a platform abstraction, but a UI abstraction. Hence there is no access to system services like the screen brightness. Neither did I find a NuGet to achieve this, hence you'll have to implement platform specific classes to adjust the screen brightness and resolve via DependencyService.

在您的 PCL 中实现接口

Implement the interface in your PCL

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

并使用 DependencyService 从您的公共项目到您的平台特定实现使用该接口操作

and use that interface operations using DependencyService from your common project to your platform specific implementation

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

有关如何使用 DependencyService 的一个非常好的紧凑示例,请参阅此页面

For a very good compact example of how to use DependencyService see this page

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

您的错误信息

非静态字段、方法或属性Windows.Attribute"需要一个对象

An Object is required for the non static field, method , or property 'Windows.Attribute'

表示您试图访问一个类型,就好像它是一个对象一样.您需要一个包含 Window:Window 对象的上下文,例如在您的 MainActivity 中就是这种情况.

means that you are trying to access a type as if it was an object. You need a context in which there is a Window:Window object, this would be the case in your MainActivity for instance.

当您处于另一个上下文中时,您需要以某种方式获取 Window 的实例.在 2.5 之前,这可以通过

When you are in another context you'll need to obtain an instance of Window somehow. Pre 2.5 this was possible with

var window = ((Activity)Forms.Context).Window;

这仍然有效,但已弃用.无论如何,您可以使用 CurrentActivity 插件 并获得Window

This still works, but is deprecated. Anyway, you could use the CurrentActivity plugin and get the Window with

var window = CrossCurrentActivity.Current.Activity.Window;

(来源)

using Xamarin.Forms;

[assembly: Dependency(typeof (AndroidBrightnessService))]

public class AndroidBrightnessService : IBrightnessService
{
    public void SetBrightness(float brightness)
    {
        var window = CrossCurrentActivity.Current.Activity.Window;
        var attributesWindow = new WindowManagerLayoutParams();

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

        window.Attributes = attributesWindow;
    }
}

iOS

使用UIScreen.MainScreen.Brightness来调整亮度.

using Xamarin.Forms;
using UIKit;

[assembly: Dependency(typeof (iOSBrightnessService))]

public class iOSBrightnessService : IBrightnessService
{
    public void SetBrightness(float brightness)
    {
        UIScreen.MainScreen.Brightness = brightness;
    }
}

这篇关于如何以 xamarin 形式更改屏幕亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆