how to set icon for Toolbar Item in Xamarin forms - xamarin.forms

My xamarin andorid project does not have Drawawble folders under Resources, I have mipmap folders instead. I'm trying to set icon for a toolbar item in shared project. If I set an image as embeded resource I should be able to access it from the shared project, am I wrong?
<?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="BoringAppUi.MainPage" Title="Main Page">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Logout" Clicked="OnLogoutButtonClicked" Order="Primary" Priority="0"/>
<ToolbarItem Text="Home" Icon="#mipmap/baseline_home_blue_48.png" Clicked="OnHomeIconClicked" Order="Primary" Priority="1"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<Label Text="Main app content goes here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Also I've tried setting it from the code behind
var toolbarItemHome = new ToolbarItem
{
Text = "Home",
Icon = "#mipmap/baseline_home_blue_48.png"
};
toolbarItemHome.Clicked += OnHomeIconClicked;
ToolbarItems.Add(toolbarItemHome);

From c# code:
new ToolbarItem () { Icon = "icon.png"}
Or from xaml:
<ToolbarItem Name="iconexample" Icon="icon.png" Priority="0" Order="Primary" Activated="Onclick" />
Have that in mind:
iOS - place the image at /mipmap/icon.png
Android - place the image at /mipmap/drawable/icon.png

Related

A part of tabbedPage is sheltered in IOS

Just a simple project of maui,it use tabbar shell
------AppShell-----
<Shell
x:Class="TABSample.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TABSample"
Shell.FlyoutBehavior="Disabled">
<TabBar>
<Tab Title="Test">
<ShellContent Title="Test1" ContentTemplate="{DataTemplate local:NewPage1}" />
<ShellContent Title="Test2" ContentTemplate="{DataTemplate local:NewPage2}" />
</Tab>
</TabBar>
</Shell>
-----NewPage1---------------
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TABSample.NewPage1"
Title="NewPage1">
<StackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
-----------Iphone 14 pro IOS 16.2------------------
enter image description here
cannot see the label text
----------Android--------------------------
enter image description here
A part of tabbedPage is sheltered in IOS, the reason is iOS Simulator? Screen content is only shown in a part of screen in iOS Simulator
but ohter page is ok
Yes I could reproduce your issue. Just as #Bas H mentioned, the control is behind the tabs. I also found the same issue in Github: Contentpage is cut off in iOS #8778. You could follow it.
For a workaround, you could set the page padding
protected override void OnAppearing()
{
base.OnAppearing();
Padding = new Thickness(0, 40, 0, 0);
}
or set like #Bas H suggested but i think use Margin is better:
<VerticalStackLayout Margin="{x:OnPlatform iOS='0,40,0,0', Android='0'}" >

My toolbar buttons are disappearing after two entries

I have successfully created my app and am finally ready to start testing before continuing with designs, however, while testing I find that after using my toolbar buttons twice it just disappears for no reason.
Here is the Xaml code for the item:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:ODDJOBS.Views"
x:Class="ODDJOBS.Views.TabbedAppPage">
<TabbedPage.ToolbarItems>
<ToolbarItem Text="ODDJOBS" Priority="1"></ToolbarItem>
<ToolbarItem Text="+" Priority="2" Command="{Binding NavigateToAddPageCommand}">
</ToolbarItem>
</TabbedPage.ToolbarItems>
<!--Pages can be added as references or inline-->
<pages:NextPage Title="Jobs"/>
<pages:ServiceView Title="Services"/>
</TabbedPage>

How can I add a frame as a toolbar item in toolbar xamarin forms

Image:
I would like to add a frame to toolbar in Xamarin forms. But toolbar is not accepting.
You cannot add a frame as a Toolbar Item.
You can use, for example the TitleView and customize the whole NavigationView yourself.
For example:
<?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:TitleViewSample"
x:Class="TitleViewSample.MainPage">
<NavigationPage.TitleView>
<StackLayout Orientation="Horizontal">
<Label Text="Hello World" HorizontalOptions="Center"/>
<Frame BackgroundColor="Yellow" HorizontalOptions="End"/>
</StackLayout>
</NavigationPage.TitleView>
</ContentPage>

Xamarin.Forms: How to hide the Navigation Bar Separator on iOS devices?

