Question

I cannot for the life of me figure out why I cannot create my class in this dictionary. Intellisense isn't picking up my WindowCommand<T> class. I checked the Assembly name and it appears to be correct, no typos in the namespace either. What's making it choke?

WindowCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using Ninject;
using Premier;
using Premier.View;

namespace Premier.Command
{
    public class WindowCommand<T> : Command where T : Window
    {
        private Func<bool> focus;
        private int instantiationCount;

        public bool IsDialog { get; set; }
        public bool Multiple { get; set; }

        public WindowCommand()
        {
        }

        public override bool CanExecute(object parameter)
        {
            return true;
        }

        public override void Execute(object parameter)
        {
            var instantiatedOnce = instantiationCount > 0;

            if (!Multiple && instantiatedOnce)
            {
                focus();
                return;
            }

            instantiationCount++;

            var w = App.Kernel.Get<T>();
            w.Closed += (s, e) => instantiationCount--;
            focus = w.Focus;

            if (IsDialog)
                w.ShowDialog();
            else
                w.Show();
        }
    }
}

Windows.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:c="clr-namespace:Premier.Command;assembly=PremierAutoDataExtractor"
                    xmlns:v="clr-namespace:Premier.View;assembly=PremierAutoDataExtractor"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <c:WindowCommand x:Key="ReportsPurchased" x:TypeArguments="v:PurchasedReportsView" />
</ResourceDictionary>
Was it helpful?

Solution

x:TypeArguments XAML directive is not supported in XAML 2006 (xml namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation) on non-root XAML elements. If you want to use x:TypeArguments on a non-root XAML element, you should use XAML2009 (xml namespace http://schemas.microsoft.com/netfx/2009/xaml/presentation). However, again it is only supported for non-complied loose XAML.

Text from MSDN Page:

In WPF and when targeting .NET Framework 4, you can use XAML 2009 features together with x:TypeArguments but only for loose XAML (XAML that is not markup-compiled). Markup-compiled XAML for WPF and the BAML form of XAML do not currently support the XAML 2009 keywords and features. If you need to markup compile the XAML, you must operate under the restrictions noted in the "XAML 2006 and WPF Generic XAML Usages" section.

So, I am afraid, you cannot use your WindowCommand in a resource dictionary.

Link to MSDN page for more information on x:TypeArguments directive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top