将解析后的字符串传递给单击事件处理程序

时间:2012-01-05 20:49:10

标签: c# windows-phone-7.1 windows-phone-7

我正在使用WebClient从xml文件解析数据。在DownloadStringCompleted方法中,我有一个字符串,它被解析,我想传递给click事件处理程序。我想点击事件打开我的应用程序的市场详细信息。为此我需要一个解析后的字符串,它是一个GUID并将其放在事件处理程序中。我试图google它,什么都没发现。我只是想不出怎么做。

任何帮助都将受到高度赞赏。谢谢!

以下是代码:

public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    {
        XDocument moreApps = XDocument.Parse(e.Result);

        morePageAppList.ItemsSource = from Apps in moreApps.Descendants("App")
                                       select new MoreApps
                                       {
                                           MoreImage = Apps.Element("link").Value,
                                           Price = Apps.Element("price").Value,
                                           Title = Apps.Element("title").Value
                                       };

        var link = (from Apps in moreApps.Descendants("App")
                      select new MoreApps
                      {
                         AppUri = (string)Apps.Element("marketplace").Value
                      }).Single();

        string appLink = link.AppUri;
    }

    private void App_Name_Click(object sender, RoutedEventArgs e)
    {

        MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
        marketplaceDetailTask.ContentIdentifier = "Marketplace GUID";
        marketplaceDetailTask.Show();
    }

更新代码:

morePageAppList.ItemsSource = from Apps in moreApps.Descendants("App")
                                       select new MoreApps
                                       {
                                           MoreImage = Apps.Element("link").Value,
                                           Price = Apps.Element("price").Value,
                                           Title = Apps.Element("title").Value
                                       };

        var link = (from Apps in moreApps.Descendants("App")
                    select new MoreApps
                    {
                        AppUri = (string)Apps.Element("marketplace").Value
                    }).FirstOrDefault();

        appLink = link.AppUri;            
    }

    private void App_Name_Click(object sender, RoutedEventArgs e)
    {
        ShowMarket(appLink);
    }

    private void ShowMarket(string id)
    {
        MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
        marketplaceDetailTask.ContentIdentifier = id;
        marketplaceDetailTask.Show();
    }

2 个答案:

答案 0 :(得分:1)

只需移动点击处理程序所需的 功能并添加参数即可。这样,您只需调用方法并传入值ShowMarket(link.AppUri)

即可
private string appid;

// ..
appid = link.AppUri;
// ..
private void App_Name_Click(object sender, RoutedEventArgs e)
{
    ShowMarket(appid);
}

private void ShowMarket(string id)
{

    MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
    marketplaceDetailTask.ContentIdentifier = id;
    marketplaceDetailTask.Show();
}

编辑:在回复澄清问题的评论时...您需要做的就是将link.AppUri设置为类属性或字段,然后在单击处理程序中,只需使用该变量传递给ShowMarket(或者你选择表达功能,甚至是在点击处理程序中)

答案 1 :(得分:-2)

我认为有两种方法可以做你想做的事。

1)创建一个类宽变量,也就是一个字段。这可以在任何类方法中看到和修改。它是类方法之间的共享变量。

2)尝试调用App_Name_Click方法,直接将您想要的变量作为sender参数传递给对象。即App_Name_Click(appLink, new RoutedEventArgs())或更整洁的方法是:App_Name_Click(this, new MyRoutedEventArgs(){GUID = appLink})