Visual Studio cannot resolve a static resource in a WPF window, even if it works at run time - why?

I have a WPF window and I am using the MVVM pattern. I set the view model as a resource for the window as follows:

<Window ...other stuff removed for clarity... xmlns:mvvm="clr-namespace:VisionRT.CRM.WPF.ViewModels.VRTSystems" > <Window.Resources> <mvvm:DhrTemplatesViewModel x:Key="viewmodel" /> </Window.Resources> 

I want to set the window data context for using the resource and find that the following XAML is working ...

  <Window.DataContext> <StaticResource ResourceKey="viewmodel"/> <Window.DataContext> 

The problem is that I can only do this manually, by typing XAML manually, Visual Studio does not show the resource anywhere. I can go to the properties of the window and click the small icon next to the DataContext property, click the "Apply Resource" option, but it does not display the "viewmodel" as a resource, static or dynamic. If I enter XAML manually and then open the Apply Resource pop-up window, it has a “visible model” that is underlined as an error, and hovering above it displays a pop-up prompt “cannot resolve the resource link”

However, when I run the application, it works fine, so the resource resolved at runtime.

Can anyone explain this? I would really like this to be possible with the help of the VS Property Editor, since I think this is more convenient than entering XAMl manually. I am also worried about the fact that VS cannot solve it. It makes me think that I am doing something wrong.

Thanks for any explanation you can give.

+4
source share
2 answers

The only (sad) explanation is that XAML is a second-class citizen in Visual Studio. After you start to press XAML a little more than the basic one, and in the end you will get an “unresolved” “inappropriate display”, “sorry, I'm dumb”, etc.

Refer to this WPF suggestion: http://dotnet.uservoice.com/forums/40583-wpf-feature-suggestions/suggestions/480899-make-xaml-a-first-class-citizen-of-visual-studio?ref= title to fix them.

+2
source

You can make Intellisense 100% for XAML {StaticResource} in Visual Studio.

Tested

  • WPF / C #
  • Visual Studio 2015 Update 3

Step 1. Moving resources to a common project

The key is to move all the resources referenced by the ResourceDictionary to the shared project:

enter image description here

Step 2: intellisense development time

We are not quite there yet. XAML runtime starts the runtime if you enable resources twice.

If we include the .xaml file more than once in the entire project, we can include it at design time, but not at run time:

 public class DesignTimeResourceDictionary : ResourceDictionary { private Uri source; public new Uri Source { get { if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue) { return null; } return this.source; } set { this.source = value; } } } 

Then we can add development time resources:

 <!--Design time resource dictionary, so intellisense will work with with {StaticResource}.--> <ResourceDictionary.MergedDictionaries> <dr:DesignTimeResourceDictionary Source="/Project.Name;component/Folder/SharedResourceA.xaml" /> <dr:DesignTimeResourceDictionary Source="/Project.Name;component/Folder/SharedResourceB.xaml" /> </ResourceDictionary.MergedDictionaries> 

If we use ReSharper, it will suggest automatically adding the namespace prefix to the header:

 xmlns:dr="clr-namespace:MyNamespace;assembly=Project.Name" 

Appendix A: Extra for Experts: Why Do I Need Stage 1?

As an aside, Intellisense in XAML is the same as Intellisense in C #: it only works with referenced subprojects. That's why if projects A and B want to share some code, you have to put it in the C class library referenced by projects A and B. This is unlike C or C ++, where the #including file fundamentally makes it available for all subfiles.

With XAML, you can spoof and add static resources to the Main Application Project (see above), and the XAML runtime will work fine - but Intellisense will not work during development, presumably because it is based on the same engine that is used for c # intellisense.

To get Intellisense to work all the time in XAML, Visual Studio Intellisense would have to scan through each parent project (instead of just going to the project links).

+1
source

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


All Articles