2016-07-03 10 views
-2

私は、私がボタンとして使用する画像+画像と(データベースとして使用したい)IDを持っています。文字列+リストを持つ辞書にIDを追加する<string>

今、私はちょうどこの(私はXAMLでレイアウトを行っている)のように一緒にマッチング画像と名前を受け取る:

IMAGE   IMAGE   IMAGE 
NAME(BUTTON) NAME(BUTTON) NAME(BUTTON) 

そして私は、私はので、そこにIDを追加することはできません辞書で働いているとして、 image + imagenameによって撮影されたalrdyです。

私が最終的にやってみたいのは、imagename(それはボタン)をクリックして私が(現在動作している)画像をプッシュしたいだけですが、現在は収まりません。

辞書を最初のステップとして「文字列、文字列」から「文字列、リスト、文字列」に変更しましたが、ここからどこに行くのかはわかりません。

これは私の現在のコードです:

string theID; 

async void loadImages() 
{ 
var getImages = await phpApi.getImages(); 

foreach (var theitems in getImages ["results"]) 
{ 
    imageList.Add(
     theitems ["Photo"].ToString(), //this is the Photo I get from the db 
     theitems ["PhotoName"].ToString(), //the photoname 
     //theitems ["ID"].ToString() and this is the ID. 
    ); 

} 

foreach (var key in imageList.Keys) { //this is part of the layout. And I have the clickedfunction below where I try to send the Image, name + id. 

    var inner = new StackLayout(); 

    var image = new Image(); 
    image.Source = key; 
    image.Aspect = Aspect.AspectFill; 

    var button = new Button(); 
    button.Text = imageList [key]; 

    inner.Children.Add(image); 
    inner.Children.Add(label); 
    myStack.Children.Add (inner); 

    button.Clicked += async (object sender, EventArgs e) => { 

    Navigation.PushModalAsync (new OtherProfilePage 
    (image.Source, button.Text, theID)); //I want to add the correct ID in here 

    } 

} 

} 
+0

を、私はリストにID +何か他のものを追加してくださいどのように、まだ最新の状態に保ちますレイアウト? – medvedo

+0

提案された複製は、関連性があります。私は確信していません。あなたが求めているものが本当にはっきりしていないか、辞書の仕組みを理解しているからです。あなたが表示したコードでは、キーが '... [" Photo "]。ToString()'オブジェクトであり、その値が '...である_single_キー/値のペアを辞書に追加しています["PhotoName"]。ToString() 'オブジェクト。辞書は、 "写真"から "写真の名前"への簡単なマッピングを可能にします。どのように "id"がこれに関連しているかははっきりしない。 –

+0

特定の入力があった場合に、コードが何をしたいかを正確に述べるように質問を修正してください。あなたが何をしているのかを明確に示す良い[mcve]を提供し、そのコードが何をしたいのかを正確に説明したことを確認してください。 –

答えて

1

public Dictionary <string,List<string>> imageList = new Dictionary <string,List<string>>(); 

//public Dictionary <string,string> imageList = new Dictionary <string, string>(); 

^は、私が前に持っていたものthatsのが、あなたが見ることができるようにではなく、リストに置き換えられている最も簡単な方法は、使用することですa Tuple<string, string>。また、値を格納するカスタムクラスを定義することもできます(.Item1.Item2など)。しかし、タプルは速く書くことになります。リスト使用

async void loadImages() 
{ 
    var getImages = await phpApi.getImages(); 
    foreach (var img in getImages ["results"]) { 
     imageList.Add(
      Tuple.Create(
       img["Photo"].ToString(), 
       img["PhotoName"].ToString(), 
       img["ID"].ToString() 
      ) 
     ); 
    } 
} 

public List<Tuple<string, string, string>> imageList = new List<Tuple<string, string, string>>(); 

リストを移植するコードを更新しました

foreach (var img in imageList) { 
    var inner = new StackLayout(); 
    var image = new Image(); 
    image.Source = img.Item1; 
    image.Aspect = Aspect.AspectFill; 
    var button = new Button(); 
    button.Text = img.Item2; 
    inner.Children.Add(image); 
    inner.Children.Add(label); 
    myStack.Children.Add (inner); 
    button.Clicked += async (sender, e) => { 
     var pg = new OtherProfilePage(img.Item1, img.Item2, img.Item3); 
     await Navigation.PushModalAsync (pg); 
    }; 
} 
+0

非常に良いソリューション、ありがとう!違いは何ですか私がカスタムクラスでそれをすることにしたら?名前のほうが良かったり、何か他のものがありますか? – medvedo

+0

主な違いは、コードの可読性です。これらのアイテムプロパティの名前を選択できるからです。カスタムクラスもパフォーマンス/メモリ特性がわずかに異なりますが、この場合には十分な価値はありません。 –

関連する問題