Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic base class for Pages/Views in UWP Windows 10 App

In a UWP-Windows 10 C#/XAML app using Template10, I'm currently trying to have my pages/views inherit from a base class that inherits from Windows.UI.Xaml.Controls.Page.
This has been working correctly, however when I try to make the base class generic, and include a type argument in the child class and the XAML in the following way, it does not work:

namespace App.Views 
{
    public abstract class InfoListViewBase<M> : Page where M : InfoListViewModelBase
    {

        public InfoListViewBase() { }

    }

    public sealed partial class ModelPage : InfoListViewBase<Model>
    {
        public Model()
        {
            InitializeComponent();
            NavigationCacheMode = NavigationCacheMode.Disabled;
        }
    }
}

<local:InfoListViewBase 
    x:Class="App.Views.ModelPage"
    x:TypeArguments="l:Model"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Behaviors="using:Template10.Behaviors"
    xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:controls="using:Template10.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:App.Views"
    xmlns:l="using:Library"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

</local:InfoListViewBase>

The error I receive is:

The name "InfoListViewBase`1" does not exist in the namespace "using:App.Views".

The error shows up every time I add the line x:TypeArguments="l:Model" to the XAML.
Multiple rebuilds, cleans, etc., in visual studio have not solved the problem.
Is there something I am doing wrong in the implementation of the generic in XAML?

like image 746
cloudcrypt Avatar asked Jul 26 '17 21:07

cloudcrypt


1 Answers

Unfortunately, generic parameters in XAML still aren't supported in UWP app. You cannot use x:TypeArguments in XAML currently. But you may reference this thread to try to have a workaround.

If you still want this feature ,you can also submit this feature request to UserVoice.

like image 169
Sunteen Wu Avatar answered Oct 10 '22 12:10

Sunteen Wu