Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A loop was detected in the property expressions

I'm working on a VS 2012 Express WPF project (C# code-behind) and I'm getting the error "A loop was detected in the property expressions". Now before I continue, I know why I'm getting it - I'm just not sure what to do about it or if it even needs to be handled because even though it shows up in the error list and is underlined in blue, the program compiles and runs just fine. I've Googled this of course and results have varied. My question is a two-parter but first, here's the rundown:

The offending code:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<!-- setter properties -->
</Style>

As you can see, it's a style for Button that refers to Button which is what causes the issue. The first workaround I read involved separating the code like so:

<Style x:Key="ButtonStyleToApply" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    <!-- setter properties -->
</Style>
<!-- and in another style dictionary, -->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyleToApply}"/>

...and then the two style sets go into different dictionaries. Obviously, this didn't do any good either - VS still sniffed out what I was doing but it still compiles and runs fine. Now here is yet another workaround:

"There were some architectural changes in the XAML Designer in Visual Studio which has regressed this scenario. We will work to improve this experience in future but right now you can work-around this issue by not defining the resources in the Application object (App.xaml) but instead defining them in the local document (MainPage.xaml)"

This was posted in a tech support query Here. (Connect.Microsoft.Com)

But I trust hands on experience from the every-man. What can my fellow coders tell me? My question is a two-parter. 1. Do I even need to bother with the workaround if the program is compiling? And 2. If I'm good to go, is there a way to tell VS to just ignore it? I know ignoring it on my part or VS's is probably considered "bad practice" but beyond that...?

like image 903
Kyle Vegas Avatar asked Feb 12 '13 21:02

Kyle Vegas


1 Answers

There is nothing wrong with the syntax you are using; it's legal to define an implicit style that is based on an existing implicit style. The code sample you provided works for me in VS 2010 with no complaints, but produces the error in question in VS2012.

Since it also compiles and runs fine in both versions, I suspect the problem is with the VS 2012 XAML designer support. How big of a deal this is for you depends on how much you rely on designer rendering for your WPF views. (For example, I usually hide the rendered view and work exclusively in the XAML view.) If you do use that feature, you may notice that not all of your properties are being set properly at design time. Of course, since the setters usually don't have much effect until run-time anyway, you may never notice the difference.

And, as your question correctly points out, even Microsoft has identified this as a regression in VS2012, which strongly suggests that they don't expect the error to happen and will likely fix the designer bug at some point in the future.

like image 59
Michael Edenfield Avatar answered Oct 03 '22 19:10

Michael Edenfield