How can I create an spinning picker in Xamarin? - xamarin

I am trying to get my picker to be an wheel/spinning type.
My Current Picker:
<Picker x:Name="AmountPicker" />
AmountPicker.ItemsSource = new List<string>() { "1", "2", "3","4","5","6","7","8" };
My Picker

You have a couple of options.
1. Use Custom Renderers which enables you to achieve the wheel/spinning type. There is a similar thread you could refer to: Apply styles on Picker items in Xamarin Forms.
1.1 Create the Custom Picker Control in Shared Project:
public class MyPicker : Picker
{
}
1.2 Creating the Custom Renderer on Android & iOS:
Android:
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace App35.Droid
{
class MyPickerRenderer : PickerRenderer
{
public MyPickerRenderer(Context context) : base(context)
{
}
}
}
iOS:
[assembly: ExportRenderer(typeof(MyPicker), typeof(MyPickerRenderer))]
namespace App35.iOS
{
class MyPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
}
}
}
1.3 Consume it in XAML:
<local:MyPicker ItemsSource="{Binding MyListProperty}" ></local:MyPicker>
Output:
2. Use Xamarin.SfPicker. Firstly, install the package and then set SfPicker control namespace as xmlns:syncfusion="clr- namespace:Syncfusion.SfPicker.XForms;assembly=Syncfusion.SfPicker.XForms in XAML Content page. So you can consume it in XAML like below.For more you can refer to Getting Started with Xamarin Picker (SfPicker)
<syncfusion:SfPicker x:Name="picker" HeaderText="Choose Value" />
3. Use Wheel Picker for Xamarin Samples package, you can refer to Wheel Picker for Xamarin Samples for more details.

Related

How can I create a Custom Renderer for a ContentView in Xamarin.Forms MacOS?

We're using Xamarin.Forms with MacOS, and have a custom view MyCustomView : Xamarin.Forms.ContentView, and I'm trying to create a custom view renderer for our view, but it's interfering with the rendered view.
Does anyone know how to create a view renderer in my platform project?
This is the code I've tried so far, looking at similar places:
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomView), typeof(MyCustomViewRenderer))]
namespace Mac.Renderers
{
public class MyCustomViewRenderer : ViewRenderer<Xamarin.Forms.ContentView, AppKit.NSView>
{
public MyCustomViewRenderer()
{
// My implementation
}
}
}
Event when the implementation is left blank, having this custom renderer is affecting the display of the ContentView, so I think this code must not be right - is there a way to do this?
You could fix your issue by changing your class to inherit from VisualElementRenderer<T> instead.
[assembly: Xamarin.Forms.ExportRenderer(typeof(MyCustomView), typeof(MyCustomViewRenderer))]
namespace Mac.Renderers
{
public class MyCustomViewRenderer : VisualElementRenderer<ContentView>
{
public MyCustomViewRenderer()
{
// My implementation
}
}
}
Page has a default Renderer "PageRenderer", do not understand why ContentView does not. It would be nice if there was a ContentViewRenderer.
Hope this helps.-

How to programmatically associate a style to a button in Xamarin?

In my Xamarin app, I create a button (Xamarin.Forms.Button) programmatically. I need this button to show a different background image under normal vs hovered state. I have created a style resource similar to what is described at How to indicate currently selected control in Xamarin?. However, I cannot figure out how to apply this style to the button.
The Button class exposes a property called Image that is of FileImageSource type. The closest API I found to load my style resource is ImageSource.FromResource static method. However, this method seems to return StreamImageSource instance which is not what we need.
Class Button does not seem to provide any Style property.
Can you please suggest how I can programmatically associate a style to the button? Regards.
To achieve this request you need custom renderers.
To be able to apply your style f.e.: "myButtonStyle.xml" you have to create a custom renderer for your target platform:
Android:
[assembly: ExportRenderer (typeof (YourExtendedButtonClass), typeof (MyCustomButtonRenderer))]
namespace YourApp.Droid
{
public class MyCustomButtonRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
var myButton = this.Control as Android.Widget.Button;
myButton?.SetBackgroundResource(Resource.Drawable.myButtonStyle);
}
}
}

