I see this in many projects that demonstrate the use of CAEmitterLayer, but how does it translate to MonoTouch aka Xamarin.iOS?
+ (Class) layerClass
{
//configure the UIView to have emitter layer
return [CAEmitterLayer class];
}
I know I can use UIView.Layer.AddSubLayer() but there seems to be performance impact.
I think you need to expose it to Obj runtime like the following.
[Export ("layerClass")]
public static Class LayerClass () {
return new Class (typeof (CAEmitterLayer));
}
For further info take a look to MonoTouch... CATiledLayer example.
Hope it helps.
P.S. Check the code. I've written without Xamarin Studio support.
Related
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;
}
I have recently got into c# with XNA and am just making the transition to MonoGame since I've read that XNA is no longer supported. With that said I have come across a problem in MonoGame that I didn't have with XNA when attempting to make a Load() method for a Sprite class in my program. The way I used to do it in XNA is as follows:
public void Load(ContentManager content)
{
content.Load<Texture2D>(AssetName);
}
Now the problem I have with MonoGame is that I cannot seem to reference ContentManager in my Sprite class. The class has all the 'using Microsoft.Xna.Framework' that my Game1 class has, and nothing in my code is static so I don't understand why I cannot reference ContentManager, since it is not recognised when I try and put it in the Load(). Is there a different way to do this in MonoGame, or am I not referencing it properly?
Hm, I don't see the issue right away, but I'll try to help:
You have probably tried right-click/resolve already, but it's worth saying it anyways.
Reminder that Loading content in Monogame is different than using XNA. You've to use the build-in pipeline tool and transfer the content over there, don't forget to build it everytime when uploading a new texture.
There has been no changes to the Content.RootDirectory?
Just making some heads-up to be sure you've done that. and hopefully this will help you out as well.
Assuming you've referenced the MonoGame.Framework.dll or NuGet package there's nothing special about the code. It should look something like this:
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace YourGameProject
{
public class Sprite
{
public string AssetName { get; set; }
public void Load(ContentManager content)
{
content.Load<Texture2D>(AssetName);
}
}
}
I just wrote that code in my own project and it compiles. If it's not working for you there must be something else wrong.
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.
I'm using Xamarin and need to call through to two static java methods in a jar I've linked to. Here is the java class in the jar:
package com.test;
public class Car {
public static Car makeCar(String name);
public void drawCar(ImageView imageview);
}
I'm not sure how to pass in the parameters for the two methods to the JNI code. The JNIEnv.Call**Method() class takes a JValue[] array for parameters, I'm trying to use it to wrap a C# string and call it all like so:
// C#
string carName = "mustang";
JValue[] paramCarName = new JValue[] {
new JValue(JNIEnv.NewString(carName))
};
IntPtr theClass = JNIEnv.FindClass("com.test.Car");
IntPtr theMethod = JNIEnv.GetMethodID(theClass,
"makeCar", "()Ljava/lang/String;");
IntPtr resultCar = JNIEnv.CallStaticObjectMethod(
theClass, theMethod, paramCarName);
Is that correct usage? I'm having the same problem with calling the second method, which refers to a C# version of android's ImageView:
// C#
// Xamarin provides an ImageView wrapper class.
ImageView imageview = ...;
// Is it alright to use JNIEnv.ToJniHandle here to reference the imageview?
JValue[] paramCarName = new JValue[] {
new JValue (JNIEnv.ToJniHandle (imageview))
};
...
The above currently compiles ok, but I can't run it since I only have the free version. Any info on this would be great as I'm sure I'm misusing this.
Thanks
If you are having trouble getting a jar to bind you may be able to fix the errors with Transforms/Metadata.xml (documentation here). With that file you can suppress bindings for classes or entire packages that cause code generation issues.
If Metadata.xml cannot get the job done, another (less desirable) option is to create your own Java library that wraps the one that won't bind, and exposes only the methods you need to access from C#. Then bind that jar.
But, it sounds like you have the free version of Xamarin (probably why you are trying to avoid jar binding since that requires the paid version) so I'll attempt to fix your JNI code:
// C#
IntPtr classHandle;
IntPtr theClass = JNIEnv.FindClass("com/test/Car", classHandle);
IntPtr theMethod = JNIEnv.GetMethodID(theClass,
"makeCar", "(Ljava/lang/String;)V");
// Just create a new JValue with your C# Android object peer
JNIEnv.CallStaticObjectMethod(classRef, methodRef, new JValue[] {
new JValue(imageView)
});
My JNI is not super-advanced so take the above with some grains of salt. But according to code generated by Xamarin Studio for jar binding, the above should be (close to) correct.
VS 2008
I have this code snippet I found on a VB website.
But for some reason I am having trouble converting it to C#.
My.Computer.Network.IsAvailable
Many thanks,
using System.Net.NetworkInformation;
internal class Program
{
private static void Main()
{
NetworkInterface.GetIsNetworkAvailable();
}
}
Yes, garethm is right, this class (Network) is from a VB.NET library - you need to reference the Microsoft.VisualBasic assembly if using in a C# project.
Microsoft.VisualBasic.Devices.Network n = new Microsoft.VisualBasic.Devices.Network();
if (n.IsAvailable)
{
// do stuff
}
Works for me - my network is available :).
As far as how Network relates to NetworkInterface class, it depends on what you want to do next. For instance, Network has such nice stuff as NetworkAvailabilityChanged event, and UploadFile method. On the other hand, NetworkInterface can give you a bunch of specific technical info such as speed or whether it supports multicast.
BTW, there is nothing undocumented about using a class from Microsoft.VisualBasic namespace - it's the core idea behind .NET that you can use classes from assemblies regardless of the language they were written in.
What I generally do is write a small app, then load then project in Reflector and disassemble it.
but you can use this class:
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged
Isn't the whole "My" thing from a VB library?
This appears to work. It's probably very undocumented usage though:
Microsoft.VisualBasic.Devices.Network net = new Microsoft.VisualBasic.Devices.Network();
if (net.IsAvailable)
{
Text = "Network is available";
}
else
{
Text = "Network unavailable";
}
Note that I needed to add a reference to Microsoft.VisualBasic to my project.