2
votes

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

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

2
the error means that .Attributes isn't a static property of the Window class. You can only use it on a specific instance of the Window class.ADyson
ok now forget about the error, my question is how to change screen brightness in xamarin forms ? Is that a right code for do that ? if yes i will try to fix the errorTheodorus Agum Gumilang
I would frown upon apps that turn up/down my global brightness settings for the app that I set so that my screen is readable and does not hurt my eyes... also my phone autoadjust brightness sometimes depending on sensordata / day of time. The whole Idea is kindof against UI design rules for disabilities - screenreaders voices do not get brighter that way.Patrick Artner
@PatrickArtner well i just try to achieve what my client ask, and my client ask me to add this feature. but yeah you right this idea is not very effective, but still i must try to add that its what my client askTheodorus Agum Gumilang
@TheodorusAgumGumilang You don't always have to do exactly what the client says, especially if it doesn't make sense. You have the option of persuading them, based on your technical expertise, that what they are asking for is a bad idea. If you mention that it makes the solution cheaper and/or quicker, since you don't have to spend time to research and implement the feature, that might help to persuade them. Usually clients will thank you if you stop them doing something silly or unnecessary.ADyson

2 Answers

12
votes

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.

Implement the interface in your PCL

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

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

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

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

Android

Your error message

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

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.

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;

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

var window = CrossCurrentActivity.Current.Activity.Window;

(source)

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

Use UIScreen.MainScreen.Brightness to adjust the brightness.

using Xamarin.Forms;
using UIKit;

[assembly: Dependency(typeof (iOSBrightnessService))]

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

Android:

    public void SetBrightness(float brightness)
    {
        Window window = (MainActivity.Instance as Activity).Window;
        var attributesWindow = new WindowManagerLayoutParams();

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

        window.Attributes = attributesWindow;
    }