0

I have I am trying to build my own application to Automate my daily activity. I am building my first application using Visual Studio 2022. .NET Maui however, I am encountering issues and been stuck with it. When running the code in debug mode, the application runs smoothly. But when I exit and run the app on my device alone, the Edit Button is not working, and it crashed at exit.

sharing to you my code.

On the ClientListPage. The list is clickable to access the client's information and the Service history.

ClientListPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:clientdb"
             x:Class="clientdb.ClientListPage">

    <ContentPage.Content>
        <StackLayout>
            <SearchBar x:Name="searchBar" Placeholder="Search" TextChanged="OnSearchTextChanged" />

            <ListView x:Name="clientsListView" ItemSelected="ClientsListView_ItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Padding="10,5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <!-- First Row -->
                                <StackLayout Orientation="Vertical" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" >
                                <Label Text="{Binding Name}" FontSize="11.8" />
                                    <Label Text="{Binding PhoneNumber}" FontSize="12"/>
                                </StackLayout>
                                <Label Text="{Binding Status}" FontSize="11.8"  Grid.Row="0" Grid.Column="2" />
                                
                                 <Button Text="x" Grid.Row="0" Grid.Column="5" BackgroundColor="LightGray" HeightRequest="35" WidthRequest="43"  Clicked="DeleteButton_Clicked" />
                                <!--   <Button Text="x" Grid.Row="0" Grid.Column="5" BackgroundColor="LightGray" HeightRequest="35" WidthRequest="43"  Clicked="DeleteButton_Clicked" />
-->
                                <!-- Second Row -->
                                
                                <Label Grid.Row="1" Grid.Column="2" />
                                <!-- Empty column -->
                                <Label Grid.Row="1" Grid.Column="3" />
                                <!-- Empty column -->
                                <Label Grid.Row="1" Grid.Column="4" />
                                <!-- Empty column -->
                                <Label Grid.Row="1" Grid.Column="5" />
                                <!-- Empty column -->

                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ClientListPage.xaml.cs

using System.Diagnostics;

namespace clientdb // Replace with your actual namespace
{
    public partial class ClientListPage : ContentPage
    {

       private async void ClientsListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
       {
         if (e.SelectedItem == null)
               return;

           var selectedClient = e.SelectedItem as Client;
           clientsListView.SelectedItem = null; // Reset the selected item

            // Navigate to the ClientServiceHistoryPage and pass the selected client's details
          await Navigation.PushAsync(new ClientServiceHistoryPage(selectedClient));
        }

       
        
        private async void DeleteButton_Clicked(object sender, EventArgs e)
        {
         //for delete button
        }

        private List<Client> allClients;
        private List<Client> filteredClients;

        public ClientListPage(List<Client> clients)
        {
            InitializeComponent();
            allClients = clients;
            filteredClients = allClients;
            clientsListView.ItemsSource = filteredClients;
        }

        private void OnSearchTextChanged(object sender, TextChangedEventArgs e)
        {
            string searchText = e.NewTextValue;
            filteredClients = allClients.Where(client =>
                client.Name.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
                client.PhoneNumber.Contains(searchText, StringComparison.OrdinalIgnoreCase) ||
                client.Status.Contains(searchText, StringComparison.OrdinalIgnoreCase))
                .ToList();
            clientsListView.ItemsSource = filteredClients;


           
        }
    }
}

It will navivate to ClientHistoryServicePage where the Edit button is located.

