Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xamarin.forms - Query data from firebase Xamarin

I am trying to filter data from firebase in my xamarin forms app.

Here is code of my list view in .xaml page

<ListView x:Name="listView" ItemsSource="{Binding ProductList}" HasUnevenRows="true" SeparatorVisibility="None" BackgroundColor="#fafafa">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="0,0,0,40">
                              <Label Text="{Binding Name}" FontSize="Large" TextColor="Black"/>                                           
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

In cs file of xaml page i have inside constructor:

BindingContext = new ProductsViewModel(userId, Navigation, productType);

And this is my ViewModel

public class ProductsViewModel : BaseViewModel
{
    public Guid UserId { get; set; }
    public string ProductType { get; set; }

    private APIService services;

    public INavigation Navigation { get; }

    private ObservableCollection<ProductModel> _ProductList = new ObservableCollection<ProductModel>();
    public ObservableCollection<ProductModel> ProductList
    {
        get { return _ProductList; }
        set
        {
            _ProductList = value;
            OnPropertyChanged();
        }
    }

    public ProductsViewModel(Guid userId, INavigation navigation, string productTyp)
    {
        UserId = userId;
        ProductType = productTyp;
        services = new APIService();
        Navigation = navigation;
        ProductList = services.GetProducts();
    }
}

And my APIService's class

internal ObservableCollection<ProductModel> GetProducts()
    {
        var outfitFeedData = firebase
            .Child("Products")
            .AsObservable<ProductModel>()
            .AsObservableCollection();

        return outfitFeedData;
    }

All works fine until i start to query result, then i start getting convert type exceptions.

EG:

    internal ObservableCollection<ProductModel> GetProducts()
    {
        var outfitFeedData = firebase
            .Child("Products")
            .AsObservable<ProductModel>()
            .AsObservableCollection().Select(item => new ProductModel
            {
                Type = "K",
            });

        return outfitFeedData;
    }
question from:https://stackoverflow.com/questions/65873849/query-data-from-firebase-xamarin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<gcloset.Models.ProductModel>' to 'System.Collections.ObjectModel.ObservableCollection<gcloset.Models.ProductModel>'. An explicit conversion exists (are you missing a cast?)

From your error message, you can see that you will get System.Collections.Generic.IEnumerable type data outfitFeedData by LINQ result.

internal ObservableCollection<ProductModel> GetProducts()
{
    var outfitFeedData = firebase
        .Child("Products")
        .AsObservable<ProductModel>()
        .AsObservableCollection().Select(item => new ProductModel
        {
            Type = "K",
        });

    return outfitFeedData;
}

But you convert List data type to ObservableCollection by GetProducts method, I think it is the problem.

Please modify ObservableCollection ProductList like the following code:

 private List<ProductModel> _ProductList;
    public List<ProductModel> ProductList
    {
        get { return _ProductList; }
        set
        {
            _ProductList = value;
            OnPropertyChanged();
        }
    }

Then change GetProducts() method return value.

internal List<ProductModel> GetAllPersons()
    {

        var outfitFeedData= (firebase
          .Child("Products")
          .OnceAsync<ProductModel>()).Select(item => new ProductModel
          {
              Type = "K",
          }).ToList();
        return outfitFeedData;

    }

Note: I suggest you can implement Async and Await to fetch data from firebase, like this:

  public async Task<List<Person>> GetAllPersons()
    {

        return (await firebase
          .Child("Persons")
          .OnceAsync<Person>()).Select(item => new Person
          {
              Name = item.Object.Name,
              PersonId = item.Object.PersonId
          }).ToList();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...