我使用的是可移植项目,所以不能直接访问本机代码。
我的项目中有一个接口,允许我访问Android/iOS项目中的本机对象。我们主要用这个来播放音频。
例如,Android有类似的东西
Window w = new Window();
w.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.KeepScreenOn);但是,主要问题是访问Window对象。我可以将Xamarin.Forms.Page对象传递给本机代码,但不可能(我不认为)将其转换为本地Window对象来访问标志。
有什么方法可以用一个可移植的项目来完成吗?
发布于 2017-04-29 09:42:32
如果没有特定于平台的服务或呈现器,就无法做到这一点。为了实现这一点,可移植项目必须调用特定于平台的代码。
从该平台特定的代码(无论是DependencyService还是Renderer ),您可以通过Forms.Context访问Window对象。Forms.Context是你的安卓Activity,你可以通过它到达窗口对象。
在Android上,它的工作方式如下:
Android.Views.Window window = (Forms.Context as Activity).Window;
window.SetFlags(WindowManagerFlags.KeepScreenOn);在iOS上,您可以尝试以下操作(苹果文档):
UIApplication.SharedApplication.IdleTimerDisabled = true;发布于 2018-11-14 17:30:45
现在有一个插件做的正是Tim写的
https://learn.microsoft.com/en-us/xamarin/essentials/screen-lock
简单的源代码在这里
https://github.com/xamarin/Essentials/blob/main/Samples/Samples/ViewModel/KeepScreenOnViewModel.cs
使用System.Windows.Input;使用Xamarin.Essentials;使用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));
}
}
}发布于 2019-02-11 04:47:41
对于Xamarin Forms Android。呈现下面代码中包含的文件
Window window = (Forms.Context as Activity).Window;
window.AddFlags(WindowManagerFlags.KeepScreenOn);https://stackoverflow.com/questions/43687689
复制相似问题