Search code examples
c#wpfdata-binding

WPF: Unable to locate resources in my namespaces in XAML code


This is a tricky problem that I’ve encountered. I’ve picked up an existing repository with some especially sloppy WPF that doesn’t adhere to MVVM best practices at all. I plan to refactor, but I have near term release targets I want to hit. So, until then, I’m going to follow best practices.

Here’s the rub, if I make any user control, converter, etc, I am unable to reference them in any xaml whatsoever. Suppose I have a window:

<Window … xmlns:views=“clr-namespace:PCXperience.Views” … > 
    <Window.Resources>
        <views:MyUserControl x:Key=“MyUserControl”/>
    </Window.Resources>
    …
    <views:MyUserControl/>
</Window>
    

This is a simple version of the issue. I’ll get xaml errors that the namespace doesn’t exist … it does. And I’ll get an error on the line with the UserControl saying that MyUserControl does not exist in the namespace.

What I’ve done so far.

I’ve checked, double checked and triple checked that the namespace is correct. I’ve set the Build Action of the user control to Page and CopyIfNewer. I’ve checked thoroughly that I have the correct Attributes on both the Window and the UserControl. I have cleaned my solution, saved the code, deleted the .vs folder, deleted the obj folder, deleted the bin folder, closed the solution, reopened the solution and done Rebuild Solution. I have even copied the code — everything within the Window and UserControl tabs, their code behinds, and such and pasted into a new repo. The code works in another repo.

Areas that I suspect The declaration attributes of the Window and UserControl could still be wrong somehow though I can’t see it. The Solution/Project/Etc files could have some error that prevents them from recognizing files in their respective name spaces but it seems to only affect xaml. I can correctly make these in the code behind if I have to, but I don’t want to. It’s possible some silly issue that I’ve repeatedly overlooked.

I am able to use my resource dictionaries in my project — I just can’t declare new resources in my XAMLs whatsoever.

In an act of what can only be described as a masochistic need for punishment, I wrote this post on my phone. I’ll edit the post to include the top level declarations for each View Element in a moment so we can look at them and see if I’ve missed something obvious. But I am not prepared to type them on my phone here, for the initial post because… I guess that’s where I draw the line, lol.

Edit As promised, here's that xaml

<Window
x:Class="PCXperience.Views.SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PCXperience.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:PCXperience.Views"
Title="SettingsWindow"
Width="800"
Height="724"
AllowsTransparency="True"
Background="Transparent"
PreviewKeyDown="Window_PreviewKeyDown"
Style="{StaticResource WindowsTheme}"
WindowStartupLocation="CenterOwner"
WindowStyle="None"
mc:Ignorable="d">
 <Window.Resources>
<views:PrimaryMonitorSelectorUserControl 
   x:Key="PrimaryMonitorSelectorUserControl" />
</Window.Resources>

...

<views:PrimaryMonitorSelectorUserControl Grid.Row="0"/> 

The props I add or don't add do not matter. I've written Grid.Row here to show an example, but the problem is ultimately that PrimaryMonitorSelectorUserControl says it doesn't exist in the namespace defined by "views"... but it most certainly does.

And here is the UserControl

<UserControl x:Class="PCXperience.Views.PrimaryMonitorSelectorUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:PCXperience.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded">
    <Grid>
        <Label
            Margin="10,0,10,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Content="Display Monitor" />
        <ComboBox
            x:Name="MonitorComboBox"
            Grid.Row="8"
            Width="auto"
            Height="auto"
            Margin="10,40,10,10"
            SelectionChanged="MonitorComboBox_SelectionChanged"
            Style="{StaticResource DropDownSettingsTheme}" />
    </Grid>
</UserControl>

My views live in the PCXperience -> Views folder. It's not just Views that are affected. View Models are too. They are in PCXperience -> ViewModels. And the converters live in PCXperience -> Views -> Converters.


Solution

  • The code works in another repo. So you have a non-working repo A and a working repo B. This is what I'd do if I were in your situation: Delete everything in A that is not part of the code copied to B. Now you have two repos with the same code, one working, one not working. Then run WinMerge or any other good diff tool to find the remaining difference that's the cause for all the headache. (And then self-answer your question, because we're curious.)

    There was a non-visible character in the namespace for some of my controls. Someone must have done something really dumb and then copy pasted the namespaces in the views folder. I can’t, for the life of me figure it out. But when I fixed it, the problem resolved. Bless hex editors. Good lord.