Why does Blend / Visual Studio always generate the ResourceDictionary "Source" incorrectly?

Brief

I'm starting to go crazy. I tried this problem for about a day now and I can’t understand how easy it is to solve it. So I decided that I was going crazy with you too (I hope you are more lucky than me).

Note The link in the zipped version of the project is given below in the "Edit" section.


Project structure

I have a C # project structure that resembles the following:

β”œ Shared Solution folder (containing shared projects) β”œβ”€β”€β”€β”€ Shared.UI Shared.UI - Shared Project β”œβ”€β”€β”€β”€β”€β”€β”€β”€ Style.xaml Resource Dictionary xaml file β”œ MyProject Solution folder (containing related projects) β”œβ”€β”€β”€β”€ MyProject.UI MyProject.UI - WPF App Project β”œβ”€β”€β”€β”€β”€β”€β”€β”€ MainWindow.xaml The xaml file using Style.xaml 

The solution contains several projects organized by purpose, so MyProject contains several projects. The project structure should be supported , as well as . Also note that solution folders also exist in Explorer (not for this to matter).

The names of my projects are clearly Shared.UI and MyProject.UI . I have a link to Shared.UI in the MyProject.UI project.

Note The shared project Shared.UI should remain a shared project. We have classes that use ifdef for platform specific code (which is not available in PCL)


Code

Style.xaml

For testing purposes, I used an empty resource dictionary as follows.

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Shared.UI"> </ResourceDictionary> 

MainWindow.xaml

 <Window x:Class="MyProject.UI.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:MyProject.UI" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyProject.UI;component/Shared.UI/Style.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> </Grid> </Window> 

Question

Everything is displayed correctly in Blend / Visual Studio, no problems arise, and everything is connected correctly. When you debug a solution (click Start ), however you will get the following exception:

An exception

Fixed System.Windows.Markup.XamlParseException HResult = 0x80131501 Message = 'Set property' System.Windows.ResourceDictionary.Source 'threw an exception.' Line number "12" and line position "6".
Source = StackTrace: at System.Windows.Markup.WpfXamlLoader.Load (XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectLameLameLamePrograms Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) in System.Windows.Markup.XamlReader.LoadBaml (stream stream, ParserContext parserContext, object's parent, Boolean closeStream) with System.Windows.Application.LoadComponent (object component, UriProloc.). UI.MainWindow.InitializeComponent () in C: \ Users \ ctoscher \ source \ Repos \ Test \ MyProject \ MyProject.UI \ MainWindow.xaml: line 1

Internal exception 1: IOException: Could not find resource 'Shared.ui / style.xaml'.

The important part is IOException: Cannot locate resource 'shared.ui/style.xaml'.

Question replication

To replicate this problem, you need to open MainWindow.xaml in Blend or Visual Studio. After that, in the IDE, click the ResourceDictionary line that contains the Source attribute (the line that follows)

 <ResourceDictionary Source="/MyProject;component/Shared.UI/Styles.xaml"/> 

In the Properties panel, set the source to Styles.xaml (this should already be selected, as my code above sets this attribute).

Error solution

The source URI is set to /MyProject.UI;component/Shared.UI/Style.xaml . If you remove /Shared.UI from the line so that you stay with /MyProject.UI;component/Style.xaml and run it, it will work!

I also tried changing the assembly action for Styles.xaml to all other actions; no other method works out of the box (without the above modification).


Request Time

  • How can a ResourceDictionary with its Source attribute be correctly linked / generated in Blend / Visual Studio so as not to create these problems in the first place? (I understand that my workaround allows my application to start, but how can I fix this annoyance so that I and my team do not have to edit the link every time?)

I appreciate those of you who actually took the time to read all this. I know this is a long post, however, without creating a zip solution, this is as important information as I can give you.


Edit

Download Solution

I understand that message files are usually not approved, but I believe that this may help resolve this issue. Here is a link to the project (zipped).

Update

By @Yuri (in the comments below / chat transcript):

Here are my thoughts. When you open xaml in Blend, it sees the SharedUI project and assumes it is a DLL because it does not see MyProjectUI. Because SharedUI is a shared project, all files are directly linked to ProjectUI, so when you remove SharedUI from the path, it works.

The problem probably exists as an error in Blend / Visual Studio that does not correct the link generation method for common projects, but if anyone has any suggestions on how to solve this, it would be very useful.

+5
source share

Source: https://habr.com/ru/post/1273279/


All Articles