I have a custom control that I have written that does some location checking in the Load event of the control. This location checking moves the control to a specific location on the screen based on settings in its parent control. This code works as expected when I compile and run the application.
However, when using the Visual Studio designer, it causes my control to be painted outside of the viewable area and I cannot use the designer. Is there a flag or attribute I can set to stop that snippet of code from running ONLY when in the Visual Studio designer?
The only work around I have right now is to comment out the code, compile, launch designer, make my changes, then uncomment the code and re-compile.
You could check the value of LicenseManager.UsageMode.
if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
{
// Code here won't run in Visual Studio designer
}
For those to whom the proposed solution has not worked, I share the following hack:
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName != "devenv")
{
// Code that should not be executed at design time
}
What we are doing is getting the name of the process that executed the code. If the name of that process is "devenv", then we do not execute the code.
In the latest versions of Visual Studio, the environment runs in a process named "devenv".
I confirm that this works in VS2015 and VS2017
Yes, I know it's not the most elegant solution, but it works. Yes, I know it's an old query, but there are still some dinosaurs -like me- sticking their hands in this.
Slight variation, exits from Load method if Design Time is active...
this.Load += delegate {
if(LicenseManager.UsageMode == LicenseUsageMode.Designtime) return; //If Design Time -> Exit here
// Code here won't run in Visual Studio designer
// ...
};
Related
It happened again. My test data is not shown any more. I see only class and property names. Restarting does not help this time. Retracing neither.
I've been rewriting my whole project since that happened.
Now I tried to use Test Data class to address it from custom user controls so it would look like:
...d:DataContext="{Binding Source={DynamicResource TestData}, Path=Emploee}"...
//in resources
<main:TestData x:Key="TestData"/>
Not like it was previously:
...d:DataContext="{DynamicResource TestEmploee}"...
//in resources:
<shar:PVEmploee x:Key="TestEmploee" Name="Alpha" Surname="Omega" Phone="77777777" PVProfession="{DynamicResource TestProfession}"/>
<shar:PVProfession x:Key="TestProfession" Name="Some obnoxeously long profession name"/>
And right after those changes in this control (I've done it previously few times already to other controls, I've copied from previous project version) the designer crashed. Now I see
this ridiculousness, instead of test data
I am frustrated. Am I supposed to restart it again? Or there could be some way to fix it.
Answer to almost not realated question Actually worked well (the question was - how to crash a Designer =D ). If I kill designer in Task Manager, and click 2nd option in Studio. The Designer loads pretty well in a right way. All my Test Data right there. Or I found later that I can click small document icon on designer bottom to reload it with or without user code.
After a few such resets it started behaving. Marvelous!
I've got a Windows form with a ZedGraphControl, and for some reason whenever I rebuild it in Visual Studio while the gui builder is open, the control randomly resizes itself & changes its position. Here's a screen capture showing the behavior:
http://screencast.com/t/WxrLngp1VLf
*If I rebuild it a second time immediately after, it moves & resizes once again (in other words, it moves over & grows each and every time I build).
*I am not touching this control anywhere in my code; I just dropped it in the dialog and rebuilt.
*It behaves the same way in VS2008 and VS2013.
*If the gui builder is not opened when I build, it always remains as it should.
*I tried adding it within a panel (in case it was doing something weird based on properties of the container), but it behaves the same.
I'm totally stumped, and this makes it very cumbersome to work on the dialog - anytime I have to build I have to close the gui builder, build, then reopen it (otherwise, I have to keep manually moving the graph control back to where it was supposed to be)...
...Figured it out about 15 minutes after posting (d'oh!). The solution was to change the parent form's AutoScaleMode from None to Dpi. Looks like a bug in ZedGraph!
I am writing a Visual Studio extension that hovers above everything inside the text editor (above selections, text, etc).
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("csharp")]
[TextViewRole(PredefinedTextViewRoles.Interactive)]
internal sealed class MyAdornmentFactory : MyAdornmentFactoryBase, IWpfTextViewCreationListener
{
[Export(typeof(AdornmentLayerDefinition))]
[Name("MyName")]
[Order(After = PredefinedAdornmentLayers.Text, Before = PredefinedAdornmentLayers.Caret)]
[TextViewRole(PredefinedTextViewRoles.Interactive)]
public AdornmentLayerDefinition EditorAdornmentLayer;
// ...
}
Problem is, that my WPF UserControl always "hides" behind the text - no matter how I play with the OrderAttribute.
Am i doing something wrong? is there something else that can influence this behavior?
EDIT: I can now confirm that it ~sometimes~ work, possibly with connection to running without debugging... does anybody know of a related visual studio bug?
It should Work, or better for me is working setting order in your exaclty way.
eg if you start from the template for Highlight 'A' in every line called TextAdornment (comes with vs2010 SDK) and change the zorder from
[Order(After=PredefinedAdornmentLayers.Selection, Before=PredefinedAdornmentLayers.Text)]
to
[Order(After=PredefinedAdornmentLayers.Text, Before=PredefinedAdornmentLayers.Caret)]
the adornments will be placed over the editor Text.
hope it hepls.
I'm using Visual Studio 2010 to create a small WPF application. I've created a user control that I am now trying to add to my main form. The user control does show up in toolbox but every time I try to drag the control to the form I get the error:
The enumerator is not valid because the collection changed.
I should know what's wrong and it is bugging me that I can't figure it out.
You have a bug in the constructor of the usercontrol - you are using a foreach-loop over an IEnumerable and while the loop is running, the IEnumerable is changed, this is not allowed with a foreach loop. Use a for loop instead if you are manipulating the Collection you are iterating over.
The problem here is that you don't know what code is throwing the exception.
WPF is terrible about exceptions, especially in constructors. The framework insists on catching and re-throwing a new exception, usually multiple times, and it's difficult to find the original stack trace. I've found the easiest way to track down this kind of error is to tell Visual Studio to stop as soon as the exception is thrown, rather than waiting until WPF has re-thrown it a couple of times and made the details difficult to dig out.
I don't have Visual Studio 2010 in front of me, but here's how to do this in VS2008 -- 2010 is probably similar:
Go to the Debug menu > Exceptions...
Next to "Common Runtime Language Exceptions", check the box in the "Thrown" column
Then debug your app again. It will stop at the line that's actually causing the problem, and it'll be much easier for you to see what's going on. And if you're still not sure why it's throwing an exception, you'll be able to post a code sample.
In order for a user control to function properly you need to have a constructor that takes zero arguments. This way the form designer has a way to render the control in a "default" manner.
I then overloaded my constructor to take the arguments I needed to actually run the control properly and everything worked as expected.
You need to:
Remove the DLL reference
Add a reference to your control
Rebuild the solution
Add your control. It should work!
Start a new Silverlight application... and in the code behind (in the "Loaded" event), put this code:
// This will *NOT* cause an error.
this.LayoutRoot.DataContext = new string[5];
But...
// This *WILL* cause an error!
this.LayoutRoot.DataContext = this;
The error that is raised is "Value does not fall within the expected range." BTW, this code works 100% in regular WPF (Windows development), and there should be no reason why it won't work in WPF/E :)
Any thoughts?
You can't currently use visual elements as a data source for data binding in Silverlight 2. I think this is slated to be added for Silverlight v.Next.
You can use visual elements as data source if you create binding directly in the code, but trying to assign visual element to DataContext will throw ArgumentException. It doesn't make much sense, but silverlight is just on version 2.