Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Xamarin Labs HybridWebView and Javascript.

$
0
0

Hello,

I'm trying to call a javascript and use a listView to show the return.
However I'm having some problems and it is not working.
I'm using the hybridWebView from Xamarin Labs.

For my javascript to work I've also to send the device current location which I get using this method from xamarin forms recipes:

http://blog.xamarin.com/video-xamarin-forms-over-90-code-re-use-and-access-to-native-features/

Here is a little part of my code:

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using Newtonsoft.Json;


    namespace SeeYapp
    {
        public class SetLocationPage : ContentPage
        {

            LocationViewModel viewModel; //for get the device location with GPS

            HtmlWebViewSource hwvsource = new HtmlWebViewSource(); //to set the source

            public List<Model_Class> list = new List<Model_Class>(); // to show the data in list view

            Model_Location location = new Model_Location(); //To keep location

            public ListView listView = new ListView //ListView To show data
            {
                RowHeight = 40
            };

            HybridWebView hwv = new HybridWebView { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand }; //HybridWebView

            StackLayout stack = new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand };

            public SetLocationPage ()
            {
                           //here I also send the location as a parameter
                hwvsource.Html = @"
                                         <!DOCTYPE html>
                                             <html> " + location + @" 
                                            Native('dataCallback', list);
                                               //here I have a JS that returns a json list
                                               //took off the code
                                             </html>";

                hwv.Source = hwvsource; //set the JS as the source of Hybrid Web View

                hwv.RegisterCallback ("dataCallback", JSCallback); //Register a callback to get the return


                listView.ItemsSource = list; // set the list view to receive the JS return
                listView.ItemTemplate = new DataTemplate(typeof(ShowCell));

                stack.Children.Add(hwv); //add the HybridWebView
                stack.Children.Add(listView); //add the list view
                this.Content = stack;



                viewModel = new LocationViewModel();


                MessagingCenter.Subscribe<ILocation, Model_Location>(this, "LocationChanged", (sender, arg) =>
                    {
                                      //function that is called when the location changes
                                       location = args;


                    });
            }


            protected override void OnAppearing()
            {            
                base.OnAppearing ();
                //listView.ItemsSource = list;

            }


            protected override void OnDisappearing()
            {            
                base.OnDisappearing();

                hwv.RemoveCallback ("dataCallback");
            }

            private void JSCallback(string result){

                list = JsonConvert.DeserializeObject<List<Model_Location_Nearby>> (result);
                      //Here I deserialize the json into the list  
        }
        }
    }

Im having a lot of problems... first I cant refresh the listView.ItemsSource... because when I first declare it... is empty and just get filled after the JSCallback function is called... but this function is only called when appearing...And I cant get this working....

I also tried to pass the list to another page in the constructor and then in that page show the list view but I could not pop and push pages inside the JSCallback function and also could not set the listView.ItemsSource inside JSCallBack function...

My JS receive the location parameter so I would also need it to be sent every time the page appear because the location changes.

Anyone coul give a little help?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>