我的问题首先是来自服务器的downladin xml文件,如下所示:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//pobieranie danych z pliku xml znajdujacego sie na serwerze
WebClient client = new WebClient();
client.DownloadStringCompleted += HttpsCompleted;
client.DownloadStringAsync(new Uri("Myurl/baza.xml"));
}
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xdoc3 = XDocument.Parse(e.Result, LoadOptions.None);
var data = from query in xdoc3.Descendants("Item")
select new Item
{
name = (string)query.Element("name"),
prise = (int)query.Element("prise"),
howmany = (int)query.Element("howmany")
};
items_listBox.ItemsSource = data;
status_textBlock.Text = "Data ok";
}
else {
status_textBlock.Text = "Fail";
kup_button.Visibility = 0;
}
然后我做了一个刷新按钮,并添加了它的功能,基本上是相同的代码:
private void button1_Click(object sendera, RoutedEventArgs a)
{
WebClient client2 = new WebClient();
client2.DownloadStringCompleted += HttpsCompleteda;
client2.DownloadStringAsync(new Uri(Myurl/baza.xml"));
}
private void HttpsCompleteda(object sendera, DownloadStringCompletedEventArgs a)
{
#region server_ok
if (a.Error == null
{
#region refresh
XDocument xdoca = XDocument.Parse(a.Result, LoadOptions.None);
var new_data = from query in xdoca.Descendants("Item")
select new Item
{
name = (string)query.Element("name"),
prise = (int)query.Element("prise"),
howmany = (int)query.Element("howmany")
};
items_listBox.ItemsSource = new_data;
}
在第一个xml之后,它(xml文件)被chenged(我可以通过我的网络浏览器看到)但是当我刷新它看起来像钢一样
items_listBox.ItemsSource
任何想法?谢谢你的帮助 xaml代码
x:Class="Sklep.Page1"
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" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Sklep" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Sklep" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Height="49" Width="456" FontSize="30" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="zamow" Height="70" HorizontalAlignment="Left" Margin="-12,474,0,0" Name="kup_button" VerticalAlignment="Top" Width="480" Click="kup_button_Click" />
<TextBlock HorizontalAlignment="Left" Margin="230,550,0,74" Name="textBlock1" Text="Ilość sztuk" Width="190" />
<TextBox Height="57" HorizontalAlignment="Left" Margin="0,536,0,0" Name="order_textBox" Text="1" VerticalAlignment="Top" Width="224" TextAlignment="Right" FontSize="18" />
<CheckBox Content="zapłacone" Height="86" HorizontalAlignment="Left" Margin="171,568,0,0" Name="pay_box" VerticalAlignment="Top" Width="261" />
<ListBox x:Name="items_listBox" MinHeight="200" MinWidth="300" Margin="6,62,6,174" MaxHeight="500">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding prise}"/>
<TextBlock Text="{Binding howmany}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Height="53" HorizontalAlignment="Left" Margin="1,3,0,0" Name="status_textBlock" Text="" VerticalAlignment="Top" Width="449" TextAlignment="Center" />
<Button Content="Odswież" Height="103" HorizontalAlignment="Left" Margin="339,545,0,0" Name="button1" VerticalAlignment="Top" Width="114" FontSize="15" Click="button1_Click" />
</Grid>
</Grid>
答案 0 :(得分:2)
请求将由WebClient缓存 最简单的解决方案是通过添加查询字符串的唯一内容使请求看起来像另一个。
client.DownloadStringAsync(new Uri("Myurl/baza.xml?rand=" + Guid.NewGuid()));
答案 1 :(得分:0)
您的问题似乎更像是一个数据绑定问题而不是Web服务调用。你应该提供你的XAML代码。
答案 2 :(得分:0)
我用
解决了这个问题static public WebClient public_webclient