<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="VFood.Views.EntregadorEdit"
Title="{Binding Title}">
<ContentPage.ToolbarItems>
<OnPlatform x:TypeArguments="ToolbarItem">
<OnPlatform.iOS>
<ToolbarItem x:Name="SalvarItemIOS" Name="Done" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>
<ToolbarItem x:Name="RemoveItemIOS" Name="Trash" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
</OnPlatform.iOS>
<OnPlatform.Android>
<ToolbarItem x:Name="SalvarItemDroid" Icon="ic_check" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>
<ToolbarItem x:Name="RemoveItemDroid" Icon="ic_delete" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
</OnPlatform.Android>
</OnPlatform>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout Spacing="8">
<StackLayout.Padding>
<OnPlatform x:TypeArguments="Thickness" Android="16,16,16,16" iOS="10,10,10,10"/>
</StackLayout.Padding>
<Entry Placeholder="Nome" Text="{Binding Entregador.Nome}"/>
<Entry Placeholder="Telefone" Text="{Binding Entregador.Telefone}" Keyboard="Telephone" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
假设我有一个这样的代码。在数组的每个元素中,都有一个具有唯一编号<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="VFood.Views.EntregadorEdit"
Title="{Binding Title}">
<ContentPage.ToolbarItems>
<OnPlatform x:TypeArguments="ToolbarItem">
<OnPlatform.iOS>
<!--<ToolbarItem x:Name="SalvarItemIOS" Name="Done" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>-->
<ToolbarItem x:Name="RemoveItemIOS" Name="Trash" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
</OnPlatform.iOS>
<OnPlatform.Android>
<!--<ToolbarItem x:Name="SalvarItemDroid" Icon="ic_check" Command="{Binding SalvarCommand}" Order="Primary" Priority="0"/>-->
<ToolbarItem x:Name="RemoveItemDroid" Icon="ic_delete" Command="{Binding RemoveCommand}" Order="Primary" Priority="1"/>
</OnPlatform.Android>
</OnPlatform>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout Spacing="8">
<StackLayout.Padding>
<OnPlatform x:TypeArguments="Thickness" Android="16,16,16,16" iOS="10,10,10,10"/>
</StackLayout.Padding>
<Entry Placeholder="Nome" Text="{Binding Entregador.Nome}"/>
<Entry Placeholder="Telefone" Text="{Binding Entregador.Telefone}" Keyboard="Telephone" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
的变量。现在,我想使用这个数字来访问整个元素(并可能操纵它的其他变量)。我怎么能这样做?
答案 0 :(得分:1)
在数组的每个元素中,都有一个唯一的变量 号码
(bar)
。
由于您可以控制您的唯一键,因此可以很容易地实现这一点
如果您按照排序顺序排列数组,则可以使用C库函数bsearch
进行快速查找。
(注意:请在代码中初始化int var;
。)
现在我想使用这个数字来访问整个元素(和 也许操纵它的其他变量)
这可以像为函数get_foo
指定键一样简单:
Foo *obj = get_foo(3);
简单示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 5
typedef struct Foo Foo;
struct Foo{
int bar; // key
int var2; // etc...
int var3; // etc...
};
Foo array[ARRAY_SIZE];
static int compare_keys(const void *va, const void *vb) {
const Foo *a = va, *b = vb;
return (a->bar - b->bar);
}
Foo * get_foo(int key) {
Foo *foo = bsearch(&key, array, sizeof array / sizeof array[0],
sizeof array[0], compare_keys);
return foo;
}
int main(void){
int var = 0;
while(var < ARRAY_SIZE)
{
array[var].bar = var; // an unique key number
array[var].var2 = 2*var; // some data
array[var].var3 = 3*var; // some data
var++;
}
// The array of structures should be sorted by the value of the unique key
// verification that we can look up all the valid keys.
for (int i = 0; i < sizeof array / sizeof array[0]; ++i) {
Foo * v = get_foo(array[i].bar);
if(v)
printf("key: %3d -> values: %2d, %3d\n", array[i].bar, v->var2, v->var3 );
else
printf("Key not found %2d \n", array[i].bar);
}
// finding a particular object is simple:
printf("\nFind object for key = %2d \n", 3);
Foo *obj = get_foo(3);
if(obj)
printf("key: %3d -> values: %2d, %3d\n", 3, obj->var2, obj->var3 );
return 0;
}
测试:
key: 0 -> values: 0, 0
key: 1 -> values: 2, 3
key: 2 -> values: 4, 6
key: 3 -> values: 6, 9
key: 4 -> values: 8, 12
Find object for key = 3
key: 3 -> values: 6, 9