将ID添加到具有字符串+ list <string>

时间:2016-07-03 20:48:21

标签: c# xamarin xamarin.forms

我有一个图像+图像名称(我用作按钮)和我希望使用的数据库中的ID。

现在我收到匹配的图像和名称就像这样(我在XAML中完成了布局):

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

由于我使用过字典,因此我无法在其中添加ID,因为它是由图像+图像名称进行的。

我最终想要做的是当你点击图像名称(即一个按钮)时,我想用我(当前有效)推送图像,但也包括当前不适合的ID。

我已经将字典从“string,string”更改为“string,List,string”作为第一步,但我不太确定从何处开始。

这是我目前的代码:

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

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

^这就是我之前所拥有的,但正如你所看到的那样,它已被一个列表取代

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

    }

 }

}

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用.Item1。您还可以定义一个自定义类来存储您的值(并且具有比.Item2public List<Tuple<string, string, string>> imageList = new List<Tuple<string, string, string>> (); 等更好的名称),但是元组编写起来会更快。

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

填充列表:

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

使用清单:

SQL injection