DaClownie
DaClownie
CC#
Created by DaClownie on 9/19/2024 in #help
✅ OnParametersSetAsync() in a blazor component being called before clicking a button in Blazor app
I created a component in Blazor, a CourseList which has a Parameter of TermId its added to the Terms.razor page as such
<button class="btn btn-primary" onClick="@(() => ShowCourses(term.Id))">Show Course List</button>

<CourseList TermId="termId" />

@code
Guid termId;

private void ShowCourses(Guid id)
{
if (termId == id)
{
return;
}
termId = id;
StateHasChanged();
}
<button class="btn btn-primary" onClick="@(() => ShowCourses(term.Id))">Show Course List</button>

<CourseList TermId="termId" />

@code
Guid termId;

private void ShowCourses(Guid id)
{
if (termId == id)
{
return;
}
termId = id;
StateHasChanged();
}
termId is a Guid that is only being assigned a value when one of the buttons next to the rendered terms is clicked. The CourseList component itself only displays if a bool showDetails is set to true. However, when the page is loaded, it automatically displays the table headers and tries to load the list of Courses that are within that if (showDetails) CourseList.razor:
@if (showDetails)
{
table code here ...
}

@code
[Parameter]
public Guid TermId { get; set; }
bool showDetails = false;

protected override async Task OnParameterSetAsync()
{
var result = await Http.GetAsync($"api/course/ListByTermId/{TermId}");
if (result.IsSuccessStatusCode)
{
courses = await result.Content.ReadFromJsonAsync<List<CourseResponse>>();
showDetails = true;
}
else
{
showDetails = false;
}
}
@if (showDetails)
{
table code here ...
}

@code
[Parameter]
public Guid TermId { get; set; }
bool showDetails = false;

protected override async Task OnParameterSetAsync()
{
var result = await Http.GetAsync($"api/course/ListByTermId/{TermId}");
if (result.IsSuccessStatusCode)
{
courses = await result.Content.ReadFromJsonAsync<List<CourseResponse>>();
showDetails = true;
}
else
{
showDetails = false;
}
}
Why is this OnParameterSetAsync() being called on page load before clicking a button?
4 replies
CC#
Created by DaClownie on 9/1/2024 in #help
Unable to get tabs to show after initial login screen for MAUI app
No description
2 replies
CC#
Created by DaClownie on 8/26/2024 in #help
✅ How to access specific fields from an [ObservableProperty] ObservableCollection<Object>?
Hoping to pick someone's brain on MVVM and MAUI. So I have a ViewModel that looks like:
public partial class TermsViewModel : ObservableObject
{
public TermsViewModel()
{
Terms = new ObservableCollection<Term>();
}

[ObservableProperty]
ObservableCollection<Term> terms;
}
public partial class TermsViewModel : ObservableObject
{
public TermsViewModel()
{
Terms = new ObservableCollection<Term>();
}

[ObservableProperty]
ObservableCollection<Term> terms;
}
and I want to have the xaml create each of the objects in the ObservableCollection... But the items are of type Term, so how do I tell the xaml to access the IdText of the Term object and print that?
<CollectionView
ItemsSource="{Binding Terms}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
BackgroundColor="Red"/>
</SwipeItems>
</SwipeView.RightItems>
<SwipeView.LeftItems>
<SwipeItems>
<SwipeItem Text="Edit"
BackgroundColor="Orange"/>
</SwipeItems>
</SwipeView.LeftItems>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<CollectionView
ItemsSource="{Binding Terms}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
BackgroundColor="Red"/>
</SwipeItems>
</SwipeView.RightItems>
<SwipeView.LeftItems>
<SwipeItems>
<SwipeItem Text="Edit"
BackgroundColor="Orange"/>
</SwipeItems>
</SwipeView.LeftItems>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I'm not sure how best to handle a "complex" object vs. just an ObservableCollection<string>. I essentially want the term.Name to be displayed for each Term object in the Collection, but when the term.Name is tapped in my app, I want it to reference pass the term.Id to an eventual call to open a CoursePage(term.Id)
227 replies
CC#
Created by DaClownie on 8/24/2024 in #help
Handling exceptions when querying with EF
Looking for a little insight... so I'm just working through how to properly use EntityFramework, and I'm handling a simple login from a list of users from a database (yes I know the password should be hashed and salted, but i was going to handle that after. Just want it to work for now) My Users database has Id which is a BINARY(16) for a smaller method of storing GUID, and it has a virtual column it can reference for IdText which is the full version of the GUID in a string format. I'm passing the GUID string back to my application to use for future queries on courses or semesters in school bound to your user. My login is
public static async Task GetUserId(string username, string password)
{
await using (var context = new AppDbContext())
{
CurrentUser = context.Users.First(u => u.Username == username && u.Password == password).IdText;
}
}
public static async Task GetUserId(string username, string password)
{
await using (var context = new AppDbContext())
{
CurrentUser = context.Users.First(u => u.Username == username && u.Password == password).IdText;
}
}
The documentation says that it will return the first result matching the query or throw back an ArgumentNullException. How do I handle the exception? Or should I be using First? is there a better method similar to an Int32.TryParse which returns the value or silences the exception? or do I create my own TryParse method for my Users model that throws away the exception because the only exception would be if the User doesn't exist? And would I want to mix that into this function of GetUserId? or would I be better putting this inside the SubmitButton_OnClicked method? Sorry, this is probably a lot of ramblings I've never used Try in any of my prior programs so i'm reading on the best practices and looking at docs and I just don't want to develop bad habits.
53 replies
CC#
Created by DaClownie on 6/16/2024 in #help
NullReferenceException but checking for Null?
I click a button on my .NET MAUI UI to Create a new course. ContentPage loads, I enter my information. If I leave any of the entry fields blank, the program crashes... stating System.NullReferenceException Message=Object reference not set to an instance of an object.
private async void SaveCourseButton_Clicked(object sender, EventArgs e)
{
Course course = new Course(id, CourseName.Text, StartDate.Date, EndDate.Date, StatusPicker.SelectedIndex, InstructorName.Text, InstructorPhone.Text, InstructorEmail.Text, CourseNotes.Text, 0, 0);
var result = DataObjects.ValidateCourseInfo(activeTerm, course);
if (result.Length == 0)
{
SQLFunctions.AddNewCourse(course);
await Navigation.PopAsync();
}
else
{
await DisplayAlert("ERROR", result, "OK");
}
}

