3

I'm getting a weird XAML error that I don't understand that I'm hoping someone can help with. I am attempting to make a generic AxisBase user control that is capable of plotting any type of data. I then want to extend this to create a specific kind of axis, one that can graph dates on the X axis and doubles on the Y axis.

DateDoubleAxis.xaml

<axis:AxisBase  x:Class="project.Views.Controls.Chart.Axis.DateDoubleAxis"
                x:TypeArguments="system:DateTime, system:Double"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:axis="using:project.Views.Controls.Chart.Axis"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                xmlns:system="using:System">...</axis:AxisBase>


AxisBase.cs

namespace project.Views.Controls.Chart.Axis
{
    public class AxisBase<TX, TY> : UserControl {...}
}


DateDoubleAxis.xaml.cs

namespace project.Views.Controls.Chart.Axis 
{
    public sealed partial class DateDoubleAxis : AxisBase<DateTime, Double> {...}
}

But I'm getting a couple weird errors. In the xaml declaration of DateDoubleAxis I get the error

The name "AxisBase`2" does not exist in the namespace "using:project.Views.Controls.Chart.Axis"

The '`2' I believe comes from the fact that AxisBase has two generic types associated with it.



And then in the declaration of DateDoubleAxis I get the error:

Base class of 'project.Views.Controls.Chart.Axis.DateDoubleAxis' differs from declared in other parts

Along with the ironic resharper warning

Base type 'AxisBase' is already specified in other parts

I realize I'm doing something a little weird in trying to extend from my own user control, but I think that it makes sense in this context, and at the very least, a few users have suggested it here.

Can anyone point me to what I might be doing wrong?

Zach Olivare
  • 3,805
  • 3
  • 32
  • 45
  • Your `AxisBase` cannot take generic type since it has the xaml part. You should keep it as a normal usercontrol, and have `DateDoubleAxis` be `DateDoubleAxis` and inherits from `AxisBase`. – Justin XL Aug 16 '17 at 13:44
  • @JustinXL `AxisBase` does not have a xaml component (I added file names to make this more clear). I am intending for the `DateDoubleAxis.xaml` snippet to show that `DateDoubleAxis` is an `AxisBase` to mimic the class declaration in `DateDoubleAxis.xaml.cs`. Is that not actually what's happening? – Zach Olivare Aug 16 '17 at 15:03

1 Answers1

4

I just realized that you are using

x:TypeArguments="system:DateTime, system:Double"

in your XAML. Unfortunately this is not supported in UWP. Also keep in mind that Double in XAML should be defined as x:Double instead of system:Double. But this is irrelevant in this case anyway.

The only way I got this working in my project is create a UserControl (with XAML file) as the base of anything, and then create a generic class that inherits from it. Once the generic class is in place, I just create a bunch of derived classes based on it with different types. For example in your case, you would have

Base UserControl: AxisBase

<UserControl x:Class="xxx.AxisBase"

public partial class AxisBase : UserControl
{
    public AxisBase()
    {
        InitializeComponent();
    }
}

Generic class: AxisOfT

public class AxisOfT<TX, TY> : AxisBase { }

Derived class: DateDoubleAxis

public class DateDoubleAxis : AxisOfT<DateTime, double> { }
Justin XL
  • 38,763
  • 7
  • 88
  • 133
  • 2
    Thanks so much for your answer on this @JustinXL. Your answer compiles, which is great, but doesn't allow the derived class to have a XAML part, which is unfortunate. This essentially means to me that a generic user control cannot exist. I'm hesitant to check your answer because it seems a "correct" one does not exist, but I will do so out of appreciation for your time =) – Zach Olivare Aug 22 '17 at 13:45
  • Please vote [here](https://developercommunity.visualstudio.com/idea/610869/support-generic-type-arguments-in-uwp-xaml.html). – Shimmy Weitzhandler Jun 17 '19 at 20:41