Bindable Picker in Xamarin

Is the Picker Control available in Xamarin bindable? If so, could someone help on how to use it ? I would like to bind a Picker control with data (XAML approach) that comes from a DB.
The bindable Picker is available since 13th January 2017. Currently, it is contained in version 2.3.4.184-pre1.
If you want to use it, you have to install Xamarin.Forms via nuget using the -Pre flag. Or check the Prerelease checkbox in the nuget UI.
Install-Package Xamarin.Forms -Pre
And then, you can just bind your collection to ItemsSource.
<Picker
Title="Select a Color"
ItemsSource="{Binding Colors}" />
Announcement: https://blog.xamarin.com/new-xamarin-forms-pre-release-2-3-4-pre1-quality-improvements-bindable-picker
It will be released as stable not later than February 2017 (according to the Roadmap)
Yes, You can us the Picker control in Xamarin Forms.
Please check this link for the description: https://developer.xamarin.com/api/type/Xamarin.Forms.Picker/
Thank You.
Example #1 :
Use Extendedpicker of xlabs following is link to implement extended picker
Cs code of extended picker
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms/Controls/ExtendedPicker.cs
Implementation in xaml page:-
https://github.com/XLabs/Xamarin-Forms-Labs/blob/7a58dc349f17351813afa24df97ef7fea545a833/samples/XLabs.Samples/XLabs.Samples/Pages/Controls/ExtendedPickerPage.xaml
Example #2:
Try to create own bendable picker refer following link
https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker
Here is the complete solution:
https://hiranpeiris.com/2017/02/24/how-to-add-a-custom-bindable-property-to-xamarin-forms-control/
Create your custom picker class.
using System.Collections.Generic;
using Xamarin.Forms;
namespace FDNet
{
public class OutletPicker : Picker
{
public static readonly BindableProperty ItemSourceProperty = BindableProperty.Create(nameof(ItemSource), typeof(List<string>), typeof(OutletPicker), null);
public List<string> ItemSource
{
get
{
return (List<string>)GetValue(ItemSourceProperty);
}
set
{
SetValue(ItemSourceProperty, value);
}
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(ItemSource))
{
this.Items.Clear();
if (ItemSource != null)
{
foreach (var item in ItemSource)
{
this.Items.Add(item);
}
}
}
}
}
}
Add XAML reference variable to the Page.
xmlns:local=“clr–namespace:FDNet;assembly=FDNet“
Add the control and bind the property.
<local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
Now you can see our custom bindable property ItemSource=“{Binding Outlets}“)
Demo.
Outlets = new List<string> { “4G LTE“, “4G Broadband“, “Fiber connection“ };
<local:OutletPicker Title=“Select“ ItemSource=“{Binding Outlets}“ HorizontalOptions=“Center“ WidthRequest=“300“ />
(1)bind picker :
Dictionary dicobj= new Dictionary();
dicobj.Add(1, "abc");
dicobj.Add(2, "xyz");
foreach (var item in dicobj.Values)
{
YorPickerName.Items.Add(item);
}
(2)then get the selected value on SelectedIndexChanged event of picker:
var selval = Service_category.SelectedIndex;
int value = dicobj.ElementAt(selval).Key;
var data = Service_category.Items[selval];
int id = dicval.FirstOrDefault(x => x.Value == data).Key;

Picker text goes out of picker box

