I was trying to change the brightness of my iOS app using a text entry box, and I found some stuff online which showed me how to create a dependency service and use UIScreen to set the brightness of the app so that is what I did. Here is the interface:
public interface IBrightnessService
{
void SetBrightness(float factor);
}
Here is the code for actually calling the setBrightness, where I'm doing it based on a Xamarin Entry form(text is inputted, and when enter is pressed this method is called).
void Entry_Completed(object sender, EventArgs e) {
var text = ((Entry)sender).Text; //cast sender to access the properties of the Entry
float value = float.Parse(text);
var brightnessService = DependencyService.Get<IBrightnessService>();
brightnessService.SetBrightness((float)value);
}
Here is the implementation in iOS. I tested some stuff, and the value is being sent correctly and the method is being reached, but the line of code UIScreen.MainScreen.Brightness = brightness;
doesn't actually change UIScreen.MainScreen.Brightness, and I can't seem to figure out why, so was looking for help for this.
using Xamarin.Forms;
using UIKit;
[assembly: Dependency(typeof (iOSBrightnessService))]
public class iOSBrightnessService : IBrightnessService
{
public void SetBrightness(float brightness)
{
UIScreen.MainScreen.Brightness = brightness;
}
}