Visual Studio 2010 - Dictionary Not found but it exists - wpf

Well, I´m developing a wpf application and I got a strange error on design time. This is the code of a wpf form:
<Window x:Class="ViewLayer.Frm_EnrollWaitingList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Frm_EnrollWaitingList" WindowStartupLocation="CenterScreen" BorderBrush="{x:Null}" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="390" Width="410"
WindowStyle="None"
AllowsTransparency="True"
ResizeMode="NoResize">
<Window.Resources>
<ResourceDictionary Source="Dictionary/WaitingListDictorionary.xaml"/>
</Window.Resources>
<Grid>
<Rectangle Margin="0,0,0,0" Name="rectangle1" Stroke="{x:Null}" Fill="#FF8C90AD" Opacity="0.95" />
<Button Style="{DynamicResource CommonButton}" Content="Salir" Height="80" HorizontalAlignment="Left" Margin="166,234,0,0" Name="btn_close" VerticalAlignment="Top" Width="180" />
</Grid>
</Window>
The problem starts here:
<Window.Resources>
<ResourceDictionary Source="Dictionary/WaitingListDictorionary.xaml"/>
</Window.Resources>
The ResourceDictionary exits and in execution time it works perfectly. But at design time sometime when a want to modify the form in visual studio designer i get the following error:
FileNotFoundException An error occurred while finding the resource dictionary "Dictionary/WaitingListDictorionary.xaml".
Unable to find the specified file.
at Microsoft.Windows.Design.Platform.ViewProducerBase.Microsoft.Expression.DesignModel.DocumentModel.IDocumentRootResolver.GetDocumentRoot(String path)
at Microsoft.Expression.Platform.WPF.InstanceBuilders.ResourceDictionaryInstanceBuilder.ProvideResourceDictionary(IInstanceBuilderContext context, DocumentCompositeNode resourceDictionaryReferenceNode, IDocumentRoot& relatedRoot)
And I can not edit the form with the designer.
Any idea? I repeat on execution time I have no problems.

The Source property is relative, and you window appears to be in the ViewLayer directory, so the Dictionary folder must also be in the ViewLayer directory if you use
Source="Dictionary/WaitingListDictorionary.xaml".
If the Dicionary folder is at the root level, try
Source="/Dictionary/WaitingListDictorionary.xaml".
You can also use a pack:// URL.

I could figure it out!
There was an error on the ResourceDictionary I was trying to use. I fixed it, and voila! Everything works like a charm.

Related

WPF Custom Control does not exist in XML namespace

I'm having an annoying error that I can't seem to fix or find a solution to in my WPF project. I have a custom WPF control that I added to my project but I can't get it to work. This is the error I am getting:
The tag 'ZoomBorder' does not exist in XML namespace 'clr-namespace:Namespace1;assembly=Namespace1'. Line 8 Position 10.
Here is my xaml for the window:
<Window x:Class="Namespace1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Namespace1;assembly=Namespace1"
Title="MainWindow" Height="350" Width="525">
<Grid Name="MainGrid">
<Label Content="Label" HorizontalAlignment="Left" Margin="357,165,0,0" VerticalAlignment="Top"/>
<local:ZoomBorder x:Name="border" ClipToBounds="True" Background="Gray"> //ERROR HAPPENS HERE
<Image Name="DrawingImage" HorizontalAlignment="Left" Height="200" Margin="10,10,0,0" VerticalAlignment="Top" Width="300"/>
</local:ZoomBorder>
</Grid>
</Window>
I have tried building, cleaning, and rebuilding with and without the offending lines and nothing has worked.
Anyone know how to fix this?
You have new visual studio and old lib. Thats mess.
Install 4.7 dev runtime:
https://dotnet.microsoft.com/en-us/download/visual-studio-sdks?cid=getdotnetsdk
Right click your project and open folder. Edit your project file to fav editor like notepad and add following lines:
<LangVersion>latest</LangVersion>
<TargetFramework>net47</TargetFramework>
goto where you use control and add namespace my visual studio 2022 suggest this one: <mainwindow.xaml>
xmlns:paz="clr-namespace:Scar.Common.WPF.Controls;assembly=Scar.Common.WPF.Controls.ZoomBorder"
close visual studio and restart and compile. Build: succeeded if you din't forget anything.

Why do resource files "disappear" from the WPF Designer when referenced from a Window?

