I am using a portable project so do not have direct access to native code.
I have an interface in my project that allows me to access native objects in the Android/iOS projects. We use this primarily for playing audio.
Android, for example, has things like
Window w = new Window();
w.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.KeepScreenOn);
However the main issue would be accessing a Window object. I could pass a Xamarin.Forms.Page object to the native code, but there would be no way (I don't think) to cast it to a native Android Window object to access the flags.
Is there a way to do this with a portable project?
You can't do this without platform specific services or renderers. A portable project will have to call platform specific code in order to achieve this.
From that platform specific code, either as a DependencyService or Renderer, you can access the Window object through the Forms.Context. The Forms.Context is your Android Activity, through which you can reach the Window object.
On Android it works like this:
Android.Views.Window window = (Forms.Context as Activity).Window;
window.SetFlags(WindowManagerFlags.KeepScreenOn);
On iOS you can try this (Apple docs):
UIApplication.SharedApplication.IdleTimerDisabled = true;
Now there is a plugin doing exactly what Tim wrote
https://learn.microsoft.com/en-us/xamarin/essentials/screen-lock
simple source code is here
https://github.com/xamarin/Essentials/blob/main/Samples/Samples/ViewModel/KeepScreenOnViewModel.cs
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Samples.ViewModel
{
public class KeepScreenOnViewModel : BaseViewModel
{
public KeepScreenOnViewModel()
{
RequestActiveCommand = new Command(OnRequestActive);
RequestReleaseCommand = new Command(OnRequestRelease);
}
public bool IsActive => DeviceDisplay.KeepScreenOn;
public ICommand RequestActiveCommand { get; }
public ICommand RequestReleaseCommand { get; }
void OnRequestActive()
{
DeviceDisplay.KeepScreenOn = true;
OnPropertyChanged(nameof(IsActive));
}
void OnRequestRelease()
{
DeviceDisplay.KeepScreenOn = false;
OnPropertyChanged(nameof(IsActive));
}
}
}
For Xamarin Forms Android.
Renders file I included below code
Window window = (Forms.Context as Activity).Window;
window.AddFlags(WindowManagerFlags.KeepScreenOn);
Related
I am trying to create a plugin using .Net Maui class libraries that use native Android and iOS code. I tried using the Dependency Service using the Interface, but I get a null point when I try to Register the Interface.
Shared code
public class MyPlugin
{
public static DisplayModel()
{
IDeviceOrientationService service =
DependencyService.Get<IMyPlugin>();
var model = service.GetDeviceName();
}
}
public Interface IMyPlugin
{
string GetDeviceName();
}
Android platform
[assembly: Dependency(typeof(MyPlugin))]
public class MyPlugin:IMyPlugin
{
public string GetDeviceName
{
return "From Android";
}
}
This is just an example. Please help me understand if I am not going about this the correct way and any suggestion would be appreciated.
I'm trying to login with integration to social networks, more specifically to Google in .NET MAUI. I've done it with Xamarin Forms and it worked perfectly, however, in MAUI a standard error is occurring:
Error CS0246 The type or namespace name 'Android' could not be found (are you missing a using directive or an assembly reference?) LoginWithRedes (net6.0-ios), LoginWithRedes (net6.0-maccatalyst), LoginWithRedes (net6.0-windows10.0.19041) C:\MAUI\LoginWithRedes\LoginWithRedes\Platforms\Android\GoogleManager.cs
Libraries not being recognized
Packages I added to the project
Code of the GoogleManager.CS Class where the standard error occurs to me:
`[assembly: Dependency(typeof(GoogleManager))]
namespace LoginWithRedes.Platforms.Android
{
public class GoogleManager : Java.Lang.Object, IGoogleManager, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
{
public static GoogleApiClient _googleApiClient { get; set; }
public static GoogleManager Instance { get; private set; }
public bool IsLogedIn { get; set; }
Context _context;
public GoogleManager()
{
_context = global::Android.App.Application.Context;
Instance = this;
}
public void Login()
{
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
.RequestEmail()
.Build();
_googleApiClient = new GoogleApiClient.Builder((_context).ApplicationContext)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
.AddScope(new Scope(Scopes.Profile))
.Build();
Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
((MainActivity)Forms.Context).StartActivityForResult(signInIntent, 1);
_googleApiClient.Connect();
}
public void Logout()
{
var gsoBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn).RequestEmail();
GoogleSignIn.GetClient(_context, gsoBuilder.Build())?.SignOut();
_googleApiClient.Disconnect();
}
public void OnAuthCompleted(GoogleSignInResult result)
{
if (result.IsSuccess)
{
IsLogedIn = true;
Application.Current.MainPage = new MainPage();
}
else
{
}
}`
OnActivityResult method that I implemented in MainActivity class
If anyone can help me with this error, I would be very grateful.
Note: I'm new to Xamarin and Maui.
Thank you very much in advance
I'm also new to Maui, and in my experience, these errors were caused by using native Xamarin libraries in Maui. Xamarin targets each platform separately using separate nuget packages. Maui's combined 1-project architecture means you need to use packages that work for both at once.
At least when I was getting started a few months ago, these weren't readily available. Firebase client was not yet released for .NET 6.0 (Multiplatform by default).
Things may have changed since then. But I had great success using Firebase with this plugin https://github.com/TobiasBuchholz/Plugin.Firebase. It wraps up the platform specific libraries into a single project API, with a really easy to use c# interface. You can use await and stuff as you would expect. Calling the native APIs was difficult, and required a lot of code duplication. This plugin saves a lot of time, and I haven't yet run into any problems.
The documentation on the plugin is a bit sparse, but hey, it's free and works.
I have a few UWP apps I would like to migrate to Android.
I already migrated some using Xamarin.Forms
I have discovered Uno Platform that seems to be great. But I didn't find any information about integration AdMob advertisement in an Android project using Uno Platform.
Has anyone done it already?
Yes, it is possible and I have been able to get it working in my Uno Platform app on Android and iOS. I am planning to write a blogpost about getting AdMob and AdSense running on Android, iOS and WASM, and publish a Uno Platform library on NuGet that will do all the heavy lifting for you, so stay tuned :-) .
For now, here is a unedited, raw version of the control I am using currently. It requires that you install the Google Play Services Ads NuGet packages in the Android project and in the iOS project.
Android
#if __ANDROID__
using Android.Gms.Ads;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Text;
using Uno.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SmsTicket.Core.Controls
{
public partial class AdControl : ContentControl
{
public AdControl()
{
var adView = new AdView(ContextHelper.Current);
adView.AdSize = AdSize.SmartBanner;
adView.AdUnitId = "YOUR AD UNIT ID";
HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
var adParams = new LinearLayout.LayoutParams(
LayoutParams.WrapContent, LayoutParams.WrapContent);
adView.LayoutParameters = adParams;
adView.LoadAd(new AdRequest.Builder().AddTestDevice("YOUR TEST DEVICE ID").Build());
Content = adView;
}
}
}
#endif
iOS
#if __IOS__
using Google.MobileAds;
using System;
using System.Collections.Generic;
using System.Text;
using UIKit;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using CoreGraphics;
namespace SmsTicket.Core.Controls
{
public partial class AdControl : ContentControl
{
public AdControl()
{
HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
Background = SolidColorBrushHelper.Red;
Width = AdSizeCons.LargeBanner.Size.Width;
Height = AdSizeCons.LargeBanner.Size.Height;
Windows.UI.Xaml.Window.Current.Activated += Current_Activated;
}
private void LoadAd()
{
if (!(Content is BannerView))
{
var adView = new BannerView(AdSizeCons.LargeBanner)
{
AdUnitID = "YOUR AD UNIT ID",
RootViewController = GetVisibleViewController()
};
adView.LoadRequest(GetRequest());
Content = adView;
}
}
Request GetRequest()
{
var request = Request.GetDefaultRequest();
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator. After you get your device ID, add it here
request.TestDevices = new[] { Request.SimulatorId.ToString(), "YOUR TEST DEVICE ID" };
return request;
}
UIViewController GetVisibleViewController()
{
UIViewController rootController;
if (UIApplication.SharedApplication.KeyWindow == null)
{
return null;
}
else
{
rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
}
if (rootController.PresentedViewController == null)
return rootController;
if (rootController.PresentedViewController is UINavigationController)
{
return ((UINavigationController)rootController.PresentedViewController).VisibleViewController;
}
if (rootController.PresentedViewController is UITabBarController)
{
return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
}
return rootController.PresentedViewController;
}
private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
LoadAd();
}
}
}
#endif
Also make sure to include the Ad control only conditionally (as I have provided only Android and iOS version here).
Uno Platform won't block you from using any third party (especially not on mobile). If there is a xamarin binding you can use it as-is in your code, just like you would use it in a Xamarin.Forms app. If it's a library affecting the view you will most likely create a custom control and interact with the third-party classes via C#.
If there's no xamarin binding for a library, you can create one following microsoft documentation.
Good news! For admob there is a microsoft-supported binding nuget and it has been used in an uno application in the past.
I save some preferences in Application.Current.Properties for my Xamarin.Forms app and have a BroadcastReceiver that needs those preferences. The problem is that Xamarin.Forms is not initialized in the BroadcastReceiver so I need a way to access those variables in native Xamain.Android. Is that possible?
I need a way to access those variables in native Xamain.Android
you could use DependencyService ,this functionality enables Xamarin.Forms apps to do anything that a native app can do.
here is a sample for using Android's Toast to show message in shared code:
1.create a interface in shared code:
public interface IToast
{
void LongAlert(string message);//the parameter could pass to the native platform
void ShortAlert(string message);
}
2.Android Implementation:
[assembly: Dependency(typeof(ToastAndroid))]
namespace Demo.Droid
{
class ToastAndroid : IToast
{
public void LongAlert(string message)
{
Toast.MakeText(Android.App.Application.Context, message, ToastLength.Long).Show();
}
public void ShortAlert(string message)
{
Toast.MakeText(Android.App.Application.Context, message, ToastLength.Short).Show();
}
}
}
3.call in shared code:
DependencyService.Get<IToast>().ShortAlert("short toast);
DependencyService.Get<IToast>().LongAlert("long toast);
DependencyService
I found the solution myself.
I used the Xamarin.Forms source (found here) to write a function that reads the values from the same file as they are written to in the forms app. Works like a charm.
My current situation is that I have a winforms application which I would like to create a new WPF controls library projects to start showing new screens in WPF. I would really like to leverage the Caliburn.Micro framework but im struggling getting it set up. I currently have my bootstrapper as a Singleton so that I can initialize it and also Im setting the useApplication property in the base contructor to false. I'm not sure where to go from here any help would be appreciated. thanks
public class AppBootstrapper : BootstrapperBase
{
private static AppBootstrapper bootsrapper;
public static void InitializeInstance()
{
if (bootsrapper == null)
{
bootsrapper = new AppBootstrapper();
}
}
public AppBootstrapper() : base(false)
{
Initialize();
}
}