Visual Studio throwing DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION when I try to add a log

41 Views Asked by At

When I try to add a log using the below code (app for logging theatre trips) or search logs I'm sent to the App.g.i.cs autogenerated code. I've used breakpoints and stepped through the code and it seems to get through the whole subroutine and then when it gets to the end it throws this, perhaps its something with the UI?

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

This is my xaml code for the main page

<?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"
             x:Class="MyTheatreLog.MainPage"
             xmlns:viewmodel="clr-namespace:MyTheatreLog.ViewModel"
             x:DataType="viewmodel:MainViewModel"
             Title="Theatre Log">

    <Grid RowDefinitions="50, Auto, Auto, *, Auto"
          ColumnDefinitions=".8*,.05*,.15*"
          Padding="10"
          RowSpacing="10"
          ColumnSpacing="10">


        <Entry Placeholder="New Log"
               Grid.ColumnSpan="3"
               Text="{Binding Show}"
               Grid.Row="1"/>
        <DatePicker Grid.Row="2"
                    Grid.Column="0"
                    Date="{Binding Date}"/>

        <Button Text="+"
                FontSize="30"
                WidthRequest="20"
                HeightRequest="20"
                Command="{Binding AddCommand}"
                Grid.Row="2"
                Grid.Column="3"
                HorizontalOptions="End"/>

        <CollectionView Grid.Row="3" Grid.ColumnSpan="3"
                ItemsSource="{Binding Shows}"
                SelectionMode="None"
                        >

            <CollectionView.ItemTemplate>

                <DataTemplate x:DataType="viewmodel:ShowItem">
                    <SwipeView>
                        <Grid Padding="0,5">
                            <Frame CornerRadius="8"
                           HasShadow="True">
                                <Frame.GestureRecognizers>
                                    <TapGestureRecognizer NumberOfTapsRequired="2"
                                                          Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MainViewModel}}, Path=DetailsCommand}"
                                                          CommandParameter="{Binding .}"/>
                                </Frame.GestureRecognizers>
                                <Grid ColumnDefinitions="*, 90">
                                    <Label Text="{Binding Title}"
                                           Grid.Column="0"
                                           FontSize="14"/>
                                   <Label Text="{Binding DateString}"
                                          Grid.Column="1"/>
                                </Grid>

                            </Frame>
                        </Grid>
                    </SwipeView>
                </DataTemplate>

                </CollectionView.ItemTemplate>
        </CollectionView>

        
        <Entry Placeholder="Search Logs" 
               Grid.Row="4"
               Text="{Binding Searchentry}"/>
        <Button Grid.Row="4"
                Grid.Column="1"
                Grid.ColumnSpan="2"
                Text="Search"
                Command="{Binding SearchCommand}"/>

    </Grid>

</ContentPage>

and my ViewModel code

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;

namespace MyTheatreLog.ViewModel
{

    public class ShowItem
    {
        public string Title { get; set; }
        public string DateString { get; set; }
    }
    public partial class MainViewModel : ObservableObject
    {
        private LocalDatabase localDatabase;

        string appDataDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyTheatreLog");
        public MainViewModel()
        {

            if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyTheatreLog")))
            {
                Directory.CreateDirectory(Path.Combine(appDataDirectory, "MyTheatreLog"));
            }
            
            Shows = new ObservableCollection<ShowItem>();
            localDatabase = new LocalDatabase();
            LoadLogs();


        }


        public void LoadLogs()
        {
            try
            {
                
                string[] logfiles = Directory.GetFiles(appDataDirectory, "*.txt");
                Shows.Clear();
                foreach (string file in logfiles)
                {
                    using(StreamReader savefile = new StreamReader(file))
                    {
                        Title = savefile.ReadLine();
                        Datestring = savefile.ReadLine();
                        savefile.Close();
                    }

                    if (!string.IsNullOrEmpty(Title) && !string.IsNullOrEmpty(Datestring))
                    {
                        var showItem = new ShowItem { Title = Title, DateString = Datestring };
                        Shows.Add(showItem);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error loading logs " + ex.Message);
            }
        }

        [ObservableProperty]
        ObservableCollection<ShowItem> shows;

        [ObservableProperty]
        string show;

        bool searching = false;

        [ObservableProperty]
        string title;

        [ObservableProperty]
        DateTime date = DateTime.Today;

        [ObservableProperty]
        string searchentry;

        [ObservableProperty]
        string filename;

        [ObservableProperty]
        String datestring;


        [RelayCommand]
        void Add()
        {
            // checks if entry is empty
            if (string.IsNullOrWhiteSpace(Show))
                return;
            Datestring = Date.ToString("dd/MM/yyyy");
            // adds show
            Show = Show.Trim();
            string filetitle = Show.Replace(" ", "") + Datestring.Replace("/", "");
            Filename = filetitle + ".txt";


            using (StreamWriter savefile = new StreamWriter(System.IO.Path.Combine(appDataDirectory, Filename), true))
            {
                //saves to file
                savefile.WriteLine(Show);
                savefile.WriteLine(Datestring);
                savefile.Close();
            }
            LoadLogs();
            Show = string.Empty;

        }

        [RelayCommand]

        void Search()
        {
            try
            {
                var Showsholder = new ObservableCollection<ShowItem>();
                Showsholder.Clear();
                if (searching == false)
                {
                    // checks if entry is empty
                    if (string.IsNullOrWhiteSpace(Searchentry))
                        return;

                    // fills Shows placeholder

                    foreach (var log in Shows)
                    {
                        Showsholder.Add(log);
                    }
                    Shows.Clear();
                    Searchentry = Searchentry.Trim();
                    Searchentry = Searchentry.ToLower();
                    foreach (var log in Showsholder)
                    {
                        string logtitle = log.Title;
                        string logsearched = logtitle.ToLower();

                        // Adds logs that match search
                        if (logsearched.Contains(Searchentry))
                        {
                            Shows.Add(log);
                        }
                    }

                    searching = true;
                }
                else
                {
                    Shows.Clear();
                    LoadLogs();
                    searching = false;
                }
                Searchentry = string.Empty;
            }
            catch
            {
                Console.WriteLine("Error Searching");
            }

        }

        [RelayCommand]
        async Task Details(ShowItem s)
        {
            //navigates to Details Page
            string filetitle = s.Title.Replace(" ", "");
            string filedate = s.DateString.Replace("/", "");
            Filename = filetitle + filedate + ".txt";
            await Shell.Current.GoToAsync($"{"Details"}?Filename_={Filename}");
        }


    }
}

I'm very new to MAUI and I've been trying my best to read through it and work it out but no luck

A new file containing the item info does get created in the correct location with the correct name and is displayed when I next run the code.

0

There are 0 best solutions below