ho h
ho h

Reputation: 255

How to access a field in a database table in Xamarin Forms

How can I access a specific row of the database table of my choice in Xamarin Forms? I have a table in the database that contains the columns: Title, URL image, ID I did everything in the API and I so did in Xamarin Forms. My view page in Xamarin Forms contains a grid. I emphasize that I do not want to use the list and I want to manually give the information to the buttons and it is not in order. I appreciate those who guide me!

my table:

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

my code in api:


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

I copy My Table in Xamarin:

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

    }

and then I connect to 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();
         }
           
    }

and My 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>

I want photo second row the database table in the first row button of the second Weston. And the photo of the fifth row of the database table should be placed in the button of the first row of the first column And I choose the rest in the same way!

Upvotes: 1

Views: 243

Answers (1)

Jason
Jason

Reputation: 89117

you can use an indexer to specify a specific item within your list

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

in your code behind

// create a myButtons property
List<ButtonCategory> myButtons { get; set; }

// then in your OnAppearing
myButtons = await GetButtonCategoryAsync();
this.BindingContext = this;

Upvotes: 2

Related Questions