I am working in Xamarin.Forms where I have a picker like this:
<Picker Grid.Row="1" Grid.Column="1" x:Name="pickerForSearchMode"
Title="Search Mode" BackgroundColor="White" >
<Picker.Items>
<x:String>Mode 1</x:String>
<x:String>Mode 2</x:String>
<x:String>Mode 3</x:String>
<x:String>Mode 4</x:String>
</Picker.Items>
It displays perfect on iOS and Android, but in a UWP app it has an issue. The text is displaying outside of the picker like this:
You will need to use a Custom Renderer, see the official Xamarin documentation.
Once the element is rendered, you will need to use the value of the Title property and set it as the PlaceholderText of the underlying UWP ComboBox.
Check the official renderer here which is in Xamarin Forms, you can see that the UpdateTitle method sets the Header property of the ComboBox. All you will need to do is to replace it with the PlaceholderText property in your own renderer.
Required steps
First you create a custom Picker in your PCL project:
public class PlaceholderPicker : Picker
{
}
Replace your Picker reference in XAML with the PlaceholderPicker:
<local:PlaceholderPicker VerticalOptions="Center" x:Name="PickerForSearchMode"
Title="Search Mode" BackgroundColor="White" >
<Picker.Items>
<x:String>Mode 1</x:String>
<x:String>Mode 2</x:String>
<x:String>Mode 3</x:String>
<x:String>Mode 4</x:String>
</Picker.Items>
</local:PlaceholderPicker>
You will have to add a namespace import to the page element - xmlns:local="clr-namespace:NamespaceWithTheCustomControl".
Now in the UWP project you now create a custom renderer for the control and add a ExportRenderer attribute for the assembly to indicate Xamarin Forms to use this renderer in UWP.
[assembly: ExportRenderer(
typeof( PlaceholderPicker ),
typeof( PlaceholderPickerRenderer ) )]
namespace FormsApp.UWP
{
public class PlaceholderPickerRenderer : PickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.PlaceholderText = Element.Title;
Control.Header = null;
}
}
}
}
Note that we first set the PlaceholderText and then clear the Header, which was already set by the default renderer.
I have created a sample solution with this here on my GitHub, you can clone it and try it out yourself :-) .

How to remove the border or border colour of the SearchBar?

I am new to Xamarin.forms. I am using a SearchBar followed by a BoxView.
The problem that I am facing is removing the border line of the SearchBar.
This border makes the UI appear as if there is a separator in between the SearchBar and the BoxView.
Any help in achieving this is much appreciated.
Thanks much :)
Think you might need a custom renderer for this like so:
using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ExportRenderer( typeof(NamespaceOfApp.MySearchBar), typeof(NamespaceOfApp.iOS.SearchBarWithNoBarRenderer_iOS))]
namespace NamespaceOfApp.iOS
{
public class SearchBarWithNoBarRenderer_iOS : SearchBarRenderer
{
protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
{
base.OnElementChanged( args );
UISearchBar bar = (UISearchBar)this.Control;
//set background to empty image
bar.SetBackgroundImage (new UIImage (), UIBarPosition.TopAttached, UIBarMetrics.Default);
}
}
}
If you need a hand with custom renderers look here
Effects was introduced in Forms v2 as the recommended way to extending the Forms base controls. https://developer.xamarin.com/guides/xamarin-forms/effects/
For example; this would be your iOS Effect code...
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ResolutionGroupName ("MyCompany")]
[assembly:ExportEffect (typeof(FocusEffect), "FocusEffect")]
namespace EffectsDemo.iOS
{
public class FocusEffect : PlatformEffect
{
protected override void OnAttached ()
{
// your code here
}
protected override void OnDetached ()
{
// your code here
}
protected override void OnElementPropertyChanged (PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged (args);
// your code here
}
}
}
Custom Renderers were the only option in Forms v1.x. Custom Renderers are great for building controls from scratch but it was a bit hit and miss when extending the built-in controls.
Using StoryBoard:
Using code:
public override void ViewWillLayoutSubviews ()
{
base.ViewWillLayoutSubviews ();
sampleSearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
sampleSearchBar.BarTintColor = UIColor.Clear;
}

Resources