Windows 8 Store Application Support for x:TypeArgu

2020-04-21 03:55发布

问题:

I'd like to know the same thing as this: Silverlight 4 Support for x:TypeArguments ... but for Windows 8 Store Apps.

Why is x:TypeArguments available if it doesnt work? Or am I missing something? The MainPage.i.g.cs file is auto-generated with a non-generic base class even though x:TypeArguments is defined in the XAML - so of course it doesn't compile.

I can get it working with the proposed work-around of having a "typedef" base class which specifies the generic type, but this feels pretty hacky to me..

// A generic PageBase, containing standard ViewModel-related utilities
internal abstract class PageBase<T> : Windows.UI.Xaml.Controls.Page where T : ViewModelBase
{
    protected abstract T ViewModel { get; }
    ...
}

// The hack...
internal abstract class MainPageTypeDef : PageBase<MainViewModel>
{
    // No code goes here...
}

// The page itself
internal sealed partial class MainPage : MainPageTypeDef
{
}

<views:PageBase
    ...
    x:Class="Namespace.MainView"
    x:TypeArguments="store:MainViewModel">

Anyone know if there is a way of not having the "typedef" class?

Many thanks, Jon

回答1:

Sadly it's not possible. I got a definitive answer from a Microsoft contact: "I can confirm that but x:TypeArguments use in Xaml is not a supported scenario Windows Store Apps."



回答2:

I'm pretty sure the answer is the same as for Silverlight. WinRT Xaml is in many ways closer to Silverlight Xaml than it is to WPF, and they've made all sorts of strange decisions in what to include and what not to.