将解析的字符串传递给点击事件处理程序 [英] Passing a parsed string into a click event handler

查看:16
本文介绍了将解析的字符串传递给点击事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebClient 解析 xml 文件中的数据.在 DownloadStringCompleted 方法中,我有一个经过解析的字符串,我想将其传递给单击事件处理程序.我希望点击事件打开我的应用程序的市场详细信息.为此,我需要一个解析后的字符串,它是一个 GUID,并将它放在事件处理程序中.我试图用谷歌搜索它,但一无所获.我只是不知道该怎么做.

I'm parsing data from an xml file using WebClient. In DownloadStringCompleted method I have a string, which is parsed, that I want to pass into a click event handler. I want the click event to open my app's Marketplace details. For that I need a that parsed string which is a GUID and place it in the event handler. I tried to google it and found nothing. I just can't figure out how to do it.

任何帮助将不胜感激.谢谢!

Any help will be highly appreciated. Thanks!

代码如下:

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();
    }

推荐答案

只需将您想要的功能移出点击处理程序并添加一个参数.这样,您只需调用该方法并传入值 ShowMarket(link.AppUri)

Simply move your desired functionality out of the click handler and add a parameter. That way, you can just call the method and pass in the value 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();
}

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

edit: in response to a comment clarifying the question ... all you'd need to do is set link.AppUri to a class property or field, and then in the click handler, simply use that variable to pass to ShowMarket (or however you choose to express the functionality, even right in the click handler)

这篇关于将解析的字符串传递给点击事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