How to change screen brightness in xamarin forms - c#

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

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://learn.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;
}
}

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;
}

Related

how to get axVLCPlugin21 to play video the size of the axVLCPlugin21 control window?

I am playing a video file that is 1280 x 720 in axVLCPlugin21 like this:
var file = #"C:\test\Amex.mov";
var convertedURI = uri.AbsoluteUri;
axVLCPlugin21.playlist.add(convertedURI);
axVLCPlugin21.playlist.play();
The video does play just fine but it is at the full size 1280x720 the window is 1/3 that size. My understanding is VLC should just automatically scale the video to the size of the axVLCPlugin21 control window. I am using the latest version of VLC 3.0.4 downloaded just a few days ago. I am using VS2015 and .NET 4.5 (I have tried .NET 4.6 and .NET 4.7 also. The full VLC application plays the video at the size of the window? Is there something I need to set in axVLCPlugin21 to get it to automatically scale the video to the size of the control window?
In other words what I want is fit video to window
I had same problem however my project is C++, in order to get that done you need to set video scale to 1 after the InitializeComponent() otherwise you get an exception.
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
this->axVLCPlugin21->video->scale = 1;
//
//TODO: Add the constructor code here
//
}
//Rest of the C++ file...
I assume that code would look like this in C#:
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
this.axVLCPlugin21.video.scale = 1;
//
//TODO: Add the constructor code here
//
}
//Rest of the C# file...
You could also wrap all your axVLCPlugin21 components in a method to set all its video scales and call that method after InitializeComponent() if you want something more elegant.
Let me know if this solution worked for you, because that worked for me.

Reference to the right "System" in c# project

I am new to c# and visual studio and have run into some trouble.
I have created a project with references to "Windows" and ".Net" in visual studio because I want to test a little with smart cards.
The code:
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.SmartCards;
class HandleSmartCard
{
public async void checkNumberOfSmartCards()
{
string selector = SmartCardReader.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
// return "2";
}
}
So far it looks fine. However I also want the project to be able to use
System.Windows.Forms; which I have used in a previos test.
I add reference to System.Windows.Forms; and try to create a form. However in that form when I try this:
Form prompt = new Form();
System.Windows.Forms.Button confirmation = new System.Windows.Forms.Button() { Dock = DockStyle.Bottom };
confirmation.Click += (sender, e) => { prompt.Close(); };
I get a red line under "Close" with the message:
Reference to type component claims it is defined in system but could
not be found.
System is referenced at top of file, but I am guessing it is the wrong type of system right?
Can I somehow use "both Systems" in one project so to speak?
I hope someone understands what I mean and can help me understand this.
You're most likely working on a UWP app. The API for UWP apps is a very small subset of the full .NET framework. You can find more information here
https://msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx
You're attempting to reference System.Windows.Forms which is not allowed in UWP applications.
Looks like you're trying to create a popup to ask the user something. For this, use the MessageDialog class.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.aspx

implement an interface Xamarin forms pcl with c#

I just downloaded the plugin XamJam.Screen and I have to implement it, the problem is that it is an interface.
That said, I've never implemented interfaces in c # programming, and I wonder if anyone can advise me how to implement it, I tried but without success, the following code:
namespace Fimap.WebPart
{
public partial class HomePage : ContentView
{
public HomePage()
{
InitializeComponent();
Screen xyz = new getScreen();
var w = xyz.Size.Width;
}
public class getScreen : Screen
{
public ScreenSize Size
{
get
{
return Size;
}
}
}
}
}
the problem is that size 0 me back around.
My goal is to take the width and height of the device.
You do not need to implement the interface yourself. If you installed the package through NUGET, you only need to call
var size = Plugin.XamJam.Screen.CrossScreen.Current.Size;
you can access the width and height of the screen with size.Width and size.Height
Please visit the project github home page and checkout the additional disclaimers on how the library works, provided by the author.
As you can see, the library is of limited use. There is probably a better way to achieve what ever it is that you are trying to do. Consider researching your actual use case / problem further.

Unable to determine if the Xamarin Forms application is running in the Simulator or on the Device

