Search code examples
xamarin.formssqlite-netxamarin.essentials

How do i make a button read the text(url) stored in the database so it runs the xamarin essentials share function


Ok so i followed a few tutorials to make it so my app can read a database file and used this https://github.com/jfversluis/ExistingSQLiteDbSample

And it did work for me, but now what I'm trying to do is so my app can use the text stored in the database so it can do a share function using Xamarin.Essentials: Share

I would prefer it was a button but no idea were to even begin (since i want the button to be a image)

The code of my main page is this (its almost 1:1 with the first link), the data that i want to turn into a button is "LocationLink" which i temporary have setup as a Label

MainPage.xaml

<StackLayout>
    <CollectionView ItemsSource="{Binding List}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <BoxView HeightRequest="1" Color="#000000" IsVisible="true"/>
                    <Label Text="{Binding LocationName}"/>

                    <!-- Bellow is what i need help with-->
                    <Label Text="{Binding LocationLink}"/> 
                    <Button/>
                    <!-- Above is what i need help with-->                        
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

Item.cs

public class Temp
{
    public Int Id { get; set; }
    public string LocationName { get; set; }
    public string LocationLink { get; set; }
}

Solution

  • <Button Text="Click Me!" Clicked="ButtonClick" />
    

    then

    protected void ButtonClick(object sender, EventArgs args)
    {
       // get the button
       var button = (Button)sender;
    
       // I don't know what your class is called, you will need to put
       // the correct name here
       var item = (MyListItem)button.BindingContext;
    
       // now you can use item.LocationName, item.LocationLink, etc
       // when calling the Share function
    }