我在Windows Phone 8上遇到第一个应用程序时出现问题。
我有一个列表框,由json webservice提供支持。
需要获取文本框的值才能使用一个按钮来获取列表框的每一行。
示例...在json webservice中我有纬度和经度的值...想要获取这些值并使用它们打开gps,点击每行上有列表框的按钮
这是我的代码 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.
}
}
}
}
你可以帮我吗?
答案 0 :(得分:0)
您可以尝试使用Button
DataContext
来获取纬度和经度值:
void gps_Click(object sender, RoutedEventArgs e)
{
Wsempresa wsempresa = (Wsempresa)((Button)sender).DataContext;
string latitude = wsempresa.latitude;
string longitude = wsempresa.longitude;
.........
.........
}