Is it possible to declare a type alias name in XAML?
-
22-10-2019 - |
Question
Is it possible in XAML to declare an alias name for a type?
Let me explain with an example. Given these type declarations...
namespace Somewhere
{
public class Blob { … }
public class BlobCollection : List<Blob> {} // "type alias" in C#
}
... the following (abbreviated) XAML should be valid:
<BlobCollection xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</BlobCollection>
I already know that I can define something like type aliases through inheritance (see code comment above). Suppose that wanted to do the same in XAML, how would I have to change the XAML in order to be able to refer to BlobCollection
as Blobs
?
<Blobs xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:Somewhere;…">
<Blob … />
<Blob … />
</Blobs>
Solution
I'm not sure if you can do the aliasing directly in XAML, but it's easiest to use the aliased name in XAML by simply subclassing the collection in code (or heck, renaming the collection class itself if it won't adversely affect the rest of your code):
public class Blobs : BlobCollection {}
It does seem unnecessary to have to do this, but it's all I can think of right now.