daniel-luberda / DLToolkit.Forms.Controls

Xamarin.Forms Custom Controls
Apache License 2.0
393 stars 182 forks source link

FlowListView Grouping Causes System.InvalidCastException: Object must implement IConvertible. #211

Open Marc-K-Plewka opened 6 years ago

Marc-K-Plewka commented 6 years ago

Hello, when i am trying to group the listview, I always get this exception, no matter what. I tried different approaches to group the data. I also reduced my code to a minimal example using your simpleItem, but I still get this exception. Unfortunately, this exception line is all I get, therefore I am not able to pinpoint the problem.

XAML: `<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

    xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"

    xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
     x:Class="DachDeckerApp.MainView.GalleryPage">
<ContentPage.ToolbarItems>
    <ToolbarItem  Icon="photo_camera_white.png" Order="Primary" Priority="0" Command="{Binding OnTapGestureCameraIconCommand}"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <flv:FlowListView SeparatorVisibility="None" HasUnevenRows="true" IsGroupingEnabled="true" FlowGroupDisplayBinding="{Binding Key}"

        FlowColumnMinWidth="110" FlowItemsSource="{Binding Pictures}">
        <flv:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <!--<ffimageloading:CachedImage HeightRequest="100" Aspect="AspectFill"
                        DownsampleHeight="100" DownsampleUseDipUnits="false" 
                        LoadingPlaceholder="image_loading.gif" ErrorPlaceholder="image_loading.gif"
                        Source="{Binding ImageUrl}" RetryCount="5"/>-->
            </DataTemplate>
        </flv:FlowListView.FlowColumnTemplate>
    </flv:FlowListView>
</ContentPage.Content>

`

PageModel:

` public class GalleryPageModel : FreshBasePageModel {

    public String Name { get; set; }
    public FlowObservableCollection<object> Pictures { get; set; }
    public ObservableCollection<ImageModel> LocalPictures { get; set; }

    public Project Project { get; set; }

    IPluginCollector _pluginCollector;
    IDataModel _dataModel;

    MainPageSettingsModel _settingsModel;

    /// <summary>
    /// gets all settingsmodel from the plugincollector to get the widgets on application start
    /// </summary>
    /// <param name="pluginCollector"></param>
    public GalleryPageModel(IPluginCollector pluginCollector, IDataModel dataModel)
    {

        _pluginCollector = pluginCollector;
        _dataModel = dataModel;
        _settingsModel = (MainPageSettingsModel)_pluginCollector.SettingsModels.Where(x => x.Key == PluginNames.MainPluginName).Select(x => x.Value).First();

        Project = _settingsModel.SelectedProject;

        var exampleData = new List<SimpleItem>();
        var random = new Random(DateTime.Now.Millisecond);
        var howMany = 60;
        for (int i = 0; i < howMany; i++)
        {
            exampleData.Add(new SimpleItem() { Title = Guid.NewGuid().ToString("N").Substring(0, 8) });
        }
        var sorted = exampleData
            .OrderBy(item => item.Title)
            .GroupBy(item => item.Title[0].ToString())
            .Select(itemGroup => new Grouping<string, SimpleItem>(itemGroup.Key, itemGroup, random.Next(1, 6)))
            .ToList();

//var sorted = // Project.AllPics.OrderBy(p => p.Location) // .GroupBy(p => p.Location[0].ToString()) // .Select(p => new Grouping<string, ImageModel>(p.Key,p)) // .ToList();

        Pictures = new FlowObservableCollection<object>(sorted);
        LocalPictures = Project.LocalPics;
        LastTappedItem = new ImageModel();
    }

    private void RefreshPictures()
    {
        _dataModel.RefreshPictures(_settingsModel.SelectedProject);
    }

    public Command<object> ItemTappedCommand
    {
        get
        {
            return new Command<object>(async (item) =>
            {
               _settingsModel.SelectedPicturePath = ((ImageModel)item).ImageUrl;
                await this.CoreMethods.PushPageModel<ImagePageModel>();
            });
        }
    }

    public Command<object> OnTapGestureCameraIconCommand
    {
        get
        {
            return new Command<object>(async (item) =>
            {
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {

                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Directory = "Images",
                    Name = $@"image -{ DateTime.Now.Ticks }.jpg",
                    SaveMetaData = true,
                    CompressionQuality = 92,
                });

                if (file == null)
                    return;

                _dataModel.SavePictureLocal(file.Path, _settingsModel.SelectedProject);

                OnTapGestureCameraIconCommand.Execute(null);
            });
        }
    }

    public object LastTappedItem { get; set; }

    protected override void ViewIsAppearing(object sender, EventArgs e)
    {
        base.ViewIsAppearing(sender, e);

    }
}`
Wazzaps commented 6 years ago

I seem to get this error with native xamarin.forms. Did you find a solution?

vlkam commented 6 years ago

Pictures = new FlowObservableCollection< object >(sorted);

Why do you use the object as generic type ?