Passing a parsed string into a click event handler

…衆ロ難τιáo~ 提交于 2019-12-13 03:22:05

问题


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!

Here's the code:

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

UPDATED CODE:

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

回答1:


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




回答2:


As I see it there are two ways of doing what you want.

1) Make a class wide variable, aka a field. This can be seen and modified in any of the classes methods. It is a shared variable between the classes methods.

2) Try calling the App_Name_Click method directly passing the variable you want as an object in the sender parameter. ie App_Name_Click(appLink, new RoutedEventArgs()) or a tidier way is to do this: App_Name_Click(this, new MyRoutedEventArgs(){GUID = appLink})



来源:https://stackoverflow.com/questions/8749585/passing-a-parsed-string-into-a-click-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!