Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views7 pages

Codigo

The document contains XAML and C# code for a bookstore application, including admin, cart, customer, and main pages. It features functionalities such as searching for books, adding to cart, and managing books through CRUD operations with Firebase. The application is structured to allow navigation between different pages and manage book data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Codigo

The document contains XAML and C# code for a bookstore application, including admin, cart, customer, and main pages. It features functionalities such as searching for books, adding to cart, and managing books through CRUD operations with Firebase. The application is structured to allow navigation between different pages and manage book data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

ADMINPAGE.

XAML

<?xml version="1.0" encoding="utf-8" ?>


<ContentPage xmIns="http://schemas.microsoft.com/dotnet/2021/maui"
xmins:x="http://schemas.microsoft.com/winfx/2009/xam!"
x:Class="BookstoreApp.AdminPage"
Title="Panel Administrador">

<Grid RowDefinitions="Auto,*,Auto">
<SearchBar x:Name="searchBar"
Placeholder="Buscar libros..."
Grid.Row="0"
Margin="10"
TextChanged="OnSearchTextChanged"/>

<RefreshView Grid.Row="1">
<CollectionView x:Name="booksCollection">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame Padding="15" Margin="10" CornerRadius="10">
<Grid ColumnDefinitions="Auto,*
Auto" ColumnSpacing="15">
<Image Source="{Binding ImageUrl}"
HeightRequest="80"
WidthRequest="60"
Aspect="AspectFill"/>

<VerticalStackLayout Grid.Column="1" Spacing="5">


<Label Text="(Binding Title}"
FontAttributes="Bold"
FontSize="Medium"/>
<Label Text="(Binding Author}"
FontSize="Small"/>
<Label Text="(Binding Price, StringFormat="'Precio: {0:C}'}"/>
<Label Text="(Binding Stock, StringFormat='Stock: {0}'}"/>
</VerticalStackLayout>
<HorizontalStackLayout Grid.Column="2" Spacing="10">
<Button Text=" "
BackgroundColor="#f39c12"
TextColor="White"
WidthRequest="50"
HeightRequest="50"
CornerRadius="25"
Clicked="OnEditClicked"
CommandParameter="(Binding .}"/>
<Button Text="_"
BackgroundColor="#e74c3c"
TextColor="White"
WidthRequest="50"
HeightRequest="50"
CornerRadius="25"
Clicked="OnDeleteClicked"
CommandParameter="(Binding .}"/>
</HorizontalStackLayout>
</Grid>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
<Button Text="Agregar Libro"
Grid.Row="2"
BackgroundColor="#9b59b6"
TextColor="White"
Margin="20"
HeightRequest="50"
FontSize="Medium"
CornerRadius="10"
Clicked="OnAddBookClicked"/>
</Grid>
</ContentPage>

cartPage.xaml

<?xml version="1.0" encoding="utf-8" ?>


<ContentPage xmIns="http://schemas.microsoft.com/dotnet/2021/maui"
xmins:x="http://schemas.microsoft.com/winfx/2009/xami"
x:Class="BookstoreApp.CartPage"
Title="Carrito de Compras">
<Grid RowDefinitions="* Auto,Auto">
<CollectionView x:Name="cartCollection" Grid.Row="0">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame Padding="10" Margin="5" CornerRadius="10">
<Grid ColumnDefinitions="Auto,* Auto,Auto">
<Image Source="{Binding Book.ImageUrl}"
HeightRequest="60"
WidthRequest="40"
Aspect="AspectFill"/>
<VerticalStackLayout Grid.Column="1" Spacing="5">
<Label Text="{Binding Book.Title}" FontAttributes="Bold"/>
<Label Text="{Binding Book.Author}"/>
<Label Text="(Binding Book.Price, StringFormat='Precio unitario: {0:C}'}"/>
</VerticalStackLayout>
<Label Text="{Binding Quantity}"
Grid.Column="2"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center"/>
<Label Text="{Binding Total, StringFormat="{0:C}'}"
Grid.Column="3"
FontSize="Large"
FontAttributes="Bold"
HorizontalOptions="End"
VerticalOptions="Center"/>
</Grid>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Label x:Name="totalLabel"
Grind.Row="1">
FontSize="Large"
FontAttributes="Bold"
HorizontalOptions="End"
Margin="20"
TextColor="Black"/>
<Button Text="Finalizar Compra"
Grid.Row="2"
BackgroundColor="#27ae60"
TextColor="White"
Margin="20"
HeightRequest="50"
FontSize="Medium"
CornerRadius="10"
Clicked="OnCheckoutClicked"/>
</Grid>
</ContentPage>
CustomerPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmins="http://schemas.microsoft.com/dotnet/2021/maui"
xmins:x="http://schemas.microsoft.com/winfx/2009/xam!"
x:Class="BookstoreApp.CustomerPage"
Title="Catálogo de Libros">
<Grid RowDefinitions="Auto,*
Auto">
<SearchBar x:Name="searchBar"
Placeholder="Buscar libros..."
Grid.Row="0"
Margin="10"
/ TextChanged="OnSearchTextChanged"/>
<RefreshView Grid.Row="1">
<CollectionView x:Name="booksCollection"
SelectionMode="None"
ItemsLayout="VerticalGrid, 2">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame HeightRequest="320" Padding="10" Margin="5" CornerRadius="10">
<VerticalStackLayout Spacing="5">
<Image Source="(Binding ImageUrl)"
Aspect="AspectFill"
HeightRequest="150"/>
<Label Text="{Binding Title}"
FontAttributes="Bold"
FontSize="Medium"/>
<Label Text="{Binding Author}"
FontSize="Small"/>
<Label Text="{Binding Price, StringFormat="{0:C}'}"
FontAttributes="Bold"
TextColor="Green"/>
<Grid ColumnDefinitions="* Auto,*"
VerticalOptions="Center"
Margin="0,10">
<Label Text="Cantidad:"
Grid.Column="0"
VerticalOptions="Center"/>
<Stepper x:Name="quantityStepper"
Grid.Column="1"
Minimum="1"
Maximum="{Binding Stock}"
Value="1"
Increment="1"
HorizontalOptions="Center"
VerticalOptions="Center"/>
<Label Text="{Binding Source={x:Reference quantityStepper}, Path=Value}"
Grid.Column="2"
FontSize="Medium"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</Grid>
<Button Text="Agregar al carrito"
BackgroundColor="#3498db"
TextColor="White"
CornerRadius="5"
Clicked="OnAddToCartClicked"
CommandParameter="(Binding .}"/>
</VerticalStackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
<Button x:Name:“cartButton"
Grid.Row="2"
Text="Ver Carrito (0)"
BackgroundColor="#2ecc71"
TextColor="White"
Margin="20"
HeightRequest="50"
FontSize="Medium"
CornerRadius="10"
Clicked="OnViewCartClicked"/>
</Grid>
</ContentPage>
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmins="http://schemas.microsoft.com/dotnet/2021/maui"
xmins:x="http://schemas.microsoft.com/winfx/2009/xami"
x:Class="BookstoreApp.MainPage"
Title="Bookstore App">
<VerticalStackLayout Spacing="30" Padding="40" VerticalOptions="Center">
<Image Source="books_logo.png"
HeightRequest="150"
Aspect="AspectFit"
HorizontalOptions="Center"/>
<Button Text="Administrador"
BackgroundColor="#3498db"
TextColor="White"
HeightRequest="60"
FontSize="Medium"
CornerRadius="10"
Clicked="OnAdminClicked"/>
<Button Text="Cliente"
BackgroundColor="#2ecc71"
TextColor="White"
HeightRequest="60"
FontSize="Medium"
CornerRadius="10"
Clicked="OnCustomercClicked"/>
</VerticalStackLayout>
</ContentPage>
firebaseservice.cs

