如何使用Xamarin Forms访问数据库表中的字段



如何在Xamarin Forms中访问所选数据库表的特定行?我在数据库中有一个表,其中包含列:标题、URL图像、ID我在API中做了所有的事情,我在Xamarin表单中也做了。我在Xamarin Forms中的视图页面包含一个网格。我强调,我不想使用列表,我想手动将信息提供给按钮,但它不按顺序。我感谢那些指导我的人!

我的桌子:

public class ButtonCategory
{
[Key]
public int ButtonCategoryId { get; set; }
public string ButtonCategoryTitle { get; set; }
public string ButtonCategoryImageUrl { get; set; }
}

我在api中的代码:


[HttpGet("GetCategories")]
public List<ButtonCategory> GetCategories()
{
return context.ButtonCategories.ToList();
}

我在Xamarin:复制我的桌子

public class ButtonCategory
{

public int ButtonCategoryId { get; set; }
public string ButtonCategoryTitle { get; set; }
public string ButtonCategoryImageUrl { get; set; }
}

然后我连接到Api:


public   class ButtonHelper
{

public async  Task<List<ButtonCategory>> GetButtonCategoryAsync()
{
string Url = "http://111.111.1.111:1111/api/Buttons/GetCategories";
var client = new HttpClient();
var Uri = new Uri(Url);
var result = await client.GetStringAsync(Uri);
var newresult = JsonConvert.DeserializeObject<List<ButtonCategory>>(result);
return newresult.ToList();
}

}

和我的Ui:


<ContentPage.Content>

<StackLayout >
<Grid BackgroundColor="Black" ColumnSpacing="10" RowSpacing="10" Padding="15">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ImageButton  Source="{Binding }" Aspect="AspectFill" VerticalOptions="FillAndExpand" 
HorizontalOptions="FillAndExpand"  Grid.Column="0" Grid.Row="0"  CornerRadius="30"/>

<ImageButton  Source="{Binding}" Aspect="AspectFill"  VerticalOptions="FillAndExpand"  
HorizontalOptions="FillAndExpand" Grid.Column="1" Grid.Row="0" CornerRadius="30"/>

<ImageButton Source="{Binding }" Aspect="AspectFill"  VerticalOptions="FillAndExpand"  
HorizontalOptions="FillAndExpand" Grid.Column="0" Grid.Row="1" CornerRadius="30" />
<ImageButton Source="{Binding }" Aspect="AspectFill"  VerticalOptions="FillAndExpand"  
HorizontalOptions="FillAndExpand" Grid.Column="1" Grid.Row="1" CornerRadius="30"/>
<ImageButton Source="{Binding}" Aspect="AspectFill"  VerticalOptions="FillAndExpand"  
HorizontalOptions="FillAndExpand" Grid.Column="0" Grid.Row="2" CornerRadius="30" />
<ImageButton Source="{Binding}" Aspect="AspectFill"  VerticalOptions="FillAndExpand" 
HorizontalOptions="FillAndExpand" Grid.Column="1" Grid.Row="2" CornerRadius="30"/>

</Grid>
</StackLayout>

</ContentPage.Content>

我要照片的第二行数据库表的第一行按钮的第二个韦斯顿。数据库表第五行的照片应该放在第一列第一行的按钮上,我用同样的方式选择其余的!

您可以使用索引器在列表中指定特定项目

<ImageButton  Source="{Binding myButtons[2].ButtonCategoryImageUrl}" ... />

在后面的代码中

// create a myButtons property
List<ButtonCategory> myButtons { get; set; }
// then in your OnAppearing
myButtons = await GetButtonCategoryAsync();
this.BindingContext = this;

最新更新