jeudi 17 avril 2014

ASP.net - code c# dans silverlight n'affichant ne pas de données sur la recherche, pressez le bouton (didacticiel de base) - Stack Overflow


I am c# silverlight beginner. I am practicing some samples i found on this link http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx


I have written exactly the code in this link. This links gives a GUI to search a "Topic" from a given link (please see code to know in detail).


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.Xml;
using System.Xml.Linq;
using System.Windows.Messaging;

namespace shekhar_basic
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
void SearchBtn_Click(object sender, RoutedEventArgs e)
{
string topic = watermark1.Text; //this topic receives the data correctly written on watermark, this watermark1 is the name of that button using xml.
string diggUrl = String.Format("http://services.digg.com/stories/topic/{0}?count=20&appkey=http%3A%2F%2Fscottgu.com", topic); //I think the problem creating part is here.

WebClient diggServices = new WebClient();
diggServices.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DiggService_DownloadStoriesCompleted);
diggServices.DownloadStringAsync(new Uri(diggUrl));
}

void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
}

void DisplayStories(string xmlContent)
{
XDocument xmlStories = XDocument.Parse(xmlContent);
var stories = from story in xmlStories.Descendants("story")
where story.Element("thumbnail") != null && !story.Element("thumbnail").Attribute("src").Value.EndsWith(".gif")
select new digStory
{
Id = (int)story.Attribute("id"),
Title = ((string)story.Element("title")).Trim(),
Description = ((string)story.Element("description")).Trim(),
ThumbNail = (string)story.Element("thumbnail").Attribute("src").Value,
HrefLink = new Uri((string)story.Attribute("link")),
NumDiggs = (int)story.Attribute("diggs"),
UserName = (string)story.Element("user").Attribute("name").Value,
};
dGStoreList1.SelectedIndex = -1;
dGStoreList1.ItemsSource = stories;

}

}
}

And my xml code for it is :


<UserControl x:Class="shekhar_basic.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls.WatermarkedTextBox"
mc:Ignorable="d" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">


<Grid Background="AntiqueWhite">

<Grid.RowDefinitions >
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid Grid.Row="0" Margin="7" ShowGridLines="True" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" CornerRadius="10" Background="Turquoise" >
<TextBlock Text=" Dig search" Foreground="Blue" />
</Border>

<local:WatermarkedTextBox Name ="watermark1" Grid.Column="1" Watermark="Enter the search" Margin="0,0,-7,0" />
<Button Grid.Column="2" Content="search" Click="SearchBtn_Click" />
</Grid>
<TextBlock Grid.Row="1" Margin="10" Foreground="Black">
Todo : Stories will display here
</TextBlock>
<sdk:DataGrid Grid.Row="1" Margin="5" Name="dGStoreList1">
</sdk:DataGrid>
</Grid>

</UserControl>

The GUI corresponding to this code is : http://prntscr.com/34k0d3 . What it should show is something like this : http://prntscr.com/34k0lx , You can see that inside "watermark button" they have written "television" and then on clicking "search" button it shows some results. But my code is not able to show those results i have debugged my code it is showing the string entered in my watermark button inside "topic" variable in my code in line "string topic = watermark1.Text;" that means button click even is correctly generated and it is receiving what i entered in watermark button on "search" click.


Where i guess the problem could be is in these two lines:


diggServices.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DiggService_DownloadStoriesCompleted);
diggServices.DownloadStringAsync(new Uri(diggUrl));

Could some one please help me in displaying the results on "search" click button on corresponding entered string inn "watermarkbutton" ? Than kyou so much for this help. EDIT: One more thing i want to mention i get this kind of dialogue box on running code in VS. http://prntscr.com/34ka0u and when i click "Yes" then i get this kind of window http://prntscr.com/34kabo which shows some error.




There are a couple things going on here. First and most significantly, that digg API apparently has been turned off.


You can typically check API calls like this by simply copying and pasting the URL your program is going to use into a browser, and seeing what comes back. When I put http://services.digg.com/stories/topic/television?count=20&appkey=http%3A%2F%2Fscottgu.com into my browser (note that I replaced the {0} with your test query), I just get redirected back to the digg home page. Not a good sign. Hunting around after that led me to the FAQ, which mentioned the API being offline for now.


You should also look at that API string and get a bit concerned: it has appkey=http://scottgu.com in it-- typically when you see something like that, it's a hint you should probably get your own API key.


All of that aside, your code also would not show results because you didn't get that far in the tutorial. Where you have


string result = e.Result;

In the tutorial he goes on to replace that with a call to the DisplayStories method, which is what actually parses the results.


A big part of why you had trouble tracking this down is probably that the code is deliberately ignoring errors, see:


void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
}

If e.Error isn't null -- if there was a problem returned from the service itself -- you ignore it, and have no way to know. Consider instead:


void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
else
{
MessageBox.Show(e.Error.ToString());
}
}