ClientHistoryServicePage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:local="clr-namespace:clientdb"
             x:Class="clientdb.ClientServiceHistoryPage">

    <ContentPage.Content>
        <StackLayout>
 
            <StackLayout Orientation="Horizontal" Padding="10">
                <StackLayout VerticalOptions="CenterAndExpand">
                    <Label Text="{Binding SelectedClient.Name}" FontSize="20"/>
                    <Label HeightRequest="10" />
                    <Label Text="Status:" FontAttributes="Bold" />
                    <Label Text="{Binding SelectedClient.Status}" />
                    <Label HeightRequest="10" />
                    <Label Text="Phone Number:" FontAttributes="Bold" />
                    <Label Text="{Binding SelectedClient.PhoneNumber}" />
                    <Label HeightRequest="10" />
                    <Label Text="Notes:" FontAttributes="Bold" />
                    <Label Text="{Binding SelectedClient.Notes}" />
                    <Label HeightRequest="10" />
                    <Label Text="Location:" FontAttributes="Bold" />
                    <Label Text="{Binding SelectedClient.Location}" />
                   
                    
                    <Button Text="Add Service" BackgroundColor="LightGray" HeightRequest="35" WidthRequest="43"  Clicked="AddServiceButton_Clicked" />
                    <Button Text="Edit"  BackgroundColor="LightGray" HeightRequest="35" WidthRequest="50"  Clicked="EditButton_Clicked" Command="{Binding EditServiceCommand}" />

                </StackLayout>
                <Label HeightRequest="10" />
                <Label HeightRequest="10" />
                <Label HorizontalOptions="EndAndExpand"/>
                
                <Label Text="{Binding SelectedClient.ID, StringFormat='Client ID: {0}'}" FontAttributes="Bold" FontSize="12" />
            
            
            </StackLayout>

            <BoxView Color="Gray" HeightRequest="1" HorizontalOptions="FillAndExpand" />

            <ListView x:Name="serviceListView"  >
                <ListView.Header>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="Date" FontAttributes="Bold" WidthRequest="70" />
                        <Label Text="Type" FontAttributes="Bold" WidthRequest="60" />
                        <Label Text="Price" FontAttributes="Bold" WidthRequest="50" />
                        <Label Text="Tip" FontAttributes="Bold" WidthRequest="50" />
                        <Label Text="Details" FontAttributes="Bold" HorizontalOptions="FillAndExpand" />
                    </StackLayout>
                </ListView.Header>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell Height="400">
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Date}" WidthRequest="70" />
                                <Label Text="{Binding Type}" WidthRequest="60" />
                                <Label Text="{Binding Price}" WidthRequest="50" />
                                <Label Text="{Binding Tip}" WidthRequest="50" />

                                <!-- Use a ScrollView to allow scrolling within the "Details" column -->
                                <ScrollView HorizontalOptions="FillAndExpand">
                                    <Label Text="{Binding Details}" FontSize="12"/>
                                </ScrollView>
                               
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

ClientHistoryServicePage.xaml.cs

using SQLite;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace clientdb
{
    public partial class ClientServiceHistoryPage : ContentPage
    {
        public ObservableCollection<Service> ServiceHistory { get; set; }
        public Client SelectedClient { get; set; }
     
        public ClientServiceHistoryPage(Client selectedClient)
        {
            InitializeComponent();

            SelectedClient = selectedClient;
            ServiceHistory = new ObservableCollection<Service>();
     

            // Load service history for the selected client from ServiceDatabase
            LoadServiceHistory();

            BindingContext = this;
        }


        public class ServiceDatabase
        {
            readonly SQLiteAsyncConnection _database;

            public ServiceDatabase(string dbPath)
            {
                _database = new SQLiteAsyncConnection(dbPath);
                _database.CreateTableAsync<Service>().Wait();
            }

        

            public async Task<List<Service>> GetServicesAsync()
            {
                return await _database.Table<Service>().ToListAsync();
            }

            public async Task<List<Service>> GetServicesForClientAsync(int clientId)
            {
                return await _database.Table<Service>().Where(s => s.ClientID == clientId).ToListAsync();
            }

            public async Task<int> SaveServiceAsync(Service service)
            {
                if (service.ID != 0)
                {
                    return await _database.UpdateAsync(service);
                }
                else
                {
                    return await _database.InsertAsync(service);
                }
            }

            // Add other methods for data manipulation as needed
        }


        private async void LoadServiceHistory()
        {
            var dbPath = Path.Combine(FileSystem.AppDataDirectory, "service.db");
            var serviceDatabase = new ServiceDatabase(dbPath);
            var servicesForClient = await serviceDatabase.GetServicesForClientAsync(SelectedClient.ID);

            ServiceHistory = new ObservableCollection<Service>(servicesForClient);

            // Bind the serviceHistory variable to the serviceListView control
            serviceListView.ItemsSource = ServiceHistory;


        }

      
        private async void EditButton_Clicked(object sender, EventArgs e)
        {
            // Get the selected client
            var selectedClient = SelectedClient;

            if (selectedClient != null)
            {
                await Navigation.PushAsync(new EditClientPage(selectedClient));
            }

        }

        
        

        private async void AddServiceButton_Clicked(object sender, EventArgs e)
        {
            // Get the selected client
            var selectedClient = SelectedClient;

            if (selectedClient != null)
            {
                await Navigation.PushAsync(new AddServicePage(selectedClient));
            }
        }

        public class Service
        {
            [PrimaryKey, AutoIncrement]
            public int ID { get; set; }
            public int ClientID { get; set; } // This will link the service to a specific client
            public string Date { get; set; }
            public string Type { get; set; }
            public double Price { get; set; }
            public double Tip { get; set; }
            public string Details { get; set; }
        }

    }
}

I have already tried this MAUI app not working in release mode but it works perfectly in debug mode

0 Answers0