Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

wpf - How can I use List to populate text block?

I am trying to place strings to grid element in xaml, but i am having trouble populating the list from which I take strings. Here is the XAML code:

 
 </Grid.RowDefinitions>

                <StackPanel Grid.Column="2" Grid.Row="1">
                    <TextBlock x:Name="HomeDefender1"  TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
                </StackPanel>
                <StackPanel Grid.Column="2" Grid.Row="2">
                    <TextBlock x:Name="HomeDefender2" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
                </StackPanel>
                <StackPanel Grid.Column="2" Grid.Row="3">
                    <TextBlock x:Name="HomeDefender3" TextWrapping="Wrap"  Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
                </StackPanel>
                <StackPanel Grid.Column="2" Grid.Row="4">
                    <TextBlock x:Name="HomeDefender4" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
                </StackPanel>


Each text block is basicaly a position on a grid/canvas where name of a single player is suppose to go. By selecting an element from combo box, here named cb, a method forms an API call by completing URL with combo box element. Here is a method that should place element in these four text blocks, from API. This method works.




public static async void SetHomeTeamPlayersPositionsDEF(ComboBox cb,TextBlock tb1, textBlock tb2, TextBlock tb3, TextBlock tb4)
        {
            if (cb.SelectedIndex == -1)
            {
                return;
            }
            var url = new Url("http://worldcup.sfg.io/matches/country?fifa_code=");
            string urlEndpoint = GetItemFromComboBoxWpf(cb);
            var request = url + urlEndpoint;
            List<Teams.StartingEleven> defenders = new List<Teams.StartingEleven>();

            try
            {
                //await Task.Run(async () =>
                //{
                if (request != null)
                {
                    List<Teams.RootObject> matches = await request.GetJsonAsync<List<Teams.RootObject>>();
                    foreach (var match in matches)
                    {
                        foreach (var player in match.home_team_statistics.starting_eleven)
                        {
                            foreach (var position in player.position)
                            {
                                if (player.position.Contains("Defender"))
                                    {                                    
                                        defenders.Add(player);   

                                        var firstDef = defenders.ElementAt(0).name;
                                        var secondDef = defenders.ElementAt(1).name;
                                        var thirdDef = defenders.ElementAt(2).name;
                                        var fourthDef = defenders.ElementAt(3).name;

                                        tb1.Inlines.Add(firstDef);
                                        tb2.Inlines.Add(secondDef);
                                        tb3.Inlines.Add(thirdDef);
                                        tb4.Inlines.Add(fourthDef);                               

                                    }
                               }

                        }
                    }
                }
               
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
                       
    }

Now, here is the problem. Debugging the method, I see that there is only one element contained in the list defenders. There should be four elements in the list, but there is only one player in this list. Method only works if I comment out other variables and use only variable with zero index. So, I can populate only one text block. And here we come to another problem. In the text block, added player's name repeats itself border to border, if textblok has enough space, it is filled with repeated name of a selected player. My goal is to place one name per text block without repeating. Any ideas? Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I need make some changes on code.

To use ReadAsAsync<> it's needed Microsoft.AspNet.WebApi from Nuget.

public async void SetHomeTeamPlayersPositionsDEF(ComboBox cb, TextBlock tb1, TextBlock tb2, TextBlock tb3, TextBlock tb4)
{
    tb1.Inlines.Clear();
    tb2.Inlines.Clear();
    tb3.Inlines.Clear();
    tb4.Inlines.Clear();

    List<Player> defenders = new List<Player>();

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    if (cb.SelectedIndex == -1)
        return;

    string urlEndpoint = GetItemFromComboBoxWpf(cb);
    var url = new Uri($"http://worldcup.sfg.io/matches/country?fifa_code={urlEndpoint}");

    try
    {
        var response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        List<Match> matches = await response.Content.ReadAsAsync<List<Match>>();

        foreach (var match in matches)
        {
            foreach (var player in match.home_team_statistics.starting_eleven)
            {
                if (player.Position.Contains("Defender"))
                    defenders.Add(player);
            }

            var firstDef = defenders.ElementAt(0).Name;
            var secondDef = defenders.ElementAt(1).Name;
            var thirdDef = defenders.ElementAt(2).Name;
            var fourthDef = defenders.ElementAt(3).Name;

            tb1.Inlines.Add(firstDef);
            tb2.Inlines.Add(secondDef);
            tb3.Inlines.Add(thirdDef);
            tb4.Inlines.Add(fourthDef);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

I'm create other classes to deserialize JSON return.

public class Match
{
    public Statics home_team_statistics { get; set; }
}

public class Statics
{
    public List<Player> starting_eleven { get; set; }
}

public class Player
{
    public string Name { get; set; }
    public bool Captain { get; set; }
    public int Shirt_Number { get; set; }
    public string Position { get; set; }
}

Final Result

enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...