Is there any way to remove the Navigation Bar from Xamarin.Forms - Portable (xaml) in Android?
I want to remove the "less than sign" ('<') and the application icon which appears above the content page of the Xamarin.Forms xaml.
You can remove navigation bar from Xaml using Xamarin.Forms using below code.
NavigationPage.SetHasNavigationBar (this, false);
Where this stands for current page / form instance.
Hope this helps!
NavigationPage.SetHasNavigationBar(this, false);
The above mentioned is not the good solution.
By using this code it disable the NavigationBar present in the page.
We can achieve the real solution only by creating a NavigationRenderer for NavigationPage for Android.
void RemoveAppIconFromActionBar()
{
var actionBar = ((Activity)Context).ActionBar;
actionBar.SetIcon (new ColorDrawable (Color.Transparent.ToAndroid ()));
}
Refer the Github for the complete code snippet : https://gist.github.com/Vaikesh/f86d1968c8166519f102#file-customnavigationrenderer-cs
It's called the "back button" and it is available in the action bar. you can remove it using:
NavigationPage.SetHasBackButton(this, false);
The easiest way to achieve this is to add NavigationPage.HasNavigationBar = "false" in your ContentPage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SterlingSwitch.Pages.Page1"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout>
<Label Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
The Best way to achieve this from xml page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ICLDC.Digital.General.Pages.AboutApp.AboutApplication"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
xmlns:local="clr-namespace:ICLDC.Digital.General.Pages.Generic"
xmlns:translate="clr-namespace:ICLDC.Digital.General.Helpers"
ios:Page.UseSafeArea="True"
NavigationPage.HasNavigationBar="False">
<ContentPage.Content>
<StackLayout
BackgroundColor="White"
HorizontalOptions="FillAndExpand"
Spacing="0"
VerticalOptions="FillAndExpand"/>
</ContentPage.Content>
</ContentPage>
Related
I am implementing an APP with Xamarin using MasterDetail page
However in iOS encountered a black screen issue.
Here is example.
If I don't put a detail page in Xaml file like below.
<MasterDetailPage>
<MasterDetailPage.Master>
<ContentPage Title = "Menu">
<ScrollView>
...
</ScrollView>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:xxx_Page />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
During runtime. If I called below
Detail = new NavigationPage(page);
In iOS system. The phone enters a black screen
In Android. It works fine.
Is there any reason?
MasterDetailPage is obsolete. You can use FlyoutPage instead.
I used FlyoutPage to refer to your code and simply tested it on iOS without any problems, hope it can help you:
For .xaml file:
<FlyoutPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FlyoutPageNavigation;assembly=FlyoutPageNavigation"
x:Class="FlyoutPageNavigation.MainPage">
<FlyoutPage.Flyout>
<ContentPage Title="Menu">
<ScrollView>
<StackLayout>
<Label Text="One"/>
<Label Text="Two"/>
<Label Text="Three"/>
</StackLayout>
</ScrollView>
</ContentPage>
</FlyoutPage.Flyout>
For .cs file:
using System;
using Xamarin.Forms;
namespace FlyoutPageNavigation
{
public partial class MainPage : FlyoutPage
{
public MainPage()
{
InitializeComponent();
Detail=new NavigationPage(new ContactsPage());
}
}
}
For more usage of Flyout, you can refer to the documentation: Xamarin.Forms FlyoutPage | Microsoft
Tis is the xml file
<ContentPage Title="Teras">
<ContentPage.Content>
<ScrollView>
<FlexLayout x:Name="teras">
<Label Text="Teras"/>
</FlexLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
see i already have FlexLayout
i want to add button with code but i cant include the button to this layout :/ im new on xamarin
var button = new Button { .. };
teras.Children.Add(button);
I have a Page with a StackLayout using BindableLayout.ItemsSource, inside each item I have a ListView, for each item in this nested ListView I need to do a Binding to a property on the Page's ViewModel. I'm trying to use the Source+Path approach but the app crashes as soon as I open the page that contains this structure.
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="BindableLayoutReferenceBug.ListViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:BindableLayoutReferenceBug">
<StackLayout BindableLayout.ItemsSource="{Binding Items}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<local:MessageListViewTemplate />
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</ContentPage>
MessageListViewTemplate.xaml:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="listView">
<ListView ItemsSource="{Binding Options}">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding .}" />
<Button
BackgroundColor="Blue"
Command="{Binding Source={x:Reference Name=listView}, Path=Parent.BindingContext.ShowMessageCommand}"
CommandParameter="{Binding .}"
Text="Show Message" />
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
The exception shows that there is a problem finding the reference to the x:Name I used: Can not find the object referenced by listView
This only happens when I have a nested structure as I mentioned (StackLayout with BindableLayout > ListView). I'm not sure if this is not a supported scenario or if it is a bug.
I tried using different Paths for the binding, but since this is a problem parsing the XAML not finding the x:Name referenced, I don't think it even starts evaluating my Path.
Am I doing something wrong or is this a bug in Xamarin.Forms?
Xamarin.Forms version used: 3.6.0.293080
Repro sample: https://github.com/akamud/BindableLayoutReferenceBug
Maybe, x:Name wasn't recognized at runtime even though you have set it to your content view in XAML. You could raise an issue on GitHub.
Here I tried this binding using code behind and it works fine. You could use it as an alternative:
public MessageListViewTemplate()
{
InitializeComponent();
listView.ItemTemplate = new DataTemplate(() =>
{
ViewCell viewCell = new ViewCell();
Label label = new Label();
label.SetBinding(Label.TextProperty, new Binding("."));
Button button = new Button() { Text = "Show Message", BackgroundColor = Color.Blue };
button.SetBinding(Button.CommandProperty, new Binding("Parent.BindingContext.ShowMessageCommand", source: ContentView));
button.SetBinding(Button.CommandParameterProperty, new Binding("."));
viewCell.View = new StackLayout
{
Children =
{
label,
button
}
};
return viewCell;
});
}
XAML:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BindableLayoutReferenceBug.MessageListViewTemplate"
x:Name="ContentView">
<StackLayout>
<ListView ItemsSource="{Binding Options}" x:Name="listView" HasUnevenRows="True">
</ListView>
</StackLayout>
</ContentView>
Here, I defined the parent content view as ContentView and named ListView listView.
Please notice that the list view's data template should be a view cell. So I used a view cell to wrap the content here.
It was a bug after all, it is now fixed and the above should work: https://github.com/xamarin/Xamarin.Forms/issues/6192
I am developing a cross platform app using xaml forms and have yet to figured out how to call a ContentView ontapGesture. I am able to call ContentView on xaml forms using <local:MailRoomList/> but I cant show a view ontap or on click event.
I have tried to show view on stacklayout using StackLayoutID.Children.Add(new MailRoomList()); but thats not what I want. I want to show full contentview on content page with back button enabled.
Please advise. Thank you
MailRoomList
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xx.xxxx.xx.Views.MailRoomList">
<ContentView.Content>
<StackLayout>
<Label Text="This is my MailRoom List" />
</StackLayout>
</ContentView.Content>
</ContentView>
OnTapGesture
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="OnRegisterMail" NumberOfTapsRequired="1"/>
</Frame.GestureRecognizers>
CodeBehind
async void OnRegisterMail(object sender, EventArgs e)
{
await Navigation.PushAsync(new MailRoomList());
}
Edit:
Home Page
<?xml version="1.0" encoding="utf-8" ?>
<xf1:BottomBarPage
xmlns:xf1="clr-namespace:xx.xxxx.xx.BottomBar"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:xx.xxxx.xx.Views"
x:Class="xx.xxxx.xx.Views.Home">
<ContentPage x:Name="MailRoomPage" Title="MailRoom" Icon="mailroom.png" xf1:BottomBarPageExtensions.TabColor="Orange" xf1:BottomBarPageExtensions.BadgeCount="5">
<local:MailRoom/>
</ContentPage>
<ContentPage Title="Transport" Icon="transport.png" xf1:BottomBarPageExtensions.TabColor="Green" xf1:BottomBarPageExtensions.BadgeCount="10">
<Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Transport" FontSize="Large"/>
</ContentPage>
<ContentPage Title="Communication" Icon="communication.png" xf1:BottomBarPageExtensions.TabColor="Blue" xf1:BottomBarPageExtensions.BadgeCount="3">
<Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Communication" FontSize="Large"/>
</ContentPage>
<ContentPage Title="HSE" Icon="hse.png" xf1:BottomBarPageExtensions.TabColor="Red" xf1:BottomBarPageExtensions.BadgeCount="2">
<Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="HSE" FontSize="Large"/>
</ContentPage>
<ContentPage Title="Meeting" Icon="meeting.png" xf1:BottomBarPageExtensions.TabColor="DarkCyan" xf1:BottomBarPageExtensions.BadgeCount="5">
<Label VerticalOptions="Center" HorizontalTextAlignment="Center" Text="Meeting" FontSize="Large"/>
</ContentPage>
From homepage above I am calling my view <local:MailRoom/> which is calling another view MailRoomList. This is where I need your advice on how to call a content view from another content view.
ContentView is a View not a Page. Change your MainRoomList Xaml to this
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xx.xxxx.xx.Views.MailRoomList">
<StackLayout>
<Label Text="This is my MailRoom List" />
</StackLayout>
</ContentPage>
View's dont support navigation, they can only be navigated to if they belong to a Page which are the objects that contain the navigation infrastructure.
IF you want to mantain your MailRoomList as a ContentView, then you need to include her in some other page, and perform the navigation ( inside the tap method ) to that other page
I have created a couple of entries and buttons in XAML (no C# code yet).
Is it possible to edit or even add elements in C# code to a StackLayout which was made in XAML?
For example, having the following XAML code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ORTEC_MOBILE"
x:Class="ORTEC_MOBILE.MainPage"
BackgroundColor="#0074ff">
<RelativeLayout>
<StackLayout HorizontalOptions="FillAndExpand"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
<Label Text="Login" FontSize="36" TextColor="White" HorizontalTextAlignment="Center">
</Label>
<Label Text="Welcome back! Please log into your account." FontSize="12" TextColor="White" HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</RelativeLayout>
Is it possible to add a button to the layout using C# code?
Yes, definitely possible. When you give an element an attribute of x:Name="element" it is available in code-behind as "element". See this sample on the Xamarin site to see how they call elements in code:
https://github.com/xamarin/xamarin-forms-book-samples/tree/master/Chapter08/XamlClock
The x:Name attribute allows an object instantiated in XAML to be given a name. The rules for these names are the same as for C# variable names. Following the return of the InitializeComponent call in the constructor, the code-behind file can refer to these names to access the corresponding XAML element. The names are actually converted by the XAML parser into private fields in the generated partial class.
It is definately possible to add in controls programatically from the code behind, I'll give you an example below.
It's probably useful to give the component you wish to add a control to, a name, that way it can be accessed from the class behind the xaml.
So in this instance I've simply attributed a name to your xaml stack layout.
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ORTEC_MOBILE"
x:Class="ORTEC_MOBILE.MainPage"
BackgroundColor="#0074ff">
<RelativeLayout>
<StackLayout x:Name="mainStackLayout" HorizontalOptions="FillAndExpand"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
<Label Text="Login" FontSize="36" TextColor="White" HorizontalTextAlignment="Center">
</Label>
<Label Text="Welcome back! Please log into your account." FontSize="12" TextColor="White" HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</RelativeLayout>
C# Backing Class:
Public void SomeMethodThatsInMyBackingClass()
{
Label newLabel = new Label {
Text = "Hello, Forms!",
FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)),
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
mainStackLayout.Children.Add(newLabel);
}
Using the above as a rough example should be enough for you to replicate it in your own project. Good luck.