is it possible?
I would like to remove the shadow of the buttons on Xamarin Forms.
Thanks
For delete shadow on button Android you just need create a renderer in project Droid and set BackroundColor with transparent or other color.
For a project using PCL :
[assembly: ExportRenderer(typeof(Button),typeof(FlatButtonRenderer))]
namespace Project.Droid
{
public class FlatButtonRenderer : ButtonRenderer
{
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
}
}
In XAML :
<Button BackgroundColor="Transparent" Text="ClickMe"/>
Explaining with more detail.
using Android.App;
using Android.Content.PM;
using Android.OS;
using Xamarin.Forms.Platform.Android;
using ProjectName.Droid;
using Xamarin.Forms;
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(FlatButtonRenderer))]
namespace ProjectName.Droid
{
public class FlatButtonRenderer : ButtonRenderer
{
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
}
}
#Tonatio in your renderer instead of using
[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(FlatButtonRenderer))]
use this
[assembly: ExportRenderer(typeof(YourCustomButton), typeof(FlatButtonRenderer))]
you will have to make a CustomButton control that inherits from Xamarin.Forms.Button and then use that custom button in your xaml instead of regular button. that should do the thing
//Add references to your custom control
xmlns:controls="clr-namespace:YourNameSpace.Controls"
//Use control
<Grid>
<controls:YourCustomButton x:Name="_customButton"/>
</Grid>
Feel free to drop by if you need some more help.
There's a simple hack around this. Simply wrap the button in a Frame element, with HasShadow set to False
<Frame HasShadow="False" Padding="0" Margin="0">
<Button Padding="15"
Text="Log in"
TextColor="Blue"
FontSize="17" />
</Frame>
This will remove the button's shadow 😉
Related
Is there anyway to changed the font size of the ContentPage? enter image description here
Example is that, the last Tab. SUMMARY is cut and I want to set the font size to small.
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test.ViewModels"
xmlns:model="clr-namespace:Test.Models"
Shell.TabBarBackgroundColor="Transparent"
x:Class="Test.Views.MainPage">
<ContentPage Title="Delivery">
</ContentPage>
<ContentPage Title="Pending">
</ContentPage>
<ContentPage Title="Success">
</ContentPage>
<ContentPage Title="Failed">
</ContentPage>
<ContentPage Title="Summary">
</ContentPage>
</TabbedPage>
I tried adding this:
<NavigationPage.TitleView>
<Label Text="This is my Title" FontSize="12" TextColor="White" />
</NavigationPage.TitleView>
But doesn't do anything.
I was able to reproduce the tab title as the screenshot you privided. You could try the code below to set the TabMode, TabGravity in custom renderer.
[assembly: ExportRenderer(typeof(TabbedPage),
typeof(CustomTabbedPageRenderer))]
namespace App15.Droid
{
class CustomTabbedPageRenderer : TabbedPageRenderer
{
private TabLayout tabLayout = null;
public CustomTabbedPageRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
{
base.OnElementChanged(e);
this.tabLayout = (TabLayout)this.GetChildAt(1);
tabLayout.TabMode = TabLayout.ModeScrollable;
tabLayout.TabGravity = TabLayout.GravityFill;
}
}
}
Normally, we could set the TextSize in tabbed page custom renderer. But sometimes we would miss the different types if TextView. So i think the way above would be better. You could also check the way to set the TextSize in custom renderer.
Tabbed page custom renderer on Xamarin.Forms
When TabbedPage use android:TabbedPage.ToolbarPlacement="Bottom" to set the tabs in the bottom, the renderer above does not work. You could refer to teh solution in the link below.
How to change Tabbar text size when using TabbedPage.ToolbarPlacement="Bottom" in Xamarin Android?
I have a Xamarin Forms app in which the customer really wanted to use the iOS 'scrolling alphabet index' that's not supported on Android
I have therefore two UIs for the platforms, but I am now trying to change the default colour of the text on iOS
I've looked into how to do this and it appears to require customising TableView on iOS platform
How change iOS TableView index Color?
tableView.sectionIndexColor = UIColor.red
tableView.sectionIndexBackgroundColor = UIColor.clear
My question is how do I then consume that in the view? Because the only code required to have it work on ioS can be shown simply by the Xaml
<ListView Grid.Row="1"
x:Name="ItemList"
IsGroupingEnabled="True"
ItemsSource="{Binding allItems}"
GroupDisplayBinding="{Binding Item}"
GroupShortNameBinding="{Binding IndexLetter}"
ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement"
BackgroundColor="Black"
HasUnevenRows="True">
This is a list view, not a table view, so where in the Xamarin Forms could would I reference the Custom Table View Renderer required to alter the colour of the text on iOS?
You could implement it by using Custom Renderer .
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using xxx.iOS;
[assembly:ExportRenderer(typeof(ListView),typeof(MyTableViewRenderer))]
namespace xxx.iOS
{
public class MyTableViewRenderer:ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
Control.SectionIndexColor = UIColor.Red;
Control.SectionIndexBackgroundColor = UIColor.Clear;
}
}
}
}
We have managed to wire a child content page of a tab page to its own viewmodel in our Prism Xamarin Forms app. This works if ContactsScreenViewModel has a parameterless constructor. If we inject the NavigationService into the constructor the code doesn't compile. Does anyone know how we can solve this problem?
Visual Studio is reporting that a parameterless constructor is required
<?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:views="clr-namespace:TabsTest.Views"
xmlns:viewmodels="clr-namespace:TabsTest.ViewModels"
x:Class="TabsTest.Views.MainPage"
Title="{Binding Title}"
UnselectedTabColor ="Gray"
SelectedTabColor="Green"
BarBackgroundColor="LightGray">
<views:ContactsScreen BackgroundColor="White" Title="Contacts">
<views:ContactsScreen.BindingContext>
<viewmodels:ContactsScreenViewModel/>
</views:ContactsScreen.BindingContext>
<ContentPage.ToolbarItems>
<ToolbarItem Order="Primary"
Priority="1" >
</ToolbarItem>
<ToolbarItem Order="Primary"
Priority="0" >
</ToolbarItem>
</ContentPage.ToolbarItems>
</views:ContactsScreen>
</TabbedPage>
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Text;
namespace TabsTest.ViewModels
{
public class ContactsScreenViewModel
{
private INavigationService _navigationService { get; }
public ContactsScreenViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
}
}
<viewmodels:ContactsScreenViewModel/> is a constructor call... where do you expect the parameters to come from?
You want to either resolve the view model first (through a factory) and then attach the view to it, or make use of the ViewModelLocator to resolve the view model for you (and resolve and inject its dependencies) as described here, for example.
<TabbedPage [...]
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True">
will resolve an instance of TabbedPageViewModel and put it the BindingContext.
That one should expose an instance of ContactsScreenViewModel through a property:
<views:ContactsScreen BindingContext={Binding ThePropertyOnTabbedPageViewModel}/>
You create that instance to your liking, with all parameters and dependencies you need.
How to bind an image icon on the ToolbarItem OnPlatform tag in Xamarin.Forms
This is my code:
<ToolbarItem
Icon="{OnPlatform iOS='iconscalendarplus64.png', Android='iconscalendarplus64.png'}"
Priority="0"
Order="Primary"
Command="{Binding PrikaziCommand}"
I am trying to bind images for the Android OnPlatform, but it is not working.
Since the Android and iOS properties of the OnPlatform are not BindableProperty
They don't support Data Binding. Hence you cannot bind those Property.
As an alternative you can set the platform specific image source to a property in the ViewModel and you it instead.
XAML
<ContentPage.ToolbarItems>
<ToolbarItem IconImageSource="{Binding PlatformSpecificImage}"/>
</ContentPage.ToolbarItems>
View Model
public ImageSource PlatformSpecificImage { get; set; }
public ViewModel()
{
if(Device.RuntimePlatform == Device.Android)
{
PlatformSpecificImage = "android_image.png";
}
else
{
PlatformSpecificImage = "iOS_image.png";
}
}
Hope this could help!
The easiest way would be something like :
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS><FileImageSource File=""icon.png""/></OnPlatform.iOS>
<OnPlatform.Android><FileImageSource File=""icon.png""/></OnPlatform.Android>
<OnPlatform.WinPhone><FileImageSource File=""Images/icon.png""/></OnPlatform.WinPhone>
</OnPlatform>
</ToolbarItem.Icon>
The good thing is you can use all 4 types of ImageSource here based on platforms.
Good luck feel free to get back if you have queries
I'm trying to use prism 7.1 AutoWirePartialView to bind a PartialView to its viewModel. However, binding is not working, or at least, setting the viewModel to the PartialView does not seem to work, it still has the page's BindingContext as BindingContext.
There is my Page :
<?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="Project.Core.Views.NotConnectedViews.ForecastDemoPage"
xmlns:carouselForecast="clr-namespace:Project.Core.Views.MainViews"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
x:Name="ForecastDemo"
BackgroundColor="{StaticResource PrimaryColorOne}" ControlTemplate="{StaticResource MainAppTemplate}">
<ContentPage.ToolbarItems>
<ToolbarItem Name="SearchForecast" Command="{Binding ShowSearchForecastDemoCommand}" Order="Primary" Icon="ic_search_white_24dp.png" Priority="0" />
</ContentPage.ToolbarItems>
<ContentView x:Name="ContentViewForecast" ControlTemplate="{StaticResource ForecastTownControlTemplate}">
<carouselForecast:ForecastPartialViewCarousel prism:ViewModelLocator.AutowirePartialView="{x:Reference ForecastDemo}"></carouselForecast:ForecastPartialViewCarousel>
</ContentView>
</ContentPage>
Binding: 'DayWeatherForecasts' property not found on
'Project.Core.ViewModels.ForecastDemoPageViewModel', target property:
'Project.Core.Views.MainViews.ForecastPartialViewCarousel.ItemsSource'
As you can see, I'm using the partial view as a ContentPresenter for a ContentView that uses a ControlTemplate.
There is my PartialView :
<carousel:CarouselViewControl x:Name="carouselView"
Position="{Binding CarouselPosition}"
PositionSelectedCommand="{Binding PositionChanged}"
Orientation="Horizontal" AnimateTransition="True" IsSwipeEnabled="False"
ItemsSource="{Binding DayWeatherForecasts}" InterPageSpacing="10"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:carousel="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
x:Class="Project.Core.Views.MainViews.ForecastPartialViewCarousel">
<!-- Item template is defined here, removed for readability -->
</carousel:CarouselViewControl>
And this is my PartialView ViewModel :
namespace Project.Core.ViewModels
{
public class ForecastPartialViewCarouselViewModel : ViewModelBase
{
public ForecastPartialViewCarouselViewModel(IForecastService forecastService,
INavigationService navigationService) : base(navigationService)
{
InitStubForecasts();
}
private ObservableCollection<DayWeatherForecast> _dayWeatherForecasts;
public ObservableCollection<DayWeatherForecast> DayWeatherForecasts
{
get => _dayWeatherForecasts;
set => SetProperty(ref _dayWeatherForecasts, value);
}
}
}
Of course DayWeatherForecasts is set with some stub values. I simplified the viewModel for readability purpose.
I'm not using prism AutoWiring viewModel, so in app.xaml.cs :
containerRegistry.RegisterForNavigation<ForecastDemoPage, ForecastDemoPageViewModel>();
Question : Could it be that my PartialViewModel is in the ViewModels folder and that the Partialview I want to be be bound to this ViewModel is under a subfolder MainViews ? Should I create a MainViewsViewModel folder and put my viewModel there ?
EDIT : I tried this solution, but as I expected it does nothing.
If not, then I don't know why it doesnt work ...
Thanks !
Ok so I finally found out that its not enough to put this to my PartialView
prism:ViewModelLocator.AutowirePartialView="{x:Reference ForecastDemo}
As I organized my views in subfolders, prism cannot register alone my ViewModel and my PartialView.
So what I needed is to register manually the ViewModel with the PartialView using ViewModelLocationProvider
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
ViewModelLocationProvider.Register<ForecastPartialViewCarousel,
ForecastPartialViewCarouselViewModel>();
}
It's not only a matter of name, but of namespace too. If I wanted the PartialView to have the correct ViewModel set w/o registering it manually, I should have put my PartialView in the Views root folder and the corresponding ViewModel in the ViewModels root folder (with naming convention)