using Firebase.Database;
using Firebase.Database.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookstoreApp.Services
{
public class FirebaseService
{
private readonly FirebaseClient firebase;
private const string BASE_URL = "https://prueba-556fc-default-rtdb.firebaseio.com";
public FirebaseService()
{
firebase = new FirebaseClient(BASE_URL);
}
// Operaciones CRUD Libros
public async Task<List<Book>> GetBooks()
{
return (await firebase
.Child("books")
.OnceAsync<Book>()).Select(item => new Book
{
Id = item.Key,
Title = item.Object.Title,
Author = item.Object.Author,
Price = item.Object.Price,
ImageUrl = item.Object.ImageUrl,
Stock = item.Object.Stock
)). ToList();
)
public async Task AddBook(Book book)
{
await firebase
.Child("books")
.PostAsync(book);
}
public async Task UpdateBook(Book book)
{
await firebase
.Child("books")
.Child(book.Id)
.PutAsync(book);
}
public async Task DeleteBook(string id)
{
await firebase
.Child("books")
.Child(id)
.DeleteAsync();
}
}
}
book.cs
public class Book
{
public string Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Description {get; set; }
public decimal Price { get; set; }
public string imageUri { get; set; }
public int Stock { get; set; }
}
cartitem.cs
public class Book
{
public string Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageUrl { get; set; )
public int Stock { get; set; )
cartpage.xaml.cs
namespace BookstoreApp;
public partial class CartPage : ContentPage
{
private readonly List<Cartitem> cartitems;
public CartPage(List<Cartitem> items)
{
InitializeComponent();
cartitems = items;
cartCollection.ItemsSource = cartitems;
UpdateTotal();
)
private void UpdateTotal()
{
var total = cartitems.Sum(item => item.Total);
totalLabel.Text = $"Total: {total:C}";
}
private async void OnCheckoutClicked(object sender, EventArgs e)
{

// Lógica para procesar compra


await DisplayAlert("Exito", "Compra realizada con éxito", "OK");
await Navigation.PopToRootAsync();
}
}
customerpage.xaml.cs
namespace BookstoreApp;
public partial class CartPage : ContentPage
{
private readonly List<Cartitem> cartitems;
public CartPage(List<Cartitem> items)
{
}
InitializeComponent();
cartitems = items;
cartCollection.ItemsSource = cartitems;
UpdateTotal();
private void UpdateTotal()
{
var total = cartitems.Sum(item => item.Total);
totalLabel.Text = $"Total: {total:C}";
}
private async void OnCheckoutClicked(object sender, EventArgs e)
{

//Lógica para procesar compra


await DisplayAlert("Éxito", "Compra realizada con éxito", "OK");
await Navigation.PopToRootAsync();
}
}
mainpage.xaml.cs
namespace BookstoreApp
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnAdminClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new AdminPage());
}
private async void OnCustomerClicked(object sender, EventArgs e)
{

await Navigation.PushAsync(new CustomerPage());


}
}
}

You might also like