Replaced custom BindablePicker with the updated Xamarin.Forms Picker control.
This commit is contained in:
parent
efb858f24e
commit
79c46984bb
@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace eShopOnContainers.Core.Controls
|
||||
{
|
||||
public class BindablePicker : Picker
|
||||
{
|
||||
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource",
|
||||
typeof(IEnumerable), typeof(BindablePicker), null, propertyChanged: OnItemsSourceChanged);
|
||||
|
||||
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem",
|
||||
typeof(object), typeof(BindablePicker), null, BindingMode.TwoWay, propertyChanged: OnSelectedItemChanged);
|
||||
|
||||
public static readonly BindableProperty ItemSelectedCommandProperty = BindableProperty.Create("ItemSelectedCommand",
|
||||
typeof(ICommand), typeof(BindablePicker), null);
|
||||
|
||||
public BindablePicker()
|
||||
{
|
||||
SelectedIndexChanged += (o, e) =>
|
||||
{
|
||||
if (SelectedIndex < 0 || ItemsSource == null || !ItemsSource.GetEnumerator().MoveNext())
|
||||
{
|
||||
SelectedItem = null;
|
||||
return;
|
||||
}
|
||||
|
||||
var index = 0;
|
||||
|
||||
foreach (var item in ItemsSource)
|
||||
{
|
||||
if (index == SelectedIndex)
|
||||
{
|
||||
SelectedItem = item;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public IEnumerable ItemsSource
|
||||
{
|
||||
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
public Object SelectedItem
|
||||
{
|
||||
get { return GetValue(SelectedItemProperty); }
|
||||
set
|
||||
{
|
||||
if (SelectedItem != value)
|
||||
{
|
||||
SetValue(SelectedItemProperty, value);
|
||||
InternalUpdateSelectedIndex();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand ItemSelectedCommand
|
||||
{
|
||||
get { return (ICommand)GetValue(ItemSelectedCommandProperty); }
|
||||
set { SetValue(ItemSelectedCommandProperty, value); }
|
||||
}
|
||||
|
||||
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
|
||||
|
||||
private void InternalUpdateSelectedIndex()
|
||||
{
|
||||
var selectedIndex = -1;
|
||||
if (ItemsSource != null)
|
||||
{
|
||||
var index = 0;
|
||||
|
||||
foreach (var item in ItemsSource)
|
||||
{
|
||||
string strItem = item?.ToString();
|
||||
|
||||
if (item != null && SelectedItem != null
|
||||
&& !string.IsNullOrEmpty(strItem)
|
||||
&& item.ToString().Equals(SelectedItem.ToString()))
|
||||
{
|
||||
selectedIndex = index;
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
SelectedIndex = selectedIndex;
|
||||
}
|
||||
|
||||
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
|
||||
{
|
||||
var boundPicker = (BindablePicker)bindable;
|
||||
|
||||
if (Equals(newValue, null) && !Equals(oldValue, null))
|
||||
return;
|
||||
|
||||
boundPicker.Items.Clear();
|
||||
|
||||
if (!Equals(newValue, null))
|
||||
{
|
||||
foreach (var item in (IEnumerable)newValue)
|
||||
boundPicker.Items.Add(item.ToString());
|
||||
}
|
||||
|
||||
boundPicker.InternalUpdateSelectedIndex();
|
||||
}
|
||||
|
||||
private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
|
||||
{
|
||||
var boundPicker = (BindablePicker)bindable;
|
||||
|
||||
boundPicker.ItemSelected?.Invoke(boundPicker,
|
||||
new SelectedItemChangedEventArgs(newValue));
|
||||
|
||||
if(boundPicker.ItemSelectedCommand != null)
|
||||
{
|
||||
boundPicker.ItemSelectedCommand.Execute(newValue);
|
||||
}
|
||||
|
||||
boundPicker.InternalUpdateSelectedIndex();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<slideOverKit:SlideMenuView
|
||||
xmlns="http://xamarin.com/schemas/2014/forms"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
xmlns:slideOverKit="clr-namespace:SlideOverKit"
|
||||
xmlns:controls="clr-namespace:eShopOnContainers.Core.Controls;assembly=eShopOnContainers.Core"
|
||||
xmlns:slideOverKit="clr-namespace:SlideOverKit"
|
||||
x:Class="eShopOnContainers.Core.Views.FiltersView"
|
||||
MenuOrientations="TopToBottom"
|
||||
BackgroundColor="{StaticResource BackgroundColor}"
|
||||
@ -13,7 +12,7 @@
|
||||
<ResourceDictionary>
|
||||
|
||||
<Style x:Key="FilterPickerStyle"
|
||||
TargetType="{x:Type controls:BindablePicker}">
|
||||
TargetType="{x:Type Picker}">
|
||||
<Setter Property="HeightRequest"
|
||||
Value="48" />
|
||||
<Setter Property="BackgroundColor"
|
||||
@ -70,35 +69,35 @@
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<!-- BRAND -->
|
||||
<controls:BindablePicker
|
||||
<Picker
|
||||
Grid.Row="0"
|
||||
Title="BRAND"
|
||||
ItemsSource="{Binding Brands}"
|
||||
SelectedItem="{Binding Brand, Mode=TwoWay}"
|
||||
Style="{StaticResource FilterPickerStyle}">
|
||||
<controls:BindablePicker.HeightRequest>
|
||||
<Picker.HeightRequest>
|
||||
<OnPlatform
|
||||
x:TypeArguments="x:Double"
|
||||
Android="48"
|
||||
iOS="48"
|
||||
WinPhone="36"/>
|
||||
</controls:BindablePicker.HeightRequest>
|
||||
</controls:BindablePicker>
|
||||
</Picker.HeightRequest>
|
||||
</Picker>
|
||||
<!-- TYPE -->
|
||||
<controls:BindablePicker
|
||||
<Picker
|
||||
Grid.Row="1"
|
||||
Title="TYPE"
|
||||
ItemsSource="{Binding Types}"
|
||||
SelectedItem="{Binding Type, Mode=TwoWay}"
|
||||
Style="{StaticResource FilterPickerStyle}">
|
||||
<controls:BindablePicker.HeightRequest>
|
||||
<Picker.HeightRequest>
|
||||
<OnPlatform
|
||||
x:TypeArguments="x:Double"
|
||||
Android="48"
|
||||
iOS="48"
|
||||
WinPhone="36"/>
|
||||
</controls:BindablePicker.HeightRequest>
|
||||
</controls:BindablePicker>
|
||||
</Picker.HeightRequest>
|
||||
</Picker>
|
||||
<Button
|
||||
Grid.Row="2"
|
||||
Text="Apply"
|
||||
|
1
src/Mobile/eShopOnContainers/eShopOnContainers.Core/eShopOnContainers.Core.csproj
Executable file → Normal file
1
src/Mobile/eShopOnContainers/eShopOnContainers.Core/eShopOnContainers.Core.csproj
Executable file → Normal file
@ -47,7 +47,6 @@
|
||||
<Compile Include="Controls\AddBasketButton.xaml.cs">
|
||||
<DependentUpon>AddBasketButton.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Controls\BindablePicker.cs" />
|
||||
<Compile Include="Controls\CustomTabbedPage.cs" />
|
||||
<Compile Include="Controls\ToggleButton.cs" />
|
||||
<Compile Include="Converters\CountToBoolConverter.cs" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user