Stackoverflow has other similar questions, but they seem to be related to usage in multiple assemblies or with the formatting of ResourceDictionary Source values. This question relates to single assemblies and the tests listed at the end demonstrate the Source value works as-is.
The following describes a small test application that illustrates the issue.
The test application has a style XAML file (MyStyle.xaml):
<ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<SolidColorBrush x:Key="FontColorKey" Color="DarkBlue" />
</ResourceDictionary>
Which is used in a UserControl (PanelChoice.xaml):
<ToggleButton
x:Class="PanelNS.PanelChoice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="30" Width="100" Background="Orange"
>
<ToggleButton.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/MyStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="FontColorKey" Color="Yellow" />
</ResourceDictionary>
</ToggleButton.Resources>
<TextBlock
Foreground="{StaticResource FontColorKey}"
Background="Pink" Text="Testing"
/>
</ToggleButton>
Which, in turn, is used on a test Window (MainWindow.xaml):
<Window x:Class="IncludeStyleTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PanelNS"
Title="MainWindow" Height="200" Width="200"
>
<Grid Background="Green">
<local:PanelChoice />
</Grid>
</Window>
Tests to isolate this issue:
This code compiles and when run displays green, orange and pink rectangles with yellow text in the middle (as expected).
Viewing PanelChoice.xaml in the XAML Designer shows orange and pink rectangles with yellow text in the middle (as expected).
However, viewing MainWindow.xaml in the designer shows Cannot create an instance of "PanelChoice" and lists Cannot locate resource 'styles/mystyle.xaml'. in the Visual Studio Error List pane.
If the <ResourceDictionary Source="/Styles/MyStyle.xaml"/> line in PanelChoice.xaml is removed (or commented out) and the project rebuilt, then viewing MainWindow.xaml in the designer shows green, orange and pink rectangles with yellow text in the middle and no error messages. It behaves as #1 when run.
To verify that MyStyle.xaml is properly referenced one can remove the <SolidColorBrush x:Key="FontColorKey" Color="Yellow" /> line in PanelChoice.xaml instead of removing MyStyle.xaml. This case behaves just as #1, but the text is dark blue instead of yellow. This demonstrates that MyStyle.xaml is properly referenced and is accessible.
NOTE: Visual Studio 2015 and 2017 both exhibit this behavior.
Take out the leading / in the .xaml reference so that it looks like this instead:
<ResourceDictionary Source="Styles/MyStyle.xaml"/>
The control is then able to be displayed properly in the Window when in the designer.
Another way to get it to show properly is to use the full physical path (you wouldn't do it this way, but just demonstrating the behaviour) e.g.:
<ResourceDictionary Source="\Users\colin.smith\Documents\visual studio 2017\Projects\WpfApp15\WpfApp15\Styles\MyStyle.xaml"/>
Yey another way that works:
<ResourceDictionary Source="/WpfApp15;component/Styles/MyStyle.xaml"/>
If you use the property picker to select the Source for your ResourceDictionary it does not put a leading / at the start of the Source when referring to the MyStyle.xaml that is in your Styles folder.
The issue seems to be with the designer. When it's providing a design surface which has to "include" another control which you have created in "the same" project, it must be doing something wrong when referring to those ResourceDictionaries with leading / paths.
Another way to get it to work...
If you move/create your "controls" in a separate "User Control" or "Custom Control" library, then you will be using a "pack" reference to refer to the resources...and the Window will be able to display that control properly in the designer.
I'm sure you can find an example of creating user controls in their own library...but will quickly show what it would kinda look like.
<ToggleButton
x:Class="WpfControlLibrary1
.PanelChoice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="30" Width="100" Background="Orange"
>
<ToggleButton.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Styles/MyStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="FontColorKey" Color="Yellow" />
</ResourceDictionary>
</ToggleButton.Resources>
<TextBlock
Foreground="{StaticResource FontColorKey}"
Background="Pink" Text="Testing"
/>
</ToggleButton>
Then add reference to your WpfControlLibrary1 library, and use:
<Window x:Class="WpfApp15.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp15"
xmlns:controls="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Background="Green">
<controls:PanelChoice />
</Grid>
</Window>

Why does this Microsoft.VisualStudio.Imaging.CrispImage does not show anything?

I am trying to create a very simple WPF application to experiment this concept of the Visual Studio Image Service and Catalog.
I don't understand why I cannot seem to be able to show any of the known images (using KnownMonikers) both on the WPF designer or when the app runs.
The image is there. It just does not show anything.
Here's the WPF source code (following exactly the steps described in the MSDN link above):
<Window x:Class="ImageCatalogBrowser.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ImageCatalogBrowser"
xmlns:imaging="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.Imaging"
xmlns:theming="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Imaging"
xmlns:utilities="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Utilities"
xmlns:catalog="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.ImageCatalog"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<utilities:BrushToColorConverter x:Key="BrushToColorConverter"/>
</Window.Resources>
<Grid>
<StackPanel
Background="White"
VerticalAlignment="Center"
theming:ImageThemingUtilities.ImageBackgroundColor="{Binding Background, RelativeSource={RelativeSource Self}, Converter={StaticResource BrushToColorConverter}}">
<Border BorderThickness="1" BorderBrush="Black" Width="33" Height="33">
<imaging:CrispImage
x:Name="crisp"
Visibility="Visible"
Width="32"
Height="32"
Moniker="{x:Static catalog:KnownMonikers.Save}" />
</Border>
</StackPanel>
</Grid>
</Window>
Can anyone help please?
What's your code-behind doing to initialize the ImageLibrary? If the library cannot find the moniker you're requesting, then it won't display the image.
Make sure when initializing the ImageLibrary you give it the path to the ImageCatalog's .imagemanifest file (in the VS install dir).

Don't understand why the user control we've written doesn't show up in a ModernWindow but does in a window

We're working on a WPF app, using the ModernUI for WPF. And we're also using MVVM Light. We've written some user controls which we intend to bring up in separate windows. Each of the windows are ModernUI's ModernWindow. Whenever we try to bring them up, nothing is shown in the ModernWindow. Out of frustration I thought I'd try doing the same thing in a plain old WPF window. Works perfectly each time. I don't know why it works in a regular WPF window but doesn't work in a ModernWindow. What is wrong???
Here's the XAML for the plain old window:
<Window x:Class="CoreFramework.BozoWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:local="clr-namespace:CoreFramework"
mc:Ignorable="d"
Title="Bozo Window" Height="300" Width="300">
<ContentControl Content="{Binding ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
And here's the XAML for the ModernWindow:
<mui:ModernWindow x:Class="CoreFramework.DataViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:vm="clr-namespace:CoreFramework.ViewModel"
xmlns:view="clr-namespace:CoreFramework.View"
xmlns:content="clr-namespace:CoreFramework"
Title="BOTS"
ShowInTaskbar="True"
Closing="ModernWindow_Closing">
<ContentControl Content="{Binding Path=ProductList, Source={StaticResource Locator}}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
One approach you could use is adding Menu links which is built into the Modern UI framework:
<mui:ModernWindow x:Class="ExampleApp.ExampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="Scrum Manager" IsTitleVisible="True" ContentSource="/Views/DefaultUserControl.xaml"
x:Name="mainWindow" MenuLinkGroups="{Binding Groups}"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<i:Interaction.Triggers>
</i:Interaction.Triggers>
<mui:ModernWindow.TitleLinks>
<mui:Link DisplayName="settings" Source="/Views/SettingsWindow.xaml" />
<mui:Link DisplayName="Sign out" Source="/Infrastructure/Logout.xaml" />
</mui:ModernWindow.TitleLinks>
When a link is clicked, it opens up the 'Source' which would be UserControl xaml files.
See Github for more details:
https://github.com/firstfloorsoftware/mui

xaml image not displayed during runtime but displaying during design (VB, wpf) - why?

I'm trying to understand WPF and have created a WPF test project. I've put a monkey01.jpg image in every folder starting from the project's root, moving up in hierarchy, to the debug folder. On the MainWindow I've created an Image and defined the source property to point to the monkey01.jpg and it displays correctly in design view. But when I run it, the image doesn't appear.
If I add the image file to the project as a resource, it will be displayed during runtime which is expected. (But this is not what I'm testing here)
If in the xaml file I write the absolute path as in Source="C:\Users\User\Desktop\visual_studio_projects\WpfApplication1\monkey01.jpg it will be displayed too.
But if I use a relative path (like in the example below), the image won't be displayed. It's as if upon execution, the program doesn't run inside the project folder and its subfolders; Instead it runs in some other location that has no access the image. Why this behaviour? What am I missing?
This is the MainWindow.xaml code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image x:Name="Thumbnailtest" Source="monkey01.jpg" />
</Grid>
</Window>
Thank you!
You need to set Copy to Output Directory in the Properties Window for the image to "Copy if newer".
Works fine here when I've set the image to Resource and don't copy, note I changed your monkey image to .png and I created a folder for it(Data/Images).
<Window x:Class="MasoneryLibrary.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MasoneryLibrary;assembly=MasoneryLibrary"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>
<Image Source="Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456" Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>
</Grid>
</Grid>
Hmmmm.. is this a library? You should use URI packs to be safe.
With URI pack:
<Image Source="pack://application:,,,/MasoneryLibrary;component/Data/Images/monkey01.png" HorizontalAlignment="left" VerticalAlignment="Top" Width="290" Height="456" Margin="285,0,-58,-136" Stretch="None" StretchDirection="DownOnly"/>
Hope it helps!
Cheers,
Stian

Resources