简体   繁体   中英

How to create a generic xaml user control in UWP?

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?

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> { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM