Hi I am building an app using Xamarin forms PCL project. In this app, I want to prevent users from taking screenshot so for android I tried-
this.Window.SetFlags(WindowManagerFlags.Secure, WindowManagerFlags.Secure);
In mainactivity.cs
For windows 10 and 8.1 I found-
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
I tried to put it in MainPage.xaml.cs in UWP project. but my app dont start at all after this. Only splash screen is shown.
Where to place this line of code?
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().IsScreenCaptureEnabled = false;
LoadApplication(new FISE.App());
}
}
are you calling somewhere Window.Current.Activate() ? this should be in the App.xaml.cs
Related
As described in the title I am looking to add a ToolbarItem to my MainPage.xaml.cs in a Xamarin.Forms Project.
This is because I want an Actionbar on my Android App.
This to my knowledge is available on a NavigationPage.
I have not been able to set my Mainpage as a NavigationPage despite many attempts, it appears it will not let me set it as anything other than a ContentPage.
So far the best information I can find on this subject is here:
xamarin.forms not showing ToolbarItem
This however does not answer my question as it is relevant to App.xaml.cs not MainPage.xaml.cs
And my project is a multi-platform Xamarin.Forms for building apps apps for IOS and Android with Xamarin and Xamarin.Forms project.
Currently however I'm just focusing on Android in the shared C# project (MainPage.xaml.cs).
Thanks in Advance.
As Described in Wendy's answer the following code is required:
MainPage = new NavigationPage(new MainPage());
However she forgot the mention this code needs to be added to App.xaml.cs not MainPage.xaml.cs file.
This file is present in the C# Project for a Xamarin.Forms mobile app.
You can find it by clicking the drop down arrow in the 'Solution Explorer' on the 'App.xaml' file to find the 'App.xaml.cs' file.
If you can't find it type it in the search bar on the 'Solution Explorer'.
And edit the MainPage = new MainPage(); code in the file to MainPage = new NavigationPage(new MainPage());
The ToolbarItems needs a Navigation Page to show them up. So modify your MainPage wrapped by a navigation page:
App.xaml.cs:
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage());
}
Screenshot:
I develop with Xamarin 4.5 and I'm not able to find how to put my application to cover the entire screen (full screen).
For Android and for iOS.
Note: I do not want only an image or a video to cover the entire screen, it should be all the application that should cover the entire screen.
Update 2020-04-29
I found half of the solution, only the Android part (inlcluded in my answer with help of FabriBertani for status bar). I tested it and it works fine. Now I have to find a solution for iPhone (or at least, find a way to test on an iPhone).
On Android add this to the OnCreate method of your MainActivity:
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
For iOS add these values to the info.plist file:
<key>UIStatusBarHidden</key>
<true/>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
edit: If you want to remove the toolbar too just add this on your xaml pages:
NavigationPage.HasNavigationBar="False"
Or in the C# code behind
public YourPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
If you want to add this to all your pages, I recommend you to create a base page with this and then use this base page in all your pages.
public class BaseContentPage : ContentPage
{
public BaseContentPage
{
NavigationPage.SetHasNavigationBar(this, false);
}
}
And use it on the xaml:
<?xml version="1.0" encoding="UTF-8"?>
<local:BaseContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourNamespace.Pages"
x:Class="YourNamespace.Pages.YourPage">
</local:BaseContentPage>
This is half of the solution. For Android:
In Android Project. Into MainActivity.OnCreate, add:
this.Window.AddFlags(WindowManagerFlags.Fullscreen); // Hide StatusBar, from FabriBertani
MessagingCenter.Subscribe<Object>(this, "HideOsNavigationBar", (sender) =>
{
int uiOptions = (int)Window.DecorView.SystemUiVisibility;
uiOptions |= (int)SystemUiFlags.HideNavigation;
Window.DecorView.SystemUiVisibility = (StatusBarVisibility) SystemUiFlags.HideNavigation;
});
In Shared project. Into MainPage constructor (I put it after InitializeComponent() but I doubt it is necessary):
MessagingCenter.Send<Object>(this, "HideOsNavigationBar");
I just tried to create a new UWP App referencing: MonoGame.Framework.WindowsUniversal 3.6.0.1625, and Targeting Windows 10 Creators Update (10.0; Build 15063).
I think I did everything right to "bootstrap" a : Game from the SwapChainPanel:
I put <SwapChainPanel x:Name="swapChainPanel" /> in my Page
I put this in codebehind:
public sealed partial class MainPage : Page
{
readonly Game1 _game;
public MainPage()
{
this.InitializeComponent();
// Create the game.
var launchArguments = string.Empty;
_game = MonoGame.Framework.XamlGame<Game1>.Create(launchArguments, Window.Current.CoreWindow, swapChainPanel);
_game.Run();
}
}
And my Game1 : Game is just the same as Monogame templates.
When I start the application, there are no exceptions and the whole window hangs on the "UWP SplashScreen" (when it shows you the icon of your app) and nothing happens. My Draw method just clears the screen with red, just to see it working, but it doesn't.
Am I missing something?
I attached a very simple project to show that.
Solved, the problem was calling: _game.Run();.
By reading the source code on Github, I saw that the method:
static public T Create(string launchParameters, CoreWindow window, SwapChainPanel swapChainPanel)
already called Run:
// Start running the game.
game.Run(GameRunBehavior.Asynchronous);
By removing my call to the Run method, everything worked.
Objective
Update a Xamarin.Forms ListView when the iOS Application Enters the foreground
Approach
The closes example I have seen is found in the monotouch samples
https://github.com/xamarin/monotouch-samples/blob/master/SimpleBackgroundFetch/SimpleBackgroundFetch/AppDelegate.cs
public override void PerformFetch (UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
{
UINavigationController navigationController = Window.RootViewController as UINavigationController;
UIViewController topViewController = navigationController.TopViewController;
if (topViewController is RootViewController) {
(topViewController as RootViewController).InsertNewObjectForFetch (completionHandler);
UIApplication.SharedApplication.ApplicationIconBadgeNumber++;
} else
completionHandler (UIBackgroundFetchResult.Failed);
}
This is using PerformFetch but the WillEnterForegroundMethod will accomplish what I am trying to do.
Problem
The problem is I am using a Xamarin.Form Page and don't know how to get access to the Page or the Pages' ViewModel from the App Delegate Class. Any ideas?
You could register the ViewModel with an IoC container when you set it as the BindingContext for the View and then get it using that.
If you're using MVVMLight, SimpleIoC is included in that package.
How can I disable the page load animation (kind of turn-style) when a new page is loaded in windows phone 8.1 app? I tired searching on the net, but I couldn't find anything useful.
Got the answer
public MyPage()
{
this.InitializeComponent();
Frame myFrame =(Frame)Window.Current.Content;
myFrame.ContentTransitions = null;
}