Adding ToolbarItems to MainPage.xaml.cs in a Xamarin.Forms Project - c#

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:

Related

How to make a Xamarin app fullscreen (all screen)

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");

How to best implement a log in page with Caliburn.Micro MVVM

I am new to using caliburn.micro and currently learning MVVM.
I am using the windows template studio to create my UWP app, and it works great! But unfortunately, I am not familiar with MVVM and still learning UWP.
I get how the navigation works etc and how the shellpage is loaded. However, I want to prompt the user to log in upon opening the app (i.e. a login in page will start with no navigation sidebar).
I also want to make sure I'm following best practices...
I have tried substituting the MainViewModel with LoginViewModel which I get works, however, I don't want to create the navigation pane. I get that this is triggered by the "new Lazy(CreateShell)". I'm just not sure if I want to remove this from the activation service and call a method upon login?
Below is the default code supplied by the windows template studio which triggers on activation of the app if I understand correctly.
private ActivationService CreateActivationService()
{
return new ActivationService(_container, typeof(ViewModels.LoginViewModel), new Lazy<UIElement>(CreateShell));
}
private UIElement CreateShell()
{
var shellPage = new Views.ShellPage();
_container.RegisterInstance(typeof(IConnectedAnimationService), nameof(IConnectedAnimationService), new ConnectedAnimationService(shellPage.GetFrame()));
return shellPage;
}
I just need to be pointed in the right direction or lead to a video/tutorial as I'm struggling!!! any help much appreciated.
If you want to show login page,you can remove the ShellPage.It is a navigation view.
in App.xaml.cs
private ActivationService CreateActivationService()
{​
return new ActivationService(this, typeof(LoginPage));​
}​
​
private UIElement CreateShell()​
{​
return new Views.ShellPage();​
}
When login successfully,if you want to show the navigation view,you can set the ShellPage to the content of current window.
Window.Current.Content = new Views.ShellPage();

Xamarin forms: Windows 10 Universal App prevent screenshot

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

Xamarin Forms Update ListView on WillEnterForeground

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.

Issue with opening Child window from Main window using WPF with MVVM

I am learning WPF with M-V-VM. And I am using ICommand, RelayCommand.
I have several Views, Models, and ViewModels.
The MainWIndowView open upon on application start. The MainWindowView has a button that opens another WPF window called “FileListview” via MainWindowViewModel.
The FileListView has a button “View Lookup”, which supposed to open another WPF window called “LookupView” via FileListViewModel. But I could not make this button to work unless I specify FileListView in App.xaml.cs instead of MainWIndowView. I could not understand why “View Lookup” button work if I make application to start from “FileListView” . I also don’t understand whether I need model for MainWindowView, and FileListView since I don’t have anything going except one view’s button is opening another view.
On code behind file “App.xaml.cs” I have
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
WPFProject. MainWIndowView window = new MainWIndowView ();
MainWIndowViewModel VM = new MainWIndowViewModel ();
window.DataContext = VM;
window.Show();
}
}
I would appreciate if somebody can point me to good article or sample code using WPF with M-V-VM that reflect my issue.
Here is my approach to use dialogs/child windows with mvvm and wpf. please note the comment from sllev and post all relevant code.
After rethinking the issue, I was able to figure out the solution.
The cause of the issue: I was not associating View with it’s ViewModel class.
So I put the following code in code behind of FileListView.xaml.cs.
public partial class FileListView: Window
{
private FileListViewModel _ fileListViewModel = new FileListViewModel ();
public FileListViewModel ()
{
InitializeComponent();
base.DataContext = _fileListViewModel;
}
}
Thank you

Categories