I have changed the default color of the navigation bar by using these codes in app.xaml as I am unable to make it transparent on iOS devices:
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="#0a82b8" />
<Setter Property="BarTextColor" Value="#ffffff" />
</Style>
Now, there is an unnecessary navigation bar separator on iOS:
On Microsoft's official website, it says these codes can be helpful:
This platform-specific hides the separator line and shadow that is at
the bottom of the navigation bar on a NavigationPage. It's consumed in
XAML by setting the NavigationPage.HideNavigationBarSeparator bindable
property to false:
<NavigationPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:NavigationPage.HideNavigationBarSeparator="true">
</NavigationPage>
Alternatively, it can be consumed from C# using the fluent API:
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
public class iOSTitleViewNavigationPageCS : Xamarin.Forms.NavigationPage
{
public iOSTitleViewNavigationPageCS()
{
On<iOS>().SetHideNavigationBarSeparator(true);
}
}
Source: Hiding the Navigation Bar Separator on a NavigationPage
The problem is that when I want to paste the ios:NavigationPage.HideNavigationBarSeparator="true" property into the <NavigationPage>tag, it gives this error:
No property, bindable property, or event found for 'HideNavigationBarSeparator', or mismatching type between value and property.
My full codes are:
<?xml version="1.0" encoding="utf-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:My.App" x:Class="My.App.MainPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu" BackgroundColor="#0a82b8" Icon="menu.png">
<StackLayout Orientation="Vertical">
<ListView x:Name="navigationDrawerList" RowHeight="55" SeparatorVisibility="None" BackgroundColor="#ffffff" ItemSelected="OnMenuItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<!-- Main design for our menu items -->
<StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="20,10,0,10" Spacing="20">
<Label Text="{Binding Title}" FontSize="Large" VerticalOptions="Start" HorizontalOptions="CenterAndExpand" TextColor="#28DDFF" FontAttributes="Bold" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:NavigationPage.HideNavigationBarSeparator="true">
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
Here is the tutorial which I followed for the MasterDetail navigation. The file which has <NavigationPage> tag is named as MainPage.xaml
You can use a custom renderer to hide that shadow:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationPage))]
namespace CustomNavigationPage.iOS
{
public class CustomNavigationPage : NavigationRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
NavigationBar.SetBackgroundImage(new UIKit.UIImage(), UIKit.UIBarMetrics.Default);
NavigationBar.ShadowImage = new UIKit.UIImage();
}
}
}
While you have accepted the answer, this one is more precise - you haven't followed the instructions that you have pasted from the site as you have ommited this line:
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
Place UINavigationBar.Appearance.ShadowImage = new UIImage(); inside the public override bool FinishedLaunching(UIApplication app, NSDictionary options) method within AppDelegate.cs file. AppDelegate.cs resides within the iOS project of Xamarin.Forms

Xamarin.Forms How do we remove a child from a TabbedPage's children?

I would like to add and remove child pages from a tabbed page in certain conditions.
However, it gives me that collection "Children" is read only .
Has anyone succeeded ?
Here is the code for TabbedPage
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:partialViews="clr-namespace:TestTabbed.Views.PartialViews;assembly=TestTabbed"
prism:ViewModelLocator.AutowireViewModel="False"
x:Class="TestTabbed.Views.MainPage"
ItemsSource="{Binding ChannelLists}" Appearing="MainPage_OnAppearing"
Disappearing="MainPage_OnDisappearing">
<TabbedPage.ItemTemplate>
<DataTemplate>
<ContentPage Title="{Binding Name}" x:Name="mainContentPage">
<ContentPage.Content>
<OnIdiom x:TypeArguments="View">
<OnIdiom.Phone>
<partialViews:MainPhoneView />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<partialViews:MainTabletView />
</OnIdiom.Tablet>
</OnIdiom>
</ContentPage.Content>
</ContentPage>
</DataTemplate>
</TabbedPage.ItemTemplate>
</TabbedPage>
Seeing your code might be helpful in case I am missing the actual issue but the following should work for you (note that you cannot directly assign a collection to the TabbedPage.Children property which is what it sounds like you are running into):
TabbedPage tab = new TabbedPage();
ContentPage page = new ContentPage();
tab.Children.Add(page);
tab.Children.Remove(page);
Where as the following will not work:
TabbedPage tab = new TabbedPage();
tab.Children = new List<Page>();

Resources