1

私は C# と XAML を初めて使用し、都市と州を検索して書式設定された 10 日間の予報リストを返す単純な天気アプリに取り組んでいます。

ListView を使用してWunderground Weather APIから取得したデータの表示に問題があります。これは、私が使用している JSON の例です。

私の問題: Wunderground API から結果を取得した後、項目のプロパティ (icon_url、title、fcttext) を表示する XAML のテンプレートを使用してすべてのリスト項目を実行するにはどうすればよいですか?

これが私のモデルです:

    パブリック クラス ForecastList
        {
            公開クラス
            {
                公開文字列 icon_url { get; 設定; }
                パブリック文字列のタイトル { get; 設定; }
                パブリック文字列 fcttext { get; 設定; }
            }

            パブリック クラス TxtForecast
            {
                公開リストの予測日{取得; 設定; }
            }

            公開クラス 予報
            {
                public TxtForecast txt_forecast { get; 設定; }
            }

            パブリック クラス RootObject
            {
                public 予測予測 { get; 設定; }
            }
        }

XAML:

<Stack Layout Padding="30"> <StackLayout Orientation="Horizontal"> <Entry HorizontalOptions="FillAndExpand" Placeholder="City" x:Name="City" /> <Entry Placeholder="2 letter state" x:Name="State" /> </StackLayout> <Button Text="Search" Clicked="OnClicked" /> <ListView ItemsSource="{Binding ListSource}"> <ListView.ItemTemplate> <DataTemplate> <Label Text="{Binding Title}" /> <Label Text="{Binding FctText}" /> <Image Source="{Binding IconUrl}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>

そしてビューモデル:

    パブリック クラス MainPageViewModel
        {
            public async Task GetWeatherAsync(string url)
            {
                HttpClient クライアント = 新しい HttpClient();
                client.BaseAddress = 新しい Uri(url);

                var response = await client.GetAsync(client.BaseAddress);
                response.EnsureSuccessStatusCode();

                var JsonResult = response.Content.ReadAsStringAsync().Result;

                var weather = JsonConvert.DeserializeObject(JsonResult);
                SetList(天気);
            }

            public List ListSource { get; 設定; }

            プライベート文字列 _title;
            公開文字列 タイトル
            {
                得る
                {
                    return _title;
                }
                設定
                {
                    _title = 値;
                }
            }

            プライベート文字列 _fctText;
            パブリック文字列 FctText
            {
                得る
                {
                    _fctText を返します。
                }
                設定
                {
                    _fctText = 値;
                }
            }

            プライベート文字列 _iconUrl;
            パブリック文字列 IconUrl
            {
                得る
                {
                    _iconUrl を返します。
                }
                設定
                {
                    _iconUrl = 値;
                }
            }

            private void SetList(ForecastList.RootObject 天気)
            {
                ListView listView = 新しい ListView();
                varforecastList = weather.forecast.txt_forecast.forecastday;
                List listSource = new List(forecastList);

                ListSource = listSource;

                listView.ItemsSource = ListSource;
            }
        }

乾杯!

4

1 に答える 1

2

Dtoを使用してサーバーデータを通常の形式に変換する必要があります

例えば ​​:

これは私のDtoです

public class SampleDto
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

このようにサーバーからデータを取得する

public static async Task<T> GetResultFromApi<T>(string serviceUrl)
    {
        try
        {
            GetConnection();
             var response = await _httpClient.GetAsync(new Uri(yourUrl + serviceUrl));

            var stringAsync = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var responseJson = stringAsync;

                return JsonConvert.DeserializeObject<T>(responseJson);
            }

            LoggingManager.Error("Received error response: " + stringAsync);
            return default(T);
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
            return default(T);
        }
    }

var gettingDto = await GetResultFromApi<SampleDto>(string.Format(client.BaseAddress));

そして最後に myDto を元のフォーマットに変換します

var entity = ConvertGameDtoToEntity(gettingDto);

public SampleEntity ConvertGameDtoToEntity(SampleDto gettingDto)
{
        return new SampleEntity 
        {
            Id = gettingDto.Id,
            Name= gettingDto.Name,
            Address = gettingDto.Address,
        };
}

明確な説明のあるリンクの下 https://www.codeproject.com/Articles/36781/Serialization-and-Deserialization-in-ASP-NET-with

于 2016-12-31T06:31:20.823 に答える