That should pop up a message about the error, if your call fails.



I am c# silverlight beginner. I am practicing some samples i found on this link http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx


I have written exactly the code in this link. This links gives a GUI to search a "Topic" from a given link (please see code to know in detail).


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.Xml;
using System.Xml.Linq;
using System.Windows.Messaging;

namespace shekhar_basic
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
void SearchBtn_Click(object sender, RoutedEventArgs e)
{
string topic = watermark1.Text; //this topic receives the data correctly written on watermark, this watermark1 is the name of that button using xml.
string diggUrl = String.Format("http://services.digg.com/stories/topic/{0}?count=20&appkey=http%3A%2F%2Fscottgu.com", topic); //I think the problem creating part is here.

WebClient diggServices = new WebClient();
diggServices.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DiggService_DownloadStoriesCompleted);
diggServices.DownloadStringAsync(new Uri(diggUrl));
}

void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
}

void DisplayStories(string xmlContent)
{
XDocument xmlStories = XDocument.Parse(xmlContent);
var stories = from story in xmlStories.Descendants("story")
where story.Element("thumbnail") != null && !story.Element("thumbnail").Attribute("src").Value.EndsWith(".gif")
select new digStory
{
Id = (int)story.Attribute("id"),
Title = ((string)story.Element("title")).Trim(),
Description = ((string)story.Element("description")).Trim(),
ThumbNail = (string)story.Element("thumbnail").Attribute("src").Value,
HrefLink = new Uri((string)story.Attribute("link")),
NumDiggs = (int)story.Attribute("diggs"),
UserName = (string)story.Element("user").Attribute("name").Value,
};
dGStoreList1.SelectedIndex = -1;
dGStoreList1.ItemsSource = stories;

}

}
}

And my xml code for it is :


<UserControl x:Class="shekhar_basic.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls.WatermarkedTextBox"
mc:Ignorable="d" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">


<Grid Background="AntiqueWhite">

<Grid.RowDefinitions >
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid Grid.Row="0" Margin="7" ShowGridLines="True" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" CornerRadius="10" Background="Turquoise" >
<TextBlock Text=" Dig search" Foreground="Blue" />
</Border>

<local:WatermarkedTextBox Name ="watermark1" Grid.Column="1" Watermark="Enter the search" Margin="0,0,-7,0" />
<Button Grid.Column="2" Content="search" Click="SearchBtn_Click" />
</Grid>
<TextBlock Grid.Row="1" Margin="10" Foreground="Black">
Todo : Stories will display here
</TextBlock>
<sdk:DataGrid Grid.Row="1" Margin="5" Name="dGStoreList1">
</sdk:DataGrid>
</Grid>

</UserControl>

The GUI corresponding to this code is : http://prntscr.com/34k0d3 . What it should show is something like this : http://prntscr.com/34k0lx , You can see that inside "watermark button" they have written "television" and then on clicking "search" button it shows some results. But my code is not able to show those results i have debugged my code it is showing the string entered in my watermark button inside "topic" variable in my code in line "string topic = watermark1.Text;" that means button click even is correctly generated and it is receiving what i entered in watermark button on "search" click.


Where i guess the problem could be is in these two lines:


diggServices.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DiggService_DownloadStoriesCompleted);
diggServices.DownloadStringAsync(new Uri(diggUrl));

Could some one please help me in displaying the results on "search" click button on corresponding entered string inn "watermarkbutton" ? Than kyou so much for this help. EDIT: One more thing i want to mention i get this kind of dialogue box on running code in VS. http://prntscr.com/34ka0u and when i click "Yes" then i get this kind of window http://prntscr.com/34kabo which shows some error.



There are a couple things going on here. First and most significantly, that digg API apparently has been turned off.


You can typically check API calls like this by simply copying and pasting the URL your program is going to use into a browser, and seeing what comes back. When I put http://services.digg.com/stories/topic/television?count=20&appkey=http%3A%2F%2Fscottgu.com into my browser (note that I replaced the {0} with your test query), I just get redirected back to the digg home page. Not a good sign. Hunting around after that led me to the FAQ, which mentioned the API being offline for now.


You should also look at that API string and get a bit concerned: it has appkey=http://scottgu.com in it-- typically when you see something like that, it's a hint you should probably get your own API key.


All of that aside, your code also would not show results because you didn't get that far in the tutorial. Where you have


string result = e.Result;

In the tutorial he goes on to replace that with a call to the DisplayStories method, which is what actually parses the results.


A big part of why you had trouble tracking this down is probably that the code is deliberately ignoring errors, see:


void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
}

If e.Error isn't null -- if there was a problem returned from the service itself -- you ignore it, and have no way to know. Consider instead:


void DiggService_DownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string result = e.Result;
}
else
{
MessageBox.Show(e.Error.ToString());
}
}

That should pop up a message about the error, if your call fails.


0 commentaires:

Enregistrer un commentaire