James Ko
James Ko

Reputation: 34489

Why does this code raise a 'does not exist in namespace' error when it compiles fine?

I'm working on a XAML-based app for Windows 10. I'm running into issues when trying to implement a binding in Setters, as per this answer. Here is my code:

namespace Sirloin.Helpers
{
    internal class Binding
    {
        public static readonly DependencyProperty ContentProperty = // ...

        public static string GetContent(DependencyObject o) => // ...
        public static void SetContent(DependencyObject o, string value) => // ...
    }
}

And here is my XAML:

<Page
    x:Class="Sirloin.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sirloin"
    xmlns:h="using:Sirloin.Helpers"> <!--Some designer stuff omitted for clarity-->

    <Page.Resources>
        <ResourceDictionary>
            <Style x:Key="MenuButtonStyle" TargetType="Button">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="h:Binding.Content" Value="Symbol"/> <!--The XAML parser chokes on this line-->
                <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
            </Style>
        </ResourceDictionary>
    </Page.Resources>
</Page>

For some reason, the VS designer seems to be throwing an error when it reaches the lines with the special binding syntax; namely, the one that sets h:Binding.Content to Symbol. Here is a screenshot of the message:

Strangely enough, though, the code seems to be compiling fine. When I hit Ctrl + B in Visual Studio, it builds with no errors and successfully outputs the binary. Of course, the drawback to this is I can't use the designer, which claims the XAML to be 'Invalid Markup' every time I build the project.

Can someone recommend me a solution to this problem? I tried the suggestion here of restarting Visual Studio and deleting the cached binaries, but it doesn't seem to be working.

Thanks!

Upvotes: 2

Views: 1037

Answers (2)

pollaris
pollaris

Reputation: 1321

Clear the Visual Studio Editing panel before you begin a compile, and you will not see these nagging error messages. That is, "X" off all your current editing sessions and of course answer yes to all "Save changes to the following items" messages.

Upvotes: 1

James Ko
James Ko

Reputation: 34489

Turns out Uchendu was right: one of my problems was that the class was marked as internal rather than public. I also had to mark it as sealed, and change the dependency properties from fields to properties. For example, this:

public static readonly DependencyProperty BlahProperty = /* ... */;

had to be refactored to this:

public static DependencyProperty BlahProperty { get; } = /* ... */;

since WinRT components don't allow exposing public fields.

After those modifications I was able to get the library to build successfully.

Upvotes: 3

Related Questions