【问题标题】:c# code in silverlight not displaying data on search button press (basic tutorial)silverlight 中的 c# 代码在按下搜索按钮时不显示数据(基本教程)
【发布时间】:2014-03-27 13:27:21
【问题描述】:

我是 c# silverlight 初学者。我正在练习我在此链接上找到的一些示例http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx

我已经准确地编写了此链接中的代码。此链接提供了一个 GUI,可以从给定链接中搜索“主题”(请参阅​​代码以了解详细信息)。

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;

        }

    }
}

我的 xml 代码是:

<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>

此代码对应的GUI是:http://prntscr.com/34k0d3。它应该显示的是这样的:http://prntscr.com/34k0lx,你可以看到在"watermark button" 里面他们写了“电视”,然后点击“搜索”按钮它显示了一些结果。但是我的代码无法显示那些我已经调试过我的代码的结果,它显示了在我的代码中的"topic" 变量中输入的字符串"string topic = watermark1.Text;”,这意味着按钮点击甚至是正确生成的并且它正在接收我在"search" click 上的水印按钮中输入的内容。

我猜问题可能出在这两行:

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

有人可以帮我在"search"上显示结果吗?点击相应输入的字符串旅馆"watermarkbutton"上的按钮?非常感谢 kyou 的帮助。 编辑:我还想提一提的是,在 VS 中运行代码时会出现这种对话框。 http://prntscr.com/34ka0u 当我点击“是”然后我得到这种窗口http://prntscr.com/34kabo 显示一些错误。

【问题讨论】:

    标签: c# asp.net xml silverlight silverlight-5.0


    【解决方案1】:

    这里发生了几件事。首先也是最重要的是,digg API 显然已被关闭。

    您通常可以通过简单地将您的程序将使用的 URL 复制并粘贴到浏览器中来检查这样的 API 调用,然后查看返回的内容。当我将http://services.digg.com/stories/topic/television?count=20&amp;appkey=http%3A%2F%2Fscottgu.com 放入浏览器时(请注意,我将{0} 替换为您的测试查询),我只是被重定向回digg 主页。不是一个好兆头。在那之后四处寻找,我找到了the FAQ,其中提到 API 目前处于离线状态。

    您还应该查看该 API 字符串并有点担心:其中包含 appkey=http://scottgu.com - 通常当您看到类似的内容时,这暗示您可能应该获得自己的 API 密钥。

    除此之外,您的代码也不会显示结果,因为您在本教程中没有学到那么远。你在哪里

    string result = e.Result;
    

    在教程中,他继续将其替换为调用 DisplayStories 方法,这是实际解析结果的方法。

    您无法追踪此问题的很大一部分原因可能是代码故意忽略了错误,请参阅:

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

    如果e.Error 不为空——如果服务本身返回了问题——你忽略它,并且无法知道。请考虑:

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

    如果您的呼叫失败,应该会弹出一条关于错误的消息。

    【讨论】:

    • @Jason 感谢我尝试打印错误消息的建议,我得到了这个prntscr.com/34kh9r,这对我来说非常复杂。
    • @user234839 您真正需要注意的唯一一件事是有一个SecurityError。因此,当您的代码尝试下载该 URL 时,它失败了,给出的原因是安全性。如果你回顾一下你正在做的tutorial,在顶部附近,他讨论了跨域网络访问。基本上,对于 silverlight 查询这样的内容,服务器需要通过拥有策略文件说没问题。由于这个 API 不存在,那些政策文件也没有了。
    • 我应该采取什么解决方案来显示按钮单击时的一些数据,以便进一步了解教程?您知道在 Internet Explorer 上单击“搜索”显示数据的另一个等效链接吗?我对此表示感谢。我的意思是相当于“Digg”链接。
    • 我不知道任何公开的、silverlight 友好的 API 服务,您可能想问另一个问题。或者,您可以设置自己的 Web 服务以使用,就像他们在本教程中所做的那样:silverlightshow.net/items/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多