Domanda

I'm having trouble with my first application on windows phone 8.

I have a list box, which is being fueled by a json webservice.

need to get the values ​​of textbox to use a button that takes each line of the list box.

an example ... in json webservice I have the values ​​of latitude and longitude ... wanna take those values ​​and use them to open the gps clicking on the button that has the listbox on each line

here s my code MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="JSONParsingExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <!--ContentPanel - place additional content here-->
       <Button x:Name="myButton" Grid.Row="0" Height="75" Content="Consulta" VerticalAlignment="Top" />
            <ProgressBar Name="ProgressBarRequest" IsIndeterminate="True" Visibility="Collapsed"></ProgressBar>
             <StackPanel Name="StackPanelBackground" Height="150"></StackPanel>
            <ListBox Name="lbResultado" Grid.Row="2" Grid.ColumnSpan="2">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                               <Grid.RowDefinitions>
                                   <RowDefinition Height="Auto"/>
                                   <RowDefinition Height="*"/>
                                   <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
    <Image Width="120" Height="120" Grid.RowSpan="2" Grid.Column="0" Source="{Binding foto1}" Margin="10" VerticalAlignment="Top"></Image>

    <TextBox FontWeight="Bold" Foreground="{StaticResource PhoneAccentBrush}" FontSize="20" Text="{Binding nome}" Grid.Row="0" Grid.Column="1" />

    <TextBox FontSize="25" Text="{Binding latitude, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" x:Name="txtlatitude" />

    <TextBox FontSize="25" Text="{Binding longitude}" Grid.Row="1" Grid.Column="2" Name="longitude" />

    <Button x:Name="gps" Grid.Row="2" Grid.Column="2" Height="75" Content="GPS" Click="gps_Click" />

                    </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
     </ListBox>
   </Grid>
</phone:PhoneApplicationPage>

MainPage.xaml.cs

namespace JSONParsingExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        private const string DRIVING_PROTOCOL = "ms-drive-to";
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            myButton.Click += new RoutedEventHandler(myButton_Click);
        }
        void myButton_Click(object sender, RoutedEventArgs e)
        {
            string URL = "link_to_ws"; //working correctly

            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(new Uri(URL, UriKind.Absolute));

            LayoutRoot.Opacity = 0.5;
            lbResultado.IsEnabled = false;    
        }
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            DataContractJsonSerializer json =
                new DataContractJsonSerializer(typeof(RootObject));

            RootObject lista = (RootObject)json.ReadObject(e.Result);

            lbResultado.ItemsSource = lista.wsempresas;

            LayoutRoot.Opacity = 1;
            lbResultado.IsEnabled = true;
        }

        public class Wsempresa
        {
            public string id_empresa { get; set; }
            [DataMember(Name = "nome")]
            public string nome { get; set; }
            [DataMember(Name = "foto1")]
            public string foto1 { get; set; }
            public string endereco { get; set; }
            public string numero { get; set; }
            public string bairro { get; set; }
            public string cidade { get; set; }
            public string estado { get; set; }
            public string telefone_comercial { get; set; }
            public string website { get; set; }
            public string email { get; set; }
            [DataMember(Name = "latitude")]
            public string latitude { get; set; }
            [DataMember(Name = "longitude")]
            public string longitude { get; set; }
        }

        public class RootObject
        {
            [DataMember(Name = "wsempresas")]
            public Wsempresa[] wsempresas { get; set; }
        }


        void gps_Click(object sender, RoutedEventArgs e)
        {

           // string latitude = "-20.528430"; this works but i want to get lis box values
            //string longitude = "-47.437717";

            string uriProtocol = null;
            string uri;

            // Driving or walking directions?
            string buttonClicked = ((Button)sender).Name;
            switch (buttonClicked)
            {
                case "gps":
                    uriProtocol = DRIVING_PROTOCOL;
                    break;
                default:
                    uriProtocol = DRIVING_PROTOCOL;
                    break;
            }

            // Assemble the Uri for the request.
            uri = uriProtocol + ":?" +
                "destination.latitude=" + latitude + "&" +
                "destination.longitude=" + longitude + "&" ;
                //"destination.name=" + SEATTLE_NAME;


            // Make the request.
            RequestDirections(uri);
        }

        async void RequestDirections(string uri)
        {
            // Make the request.
            var success = await Windows.System.Launcher.LaunchUriAsync(new Uri(uri));
            if (success)
            {
                // Request succeeded.
            }
            else
            {
                // Request failed.
            }
        }
    }
}

can you help me please?

È stato utile?

Soluzione

You can try to get latitude and longitude value using Button's DataContext :

void gps_Click(object sender, RoutedEventArgs e)
{
    Wsempresa wsempresa = (Wsempresa)((Button)sender).DataContext;
    string latitude = wsempresa.latitude;
    string longitude = wsempresa.longitude;
    .........
    .........
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top