I'm trying to determine if the app is running in the simulator or on the hardware (Apple iPhone) device.
Various answers are all suggesting that I do the following:
bool isSimulator = MonoTouch.ObjCRuntime.Runtime.Arch ==
MonoTouch.ObjCRuntime.Arch.SIMULATOR;
which I've added to my iOS app AppDelegate.cs file. But it does compile - I'm missing a namespace or assembly.
Here's a pic of the FULL method (with the colour coding showing it cannot find the static property):
Using Clause:
using ObjCRuntime;
Code:
bool isSimulator = Runtime.Arch == Arch.SIMULATOR;
FYI: The MonoTouch namespace is deprecated (~2012) and has been broken up into multiple namespaces within the "Xamarin.iOS" product.
Alternative approach that uses the UIDevice.CurrentDevice information:
public static class IosHelper
{
public static bool IsSimulator {
get {
return UIDevice.CurrentDevice.Model.EndsWith("Simulator") || UIDevice.CurrentDevice.Name.EndsWith("Simulator");
}
}
}
Derived from this answer.

Monogame XAML readout textbox inside gameloop

Heey,
I'm currently working on my second XNA/Monogame game for Windows 8/Metro but ran into a problem. We now came at the point which we need to store a highscore with a name attached to it so I need to handle the onscreen keyboard to get the info.
I searched through the forum and I found some topics related to this but no post with some example code or a description which helped me completely fixing my problem. I changed my project to a XAML template and I got a TextBox working in my GamePage but now I need to get the TextBox inside my game loop to read it out so I can save the name besides my score and I have currently no idea how to do this.
My current code of my GamePage.cs
public GamePage(string launchArguments)
{
this.InitializeComponent();
// Create the game.
_game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);
txtTest.TextChanged += txtTest_TextChanged;
}
void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
}
I found out how I can write the content of the TextBox to a string inside my gameloop but now I'm stuck how I can control the TextBox his properties from inside my gameloop so I can set the Visibility and Focus. Do I need to create my own EventHandler which will watch if I set a Boolean or something?
Thanks in advance.
Greetings,
ForT3X
Disclaimer: Let me just say that I've never worked with Windows 8 XAML projects or the GamePage class before but after doing a little googling I think I understand enough to help.
It seems that your issue boils down to a circular dependency. You want 2-way communication between your GamePage and your Game class.
Communicating from the GamePage to the Game class is easy, because the GamePage is already responsible for creating the Game class and storing it in the _game member variable. Therefore, to send messages from your GamePage to the Game you just need to add a method to your Game class, for example:
void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
_game.SetHighscoreName(txtTest.Text);
Debug.WriteLine(txtTest.Text); //Write content to public string in Main.cs
}
Communicating back the other way (from Game to GamePage) is a little trickier, but it can be solved using an interface and property injection.
First, create an interface that belongs to your Game class. What I mean by that is; it lives in the same project and or namespace as the Game class. It might look something like this:
public interface IGamePageController
{
void ShowHighscoreTextBox();
}
Then, add a property to your Game class like this:
public IGamePageController GamePageController { get; set; }
Next, have the GamePage class implement the interface like so:
public partial class GamePage : PhoneApplicationPage, IGamePageController
{
//...
public void ShowHighscoreTextBox()
{
txtTest.Visibility = Visibility.Visible;
}
}
And finally, in the GamePage constructor you need to set the GamePageController property.
// Create the game.
_game = XamlGame<Main>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.GamePageController = this;
Once you have this pattern in place, it's easy to add new ways for your Game and GamePage classes to communicate by adding more methods to the interface or Game class.
You can share a view model between the XAML page and your game.
The XAML page GamePage creates the instance of your game class. When it does you can also let the game class know about your view model.
_game = XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, this);
_game.XamlGameDataViewModel = new GameDataViewModel();
DataContext = _game.XamlGameDataViewModel;
There is more detail in my post Sharing your view model between Monogame and Xaml
If you need to store some values you should try with IsolatedStorage.
As MSDN says:
"Isolated storage is not available for Windows Store apps. Instead, use the application data classes in the Windows.Storage namespaces included in the Windows Runtime API to store local data and files."
You can find more information here.
Using Windows.Storage you should do something like this:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
if (string.IsNullOrEmpty((string)Windows.Storage.ApplicationData.Current.LocalSettings.Values["highscore"]))
{
localSettings.Values["highscore"] = highscore;
}

Categories