public static string ValidateCourseInfo(Term term, Course course)
{
if (course.CourseName.Length == 0 || course.CourseName is null)
{
return "Course must have a name.";
}
else if (course.EndDate.Date < course.StartDate.Date)
{
return "End date must be equal or greater than start date.";
}
else if (course.StartDate.Date < term.StartDate || course.StartDate.Date > term.EndDate || course.EndDate.Date < term.StartDate || course.EndDate.Date > term.EndDate)
{
return "Course start and end dates must fall within term start and end dates.";
}
else if (course.Status == -1)
{
return "Must pick a course status.";
}
else if (course.InstructorName.Length == 0 || course.InstructorName is null)
{
return "Instructor must have a name.";
}
...
else
{
return string.Empty;
}
}
private async void SaveCourseButton_Clicked(object sender, EventArgs e)
{
Course course = new Course(id, CourseName.Text, StartDate.Date, EndDate.Date, StatusPicker.SelectedIndex, InstructorName.Text, InstructorPhone.Text, InstructorEmail.Text, CourseNotes.Text, 0, 0);
var result = DataObjects.ValidateCourseInfo(activeTerm, course);
if (result.Length == 0)
{
SQLFunctions.AddNewCourse(course);
await Navigation.PopAsync();
}
else
{
await DisplayAlert("ERROR", result, "OK");
}
}

public static string ValidateCourseInfo(Term term, Course course)
{
if (course.CourseName.Length == 0 || course.CourseName is null)
{
return "Course must have a name.";
}
else if (course.EndDate.Date < course.StartDate.Date)
{
return "End date must be equal or greater than start date.";
}
else if (course.StartDate.Date < term.StartDate || course.StartDate.Date > term.EndDate || course.EndDate.Date < term.StartDate || course.EndDate.Date > term.EndDate)
{
return "Course start and end dates must fall within term start and end dates.";
}
else if (course.Status == -1)
{
return "Must pick a course status.";
}
else if (course.InstructorName.Length == 0 || course.InstructorName is null)
{
return "Instructor must have a name.";
}
...
else
{
return string.Empty;
}
}
23 replies
CC#
Created by DaClownie on 5/21/2024 in #help
New ContentPages in .net MAUI app don't have InitializeComponent() methods
I'm following a few guides to learn how to do Navigation in a .NET MAUI app which I need to apply for a project for school. All of the guides show creation of multiple ContentPages, assigning it as such in App.xaml.cs
MainPage = new NavigationPage(new TermPage());
MainPage = new NavigationPage(new TermPage());
I created a new ContentPage in the solution named TermPage but it doesn't have a InitializeComponent() and I'm unable to Build my project and send it to the Android Emulator. How come the guides have no issue with the creation of the InitializeComponent() method and mine does? And How do I fix it?
17 replies