0

Windowsストアアプリクライアントでアイテムのリストを取得できるMVC Web Apiを使用しています。

次のコードを使用して、Windows ストア アプリでアイテムのリストを表示できます。

    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("http://localhost:12345/api/items");

var sampleDataGroups = new List<SampleDataGroup>();


            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                // CL: Parse 1 Item from the content
               var item = JsonConvert.DeserializeObject<dynamic>(content);
                //IEnumerable<string> item = JsonConvert.DeserializeObject<IEnumerable<string>>(content);

                foreach (var data in item)
                {
                      var dataGroup = new SampleDataGroup
                            (

                                (string)data.Id.ToString(),
                                (string)data.Name,
                                (string)"",
                                (string)data.PhotoUrl,
                                (string)data.Description

                            );
                                 sampleDataGroups.Add(dataGroup);
                }
             }
            else
            {
                MessageDialog dlg = new MessageDialog("Error");
                await dlg.ShowAsync();
            }


            this.DefaultViewModel["Groups"] = sampleDataGroups;

データはこの json 形式で受信されます。つまり、リスト内の各項目のデータです。

data    {
  "Id": 1,
  "Name": "bat",
  "Description": "lorem ipsum",
  "Price": 1.39,
  "Weight": "75g",
  "Photo": "test.png",
  "ItemList": null
}

dynamic {Newtonsoft.Json.Linq.JObject}

これは、タッチ アプリの SampleDataGroups 構造にマップされます。

public class SampleDataGroup : SampleDataCommon
    {
        public SampleDataGroup()
        {

        }

        public SampleDataGroup(String uniqueId, String title, String subtitle, String imagePath, String description)
            : base(uniqueId, title, subtitle, imagePath, description)
        {
            Items.CollectionChanged += ItemsCollectionChanged;
        }
}

Windows アプリで検索項目機能を作成したいので、テキスト ボックスとボタン コントロールを追加して xaml で検索機能を作成しました。

<TextBox x:Name="SearchTB" VerticalAlignment="Top" Text="Search our Products"   Grid.Column="1"Width="420" Height="50" />

<Button x:Name="Product_Search" Content="Go" Grid.Column="2"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="60" Height="50" Margin="0 0 0 0" Click="Product_Search_Click" />

ボタンがクリックされたときにテキストボックスに入力された文字列に一致するすべてのアイテムを取得して返すために、linq クエリを使用したいと考えています。

ボタンがクリックされたときのために、この関数を以下に作成しました。パラメータは、string queryテキストボックスに入力された文字列です。テキストボックスに入力された文字列に類似するアイテムが返されます。

private void Item_Search_Click(object sender, RoutedEventArgs e)
{
       var querystr = SearchTB.Text; 

}

Linqを使用して、検索テキストボックスに入力された文字列を使用して、ボタンのクリック時に表示するアイテムのリストを取得するにはどうすればよいですか?

4

1 に答える 1

0

おそらく次のようなことをする必要があります:

private void Item_Search_Click(object sender, RoutedEventArgs e)
{
    var groups =  DefaultViewModel["Groups"] as IEnumerable<SampleDataGroup>;
    if (groups == null) 
        return;

    DefaultViewModel["FilteredGroups"] =
       groups 
            .Where(group => ContainsQueryString(group. Title, Search.Text))
            .ToList();
}

private static bool ContainsQueryString(string property, string query)
{
    //performs comparison based on the current UI culture, case insensitive
    //from here: http://stackoverflow.com/questions/444798/case-insensitive-containsstring
    return CultureInfo.CurrentUICulture.CompareInfo.IndexOf(property, query, CompareOptions.IgnoreCase) >= 0;
}
于 2013-10-27T15:03:32.140 に答える