|
|
|
ปัญหา Windows Phone Show List Data from Web Server (Website) |
|
|
|
|
|
|
|
ลองเรียกข้อมูลในดาต้าเบสมาแสดงแต่เกิดปัญหา ควรแก้ไขอย่างไร
Code
https://www.thaicreate.com/mobile/windows-phone-show-list-data-from-web-server.html
Code
<phone:PhoneApplicationPage
x:Class="Train_Guide_Thailand.SearchResultPage"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="#FF71BAE8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Margin="10,40,2,10">
<TextBlock Text="Treno" Margin="16,-40,10,0" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Comic Sans MS" Foreground="#FF494943" FontSize="20" Height="23" FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="Search result" FontFamily="Comic Sans MS" Foreground="#FF494943" FontSize="26" Height="48" Margin="16,0,0,0"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid Background="LightSkyBlue" x:Name="ContentPanel" CacheMode="BitmapCache" Margin="0,98,0,0" Grid.RowSpan="2">
<!--<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="81*"/>
<ColumnDefinition Width="79*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120*"/>
<RowDefinition Height="104*"/>
<RowDefinition Height="112*"/>
<RowDefinition Height="111*"/>
<RowDefinition Height="112*"/>
<RowDefinition Height="111*"/>
</Grid.RowDefinitions>-->
<!-- FromTo -->
<TextBlock FontFamily="Comic Sans MS" Foreground="#FF494943" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Form : " VerticalAlignment="Top" Margin="20,20,0,0" Width="132" Height="28"/>
<TextBlock FontFamily="Comic Sans MS" Foreground="#FF494943" HorizontalAlignment="Left" TextWrapping="Wrap" Text="To : " VerticalAlignment="Top" Margin="20,65,0,0" Height="28" Width="66"/>
<TextBlock x:Name="formTxt" Text="text1" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="149,20,0,0" Width="300" Height="45"/>
<TextBlock x:Name="toTxt" Text="text2" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="149,65,0,0" Width="300" Height="42"/>
<ListBox Margin="10,117,10,10" x:Name="StationList" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Station_ID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--<TextBlock Text="{Binding Station_ID}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>-->
<TextBlock Text="{Binding StationName}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Text="{Binding Latitude}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock Text="{Binding Longtitude}" TextWrapping="Wrap" Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.IO.IsolatedStorage;
namespace Train_Guide_Thailand
{
public partial class SearchResultPage : PhoneApplicationPage
{
ProgressIndicator prog;
public SearchResultPage()
{
InitializeComponent();
string url = "http://169.254.80.80/searchData.php";
Uri uri = new Uri(url, UriKind.Absolute);
StringBuilder postData = new StringBuilder();
postData.AppendFormat("{0}={1}", "NULL", HttpUtility.UrlEncode(""));
WebClient client = default(WebClient);
client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
client.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
client.UploadStringCompleted += client_UploadStringCompleted;
client.UploadProgressChanged += client_UploadProgressChanged;
client.UploadStringAsync(uri, "POST", postData.ToString());
prog = new ProgressIndicator();
prog.IsIndeterminate = true;
prog.IsVisible = true;
prog.Text = "Loading....";
SystemTray.SetProgressIndicator(this, prog);
}
private void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
}
private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Cancelled == false && e.Error == null)
{
MemoryStream ms = new MemoryStream( Encoding.UTF8.GetBytes(e.Result));
ObservableCollection<Station> list = new ObservableCollection<Station>();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Station>));
//fuckkkkk
list = (ObservableCollection<Station>)serializer.ReadObject(ms);
List<Station> myStation = new List<Station>();
foreach (Station cm in list)
{
string sStation_ID = cm.Station_ID.ToString();
string sStationName = cm.StationName.ToString();
string sLatitude = cm.Latitude.ToString();
string sLongtitude = cm.Longtitude.ToString();
myStation.Add(new Station(sStation_ID, sStationName, sLatitude, sLongtitude));
}
this.StationList.ItemsSource = myStation;
prog.IsVisible = false;
}
}
////}
[DataContract]
public class Station
{
[DataMember]
public string Station_ID { get; set; }
[DataMember]
public string StationName { get; set; }
[DataMember]
public string Latitude { get; set; }
[DataMember]
public string Longtitude { get; set; }
public Station(string strStation_ID, string strStationName, string strLatitude, string strLongtitude)
{
this.Station_ID = strStation_ID;
this.StationName = strStationName;
this.Latitude = strLatitude;
this.Longtitude = strLongtitude;
}
}
//--------show header == Form-To
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg1 = "";
string msg2 = "";
if (NavigationContext.QueryString.TryGetValue("msg1", out msg1))
{
formTxt.Text = msg1;
}
if (NavigationContext.QueryString.TryGetValue("msg2", out msg2))
{
toTxt.Text = msg2;
}
}
//-----------------------------------------------
}
}
Tag : Mobile, C#, Windows Phone, Mobile
|
|
|
|
|
|
Date :
2013-11-23 15:19:27 |
By :
ThanatthaB |
View :
1025 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เข้าใจว่า JSON ที่ถูกส่งมา อาจจะเป็นค่า null หรือผิด format ครับ
|
|
|
|
|
Date :
2013-11-24 11:03:03 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองแก้โค้ด จากต้นฉบับแล้วแต่พอเปลี่ยน บรรทัดที่ 090 .this.StationList.ItemsSource = myStation;
แล้วมันerror ค่ะ
|
|
|
|
|
Date :
2013-11-24 18:14:18 |
By :
ThanatthaB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้ได้แล้วค่ะ คือต้องออกจาก Emulator ก่อน แล้วStart Debugging ใหม่
ขอบคุญมากนะคะ
|
|
|
|
|
Date :
2013-11-24 18:32:20 |
By :
ThanatthaB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|