1

If I need to post more code, then let me know, but I think what I have is sufficient to convey what I am trying to do. I can do it if I don't use XAML to define the page content, but when I try it with XAML, addng the <TViewModel,TDataModel> causes InitializeComponent() and any named elements in the XAML to get flagged as not found.

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BaseSelectListPage<TViewModel,TDataModel> : ContentPage
    where TViewModel : BaseSelectListViewModel<TDataModel>, new() 
{
    private TViewModel _viewModel { get; set; }
    public BaseSelectListPopup()
    {
        _viewModel = new TViewModel(); 
        BindingContext = _viewModel;
        InitializeComponent();       // InitializeComponent gets flagged as not found
        ExampleElement.Spacing = 5;  // ExampleElement also gets flagged as not found
    }
  // rest of class
}

I don't know how to define the generic types in XAML, but here is what I tried.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 x:Class="IRA.Views.SelectLists.BaseSelectListPage<TViewModel,TDataModel>">
                <!-- this is obviously what I don't know how to do  ^^^^  -->
    <ContentPage.Content>
        <StackLayout x:Name="ExampleElement">
            <Label Text="Thanks for helping"/>
        </StackLayout>
    <!-- various elements -->
    </ContentPage.Content>
</ContentPage>
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
jsureke
  • 79
  • 9
  • https://learn.microsoft.com/en-us/xamarin/xamarin-forms/xaml/generics – Jason Aug 27 '20 at 01:10
  • More specifically https://learn.microsoft.com/en-us/dotnet/desktop-wpf/xaml-services/xtypearguments-directive That's the wpf page, but it's the same for xamarin xaml. – Ben Reierson Aug 27 '20 at 03:49
  • Set x:TypeArguments =TViewModel may work. Have a look at these threads: [generic-base-class-for-a-custom-contentpage](https://forums.xamarin.com/discussion/21470/generic-base-class-for-a-custom-contentpage), [Page with type parameter](https://stackoverflow.com/questions/33708567/page-with-type-parameter) and [inheritance-from-generic-pages](https://forums.xamarin.com/discussion/106919/inheritance-from-generic-pages). – nevermore Aug 27 '20 at 08:33

0 Answers0