Adding a ListView to my page is throwing a runtime exception when I try and navigate to the page using Shell.Current.Navigation.PushModalAsync(new CollectionItemDetails(item)); I'm wondering if I've malformed my XAML somehow or if my Binding is off, as removing the listview prevents the exception from happening.
The Listview XAML See ListView at the bottom
<?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="CollectionItemDetails"
xmlns:ViewModels="clr-namespaceViewModels"
Title="{Binding Item.Name}">
<ContentPage.BindingContext>
<ViewModels:CollectionItemDetailsViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<!--Header Display-->
<FlexLayout HorizontalOptions="{OnPlatform WinUI=Start, Default=Center}"
VerticalOptions="Fill"
Wrap="Wrap">
<!--Image-->
<Frame CornerRadius="45"
HeightRequest="300"
WidthRequest="300"
HasShadow="False">
<Image Source="{Binding Item.Image}"
HeightRequest="300"
MinimumWidthRequest="300"
Margin="-20"
Aspect="AspectFill"/>
</Frame>
<StackLayout HorizontalOptions="{OnPlatform WinUI=StartAndExpand, Default=Center}">
<Label Text="{Binding Item.Name}"
FontSize="{OnPlatform WinUI=Header, Default=Title}"
FontAttributes="Bold"
HorizontalOptions="Center"/>
<Grid ColumnSpacing="50"
HorizontalOptions="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!--PlayerCount-->
<HorizontalStackLayout Grid.Column="0"
HorizontalOptions="Start">
<Label VerticalOptions="Center" FontSize="Small">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Item.MinPlayers}"/>
<Span Text="-"/>
<Span Text="{Binding Item.MaxPlayers}"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Image x:Name="PlayerCountIMG"
Source="{AppThemeBinding Light=players.png, Dark=playerswhite.png}"
HeightRequest="30" WidthRequest="30"/>
</HorizontalStackLayout>
<!--Playtime-->
<HorizontalStackLayout Grid.Column="1"
HorizontalOptions="Center">
<Label VerticalOptions="Center" FontSize="Small" Text="{Binding Item.PlayTime}"/>
<Image Source="clock.png" HeightRequest="30" WidthRequest="30"/>
</HorizontalStackLayout>
<!--Age-->
<HorizontalStackLayout Grid.Column="2"
HorizontalOptions="End">
<Label VerticalOptions="Center" FontSize="Small">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Item.MinAge}"/>
<Span Text="+"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Image Source="{AppThemeBinding Light=age.png, Dark=agewhite.png}" HeightRequest="30" WidthRequest="30"/>
</HorizontalStackLayout>
<Label Grid.Column="3"
VerticalOptions="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="Weight: "
FontAttributes="Bold"/>
<Span Text="{Binding Item.Statistics.Ratings.AverageWeights.Value, StringFormat='{0:F2}'}"
TextColor="Orange"
FontAttributes="Bold"/>
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
<!--Publishers This is causing the breakage-->
<ListView BindingContext="{Binding Publishers}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!--<ListView BindingContext="{Binding Source={RelativeSource Self}, Path=Item.Publishers}">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>-->
</StackLayout>
</FlexLayout>
<Label Text="{Binding Item.Description}"
TextType="Html"/>
</StackLayout>
</ContentPage>
Code Behind
public partial class CollectionItemDetails : ContentPage
{
public ICollectionItemDetailsViewModel itemViewModel;
public CollectionItemDetails(CollectionPageItem item)
{
InitializeComponent();
Init(item);
}
private void Init(CollectionPageItem item)
{
itemViewModel = BootStrapper.Resolve<ICollectionItemDetailsViewModel>(new[] { new NamedParameter("item", item) });
BindingContext = itemViewModel;
}
}
The ViewModel
public class CollectionItemDetailsViewModel : ViewModelBase, ICollectionItemDetailsViewModel
{
public CollectionItemDetailsViewModel()
{
}
public CollectionItemDetailsViewModel(CollectionPageItem item)
{
Item = item;
Publishers = Item.Publishers.ToObservableCollection();
}
public CollectionPageItem Item { get; private set; }
private ObservableCollection<string> _publishers = new();
public ObservableCollection<string> Publishers
{
get => _publishers;
private set { _publishers = value; OnPropertyChanged(nameof(Publishers)); }
}
}
I originally tried to Bind the ListView to the Item.Publishers property, which is an IList<string>, but because of the exception I tried to expose the property as an ObservableCollection<string> even though this page does will not actually need to be watching for changes. Unfortunately still getting the exception.
Exception Details
System.Runtime.InteropServices.COMException
HResult=0x88000FA8
Message=0x88000FA8
Source=WinRT.Runtime
StackTrace:
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.Microsoft.UI.Xaml.Controls.INavigationView2.global::Microsoft.UI.Xaml.Controls.INavigationView2.set_PaneDisplayMode(NavigationViewPaneDisplayMode value)
at Microsoft.UI.Xaml.Controls.NavigationView.set_PaneDisplayMode(NavigationViewPaneDisplayMode value)
at Microsoft.Maui.Controls.Handlers.ShellItemHandler.MapTabBarIsVisible(ShellItemHandler handler, ShellItem item)
at Microsoft.Maui.Controls.Shell.SendNavigated(ShellNavigatedEventArgs args)
at Microsoft.Maui.Controls.Shell.<.ctor>b__169_0(Object _, ShellNavigatedEventArgs args)
at Microsoft.Maui.Controls.ShellNavigationManager.<>c__DisplayClass15_0.<HandleNavigated>g__FireNavigatedEvents|1(ShellNavigatedEventArgs a, Shell shell)
at Microsoft.Maui.Controls.ShellNavigationManager.<>c__DisplayClass15_0.<HandleNavigated>b__0()
at Microsoft.Maui.Controls.Page.OnAppearing(Action action)
at Microsoft.Maui.Controls.BaseShellItem.OnAppearing(Action action)
at Microsoft.Maui.Controls.ShellNavigationManager.HandleNavigated(ShellNavigatedEventArgs args)
at Microsoft.Maui.Controls.ShellNavigationManager.<GoToAsync>d__14.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.Maui.Controls.ShellSection.NavigationImpl.<OnPushModal>d__8.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.Maui.Controls.Shell.NavigationImpl.<OnPushModal>d__11.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ..\.CollectionViewModel.<ItemTapped>d__17.MoveNext() in C:\ViewModels\CollectionViewModel.cs:line 114