3
votes

I have a small window that I am trying to load when my application starts. Here is the (loose) XAML:

<ctrl:MainWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctrl="clr-namespace:Controls;assembly=Controls">
<Grid>
    <ctrl:ConnectionStatusIndicator/>
    <TextBlock Grid.Row="2" Text="{Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
</Grid>
</ctrl:MainWindow>

Notice the custom control called ConnectionStatusIndicator. The code for it is:

using System.Windows;
using System.Windows.Controls;

namespace Controls
{
    public class ConnectionStatusIndicator : Control
    {
        public ConnectionStatusIndicator()
        {
        }

        static ConnectionStatusIndicator()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ConnectionStatusIndicator),
                                                     new FrameworkPropertyMetadata(typeof(ConnectionStatusIndicator)));
            IsConnectedProperty = DependencyProperty.Register("IsConnected", typeof(bool), typeof(ConnectionStatusIndicator), new FrameworkPropertyMetadata(false));
        }

        public bool IsConnected
        {
            set { SetValue(IsConnectedProperty, value); }
            get { return (bool)GetValue(IsConnectedProperty); }
        }

        private static DependencyProperty IsConnectedProperty;
    }
}

Now, here is where it gets weird (to me, at least). With the XAML as it appears above, my application will build and run just fine. However, if I remove the following line:

<ctrl:ConnectionStatusIndicator/>

or event move it one line down, I get the following error:

Additional information: 'Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Resx'.' Line number '13' and line position '33'.

What is really strange to me is that, if I replace ConnectionStatusIndicator with another custom control from the same assembly, I get the error. The other custom control is very similar, but has a few more properties.

Can anyone explain what is going on here?

1
Shouldnt the namespace be :CNC.UI.Controls in Xaml ?CSharpie
What is this{Resx ResxName=MyApp.MainDialog, Key=MyLabel}?Hamlet Hakobyan
Something I've been bitten by before, was that the designer likes to act up if the dependency property isn't public. Try marking IsConnectedProperty public, see if it does anything to help.Psytronic
@CSharpie - Sorry, I edited the code to make it as brief as possible and missed a spot. I corrected the code in my questionJAB
@Hamlet - Resx is a markup extension we use for localization. I got it from here linkJAB

1 Answers

1
votes

The Resx markup extension belongs to the Infralution.Localization.Wpf namespace but also does something a bit hackish and attempts to register itself to the http://schemas.microsoft.com/winfx/2006/xaml/presentation xml namespace to allow developers to use it as {Resx ...} instead of having to declare the namespace in XAML and use the extension with a prefix {resxNs:Resx ...}.

I believe that if you clean up your solution and possible delete your *.sou file the project will build as expected, but a sure way to solve this will be to add an xmlns declaration for Infralution.Localization.Wpf and use the extension with the xmlns prefix:

<ctrl:MainWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctrl="clr-namespace:Controls;assembly=Controls"
    xmlns:loc="clr-namespace:Infralution.Localization.Wpf;assembly=Infralution.Localization.Wpf">
    <Grid>
        <ctrl:ConnectionStatusIndicator/>
        <TextBlock Grid.Row="2" Text="{loc:Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/>
    </Grid>
</ctrl:MainWindow>

Also, for anyone interested, the "hack" is in these lines in the localization library:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2007/xaml/presentation", "Infralution.Localization.Wpf")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2008/xaml/presentation", "Infralution.Localization.Wpf")]