Home Dashboard Directory Help
Search

Visual Studio can't resolve static resource in WPF window, even though it works at run time by Mr Yossu


Status: 

Closed
 as Fixed Help for as Fixed


5
1
Sign in
to vote
Type: Bug
ID: 666201
Opened: 5/2/2011 8:52:15 AM
Access Restriction: Public
0
Workaround(s)
view
1
User(s) can reproduce this bug

Description

I have a WPF window and 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's data context to use the resource, and found that the following XAML works...

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

The problem is that I can only do this by typing the XAML manually, Visual Studio doesn't show the resource anywhere. I can go to the window's properties and click the little icon next to the DataContext property, click the "Apply resource" option, but it doesn't show "viewmodel" as a resource, static or dynamic. If I enter the XAML manually and then open the "Apply resource" pop-up window, it has the "viewmodel" underlined as an error, and hovering over it shows a tooltip "cannot resolve resource reference"

However, when I run the application, it works fine, so the resource *is* being resolved at run time.

I would really like to be able to do this through the VS property editor, as I find that more convenient than typing the XAML by hand. I'm also bothered by the fact that VS can't resolve it. This look like a bug to me.
Details
Sign in to post a comment.
Posted by Mr Yossu on 5/29/2011 at 5:29 AM
OK, thanks for the clarification.
Posted by Microsoft on 5/26/2011 at 1:38 PM
So, to the product group here at MS, Fixed means that the codebase in the main branch has been intentionally changed to the degree that the issue no longer manifests. What this means in this case is that I can run today's build of VS, set up your scenario, and the UI works to spec and corresponds to what the user would expect. The issue that you've described does not manifest any longer: to be precise, there is no false message or indication that a resource does not resolve. That was the problem, and it's gone away. That main-brach-code will be the codebase used to build the next major or minor release of the product, however it's possible that a preview release will be published before the next retail version and the fix will be in that release too.

This fix does not qualify for a hotfix. Hotfixes are rare, they typically apply to a bug that would be considered a ship-stopper and a good example would be something that blocks productivity or adversely affects some mission-critical aspect of an enterprise.

Cheers,
Steve
Posted by Mr Yossu on 5/26/2011 at 1:28 PM
When you say it's fixed, what do you mean? It still doesn't work here.

When would we expect to see a hotfix available for users?
Posted by Microsoft on 5/26/2011 at 1:22 PM
Update: the issue has been fixed now. I am tracking an internal task to make sure we don't break it again in future and that's a test-only task. I didn't want you sitting around monitoring the test task though so I resolved the original issue Fixed, which is correct.

Cheers,
Steve
Posted by Mr Yossu on 5/26/2011 at 4:02 AM
Hello,

Why has this been closed and marked as fixed? Even if this issue only applies to that pop-up window, it's still a bug and needs fixing. There is no reason why you shouldn't be able to add a resource to the dictionary of the referencing element. As you can see from my example, it's a very logical thing to do when binding properties of a viewmodel in XAML. Or do know of a better way to do this?
Posted by Microsoft on 5/25/2011 at 2:22 PM
Hi! I see - the Resource is only 'unable to be resolved' in that particular popup window. By the way, this issue applies to any resource type (not just viewmodels) and it only happens if the resource is in the resource dictionary of the referencing element. I'll track this problem and do my best to get it resolved.

Thanks for your patience!
Steve
Posted by Mr Yossu on 5/25/2011 at 7:42 AM
Hello,

Thanks for the reply, but I think you're talking about something else. Did you follow the steps I described in my post? If so, you should see the error.

Just to clarify, please open the sample project I attached. Look at the XAML in the MainWindow.xaml file. You can see that I set the viewmodel as a resource for the window, then used it for the data context of the window. The relevant code is what I posted in my original message.

If you run the project, this works fine, so the resource is being resolved at run time.

However, you can only apply this resource by typing the code manually, you CANNOT do this through the visual studio binding pop-up window.

To see this, look at the images in the images.zip file I attached...

image1.gif shows when I had selected the window in the XAML, then clicked the small square icon next to the DataContext property in the properties pane. You can see that there is an option in the menu for "Apply Resource..."

If you choose that option, you get what you can see in image2.gif (after you click the little arrow to minimise the System Colors section). As you can see, the view model does not appear in the list of resources offered, and where it appears in the small textbox at the bottom (presumably picked up from the XAML), it has a dashed underline, with a tooltip that says "cannot resolve resource reference" - meaning that visual studio claims it cannot resolve the resource.

Please confirm that you can see this, because it is happening here, and other developers I have asked see the same thing. If you can't see this, then we cannot go any further.

Now, assuming you see the same behaviour, we are left with the fact that you can type the XAML manually, and the resource is resolved when you run it, but you CANNOT do the same thing with the visual studio resources pop-up window as vs doesn't offer the resource, and claims it cannot resolve it when you enter it manually.

Please follow these steps carefully, and confirm that you see the same as me. Your reply makes me think that you either misread my original post, or didn't see the same problem.

Thanks
Posted by Microsoft on 5/24/2011 at 3:26 PM
Hi, I think I understand the issue which is that, if a resource is in Window.Resources, then you cannot reference that using a StaticResource reference in the Window's open tag but you can if you add a property element later in XAML than the Resource definition. Using a DynamicResource reference does not exhibit this problem.

What I'm really interested in is how you can get into the situation where a reference works at run time but not at design time so I'd like to see an example of that. I don't see an example of that in the attached project. Could you confirm what I say above is what you're referring to and then provide an illustration that supports what you say here: "...and hovering over it shows a tooltip "cannot resolve resource reference" However, when I run the application, it works fine, so the resource *is* being resolved at run time."

Thanks,
Steve
Posted by Mr Yossu on 5/5/2011 at 10:40 AM
Hello,

I've attached a simple project that shows the problem. Yu can see that the data context is resolved at run time from the fact that the Welcome property on the view model is bound to the TextBlock in the view, but if you click on the window itself in the designer and try to set the data context using the Apply Resource pop-up, you can't.

Thanks
Posted by Microsoft on 5/5/2011 at 9:06 AM
Would it be possible for you to attach the XAML file / project.
Posted by Microsoft on 5/2/2011 at 9:38 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.


Posted by Microsoft on 5/2/2011 at 9:16 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
File Name Submitted By Submitted On File Size  
MvvmSample.zip 5/5/2011 110 KB
images.zip 5/25/2011 45 KB
images.zip 5/25/2011 45 KB