I have a Window, the only thing it has in it is a TextBlock. I'm getting ridiculous designer exceptions that is crashing the designer every time I change anything. I started removing everything trying to find the culprit of this error. And now I have nothing left, but the designer keeps throwing exceptions.
<Window x:Class="Company.Product.Views.About"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="397"
Width="658">
<TextBlock>Test</TextBlock>
</Window>
code behind:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class About : Window
{
public About()
{
InitializeComponent();
}
}
If I "Click here to reload the designer" the window shows up, but then if I modify the text of TextBlock, I get an exception
Exception
System.ArgumentNullException
Value cannot be null.
at System.RuntimeType.MakeGenericType(Type[] instantiation)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUtil.GetRuntimeType(Type type)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkType.TryGetRuntimeType()
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUtil.EnsureRuntimeType(Type type)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkProvider.GetRuntimeType(Type reflectionType)
at MS.Internal.Package.VSIsolationProviderService.RemoteReferenceProxy.VsReflectionResolver.GetRuntimeType(Type reflectionType)
at Microsoft.Windows.Design.Metadata.ReflectionMetadataContext.CachingReflectionResolver.GetRuntimeType(Type reflectionType)
at Microsoft.Windows.Design.Metadata.ReflectionMetadataContext.Microsoft.Windows.Design.Metadata.IReflectionResolver.GetRuntimeType(Type reflectionType)
at MS.Internal.Metadata.ClrType.get_RuntimeMember()
at MS.Internal.Metadata.ClrMember`1.Microsoft.Windows.Design.Metadata.Reflection.IReflectionMember.get_MemberInfo()
at MS.Internal.Metadata.ClrType.Equals(Object obj)
at System.Collections.Generic.ObjectEqualityComparer`1.Equals(T x, T y)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at Microsoft.Windows.Design.Metadata.Xaml.XamlExtensionImplementations.<>c__DisplayClass5.<FindAttachableProperties>b__4(ITypeMetadata walkType)
at MS.Internal.Design.Metadata.Xaml.XamlType.<GetAllAttachableProperties>d__7.MoveNext()
at MS.Internal.Design.Metadata.Xaml.XamlType.<FindAttachableProperties>d__0.MoveNext()
at Microsoft.Windows.Design.Metadata.Xaml.XamlExtensionImplementations.<FindAttachableProperties>d__7.MoveNext()
at MS.Internal.VirtualModel.VirtualModelPropertyCollection.<GetUncachedProperties>d__0.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at MS.Internal.VirtualModel.VirtualModelPropertyCollection.GetEnumerator()
at MS.Internal.Designer.PropertyEditing.Model.Properties.ModelPropertyMerger.<GetFirstProperties>d__0.MoveNext()
at MS.Internal.Designer.PropertyEditing.Views.PropertyEntryReader.RedraftEntries(IPropertyViewManager viewManager, Selection selection, Boolean attachedOnly, IEventCodeBehindProxy eventCodeBehindProxy, CategoryList categoryList)
at MS.Internal.Designer.PropertyEditing.PropertyInspector.UpdateCategories(Selection selection, Boolean attachedOnly, IEntryReader entryReader)
at MS.Internal.Designer.PropertyEditing.PropertyInspector.RefreshPropertyList(Boolean attachedOnly)
at MS.Internal.Designer.PropertyEditing.PropertyInspector.OnSelectionChangedIdle()
at MS.Internal.Designer.PropertyEditing.PropertyInspector.OnSelectionChangedIdleForced(Object sender, EventArgs e)
at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
I'm left with no way to debug what the problem is, and frankly I getting frustrated considering there should be nothing on this simple example that should be crashing the designer. Any ideas?
Update
After re-installing and updating and reloading, problem went away temporarily. Now the problem is back again.
Actually, I'm not sure if the problem ever went away. I've also tried VS safe mode, /resetsettings, and deleting my several cache folders in the VS folder.
Update2
Here's a screencast which shows the problem Excuse my resolution, I'm in portrait.
Alright, I believe I fixed the problem. I created a new executable project, copied everything from the old one to the new one and tried deleting or adding items one by one to figure out what was special about that project.
It turns out that I'm referencing several 3rd party libraries by Actipro. Earlier, I had upgraded the libraries and copied them to a /lib folder and referenced them from there. One of the references was forgotten though, and I believe it was targetting an older version in the GAC instead of the /lib folder. I think I basically had two different versions of Actipro's shared library referenced.
After removing the erroneous reference to the old build for the 3rd party library, and updating it to the proper version as the others, the problem has gone away
The strange thing, is that the Window in question used no component or anything from that library. It may be that it was trying to resolve the Attached Properties inside the library (from the stack trace).
Try to set the TextBlock's content in the TextBlock.Text property:
<Window x:Class="Company.Product.Views.About"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="397"
Width="658">
<TextBlock Text="Test"/>
</Window>
Maybe that is the issues.
Related
I have this wierd bug. When I build my project, it always pops up two instances of my singleton window. Here is video of it happening. Though I don't think it will help anyting, here is most of xaml of the window. In code behind are just events for mouse movement and a closing event.
<Window x:Class="Scoreboard.OutputWindow"
d:DataContext="{d:DesignInstance viewModel:ControlPanel}"
Title="OutputWindow" Height="375" Width="540"
Background="{Binding AppData.Settings.Color, Converter={converters:ColorToBrushConverter}}"
MouseLeftButtonDown="football_MouseLeftButtonDown"
MouseLeftButtonUp="football_MouseLeftButtonUp"
MouseMove="football_MouseMove">
<Window.InputBindings>
<KeyBinding Modifiers="Control" Key="F" Command="{Binding FullScreenCommand}" />
</Window.InputBindings>
<Grid ...>
</Grid>
</Window>
This window is a singleton in my ViewModel. I access it just from there. It happens on both of my two PCs.
EDIT:
When I remove it from the ViewModel, it doesn't happen anymore. But I sort of need it there.
EDIT2: Here is new project with that window. It doesn't behave exactly the same, but when you do some changes so it builds on debug, then on sturtup it creates another OutputWindow which isn't caught by breakpoint in constructor.
The root cause of your problem is the following line in your ViewModel:
public ViewModel()
{
OutputWindow.IsEnabled = true;
}
This implicitly creates a new OutputWindow() in the getter of the property:
private OutputWindow _outputWindow;
public OutputWindow OutputWindow =>
_outputWindow ?? (_outputWindow = new OutputWindow { DataContext = this });
The constructor then looks like this:
public OutputWindow()
{
InitializeComponent();
Show();
}
This wouldn't be so bad at the moment but every time your MainWindow is instantiated, a new instance of ViewModel is implicitly created as well because of this code in the MainWindow:
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
Okay, that explains why the OutputWindow is showing up on start - but why does it show on build?
(Or in my case even if you open the XAML code of MainWindow.xaml in Visual Studio.)
If found the culprit and it's this little gem here which originates from devenv.exe:
TestApplication.exe!TestApplication.OutputWindow.OutputWindow() Line 15
TestApplication.exe!TestApplication.ViewModel.OutputWindow.get() Line 6
TestApplication.exe!TestApplication.ViewModel.ViewModel() Line 10
...
mscorlib.dll!System.Activator.CreateInstance(System.Type type, bool nonPublic)
...
Microsoft.VisualStudio.DesignTools.Platform.dll!Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.IInstanceBuilderContext context, Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNode viewNode = {Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNode}) Unknown
Microsoft.VisualStudio.DesignTools.Platform.dll!Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNodeManager.CreateInstance(Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.IInstanceBuilder builder, Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNode viewNode) Unknown
Microsoft.VisualStudio.DesignTools.Platform.dll!Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNodeManager.Instantiate(Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNode viewNode = {Microsoft.VisualStudio.DesignTools.Platform.InstanceBuilders.ViewNode}) Unknown
...
XDesProc.exe!Microsoft.VisualStudio.DesignTools.DesignerContract.Isolation.DesignerProcess.RunApplication()
...
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
So, why does it happen?
On build or on open of the XAML designer, Visual Studio starts a thread and instructs XDesProc.exe to create a new instance of your MainWindow.
(XDesProc.exe is the component that is responsible for the XAML designer in Visual Studio and perhaps the designer should refresh if you build a project.)
And from there on, the way is as shown above:
A new ViewModel is created in the implicit DataContext of the MainWindow which implicitly creates a new instance of the OutputWindow in the getter of the property which in turn calls Show() on itself. And voilĂ , there you have your OutputWindow on screen!
In some cases there are even two OutputWindows showing but that's only because the designer (or maybe another component) is called two times.
So, bottom line - what do you need to fix your code?
There are multiple possible solutions.
Most important you should stop the implicit creation of the OutputWindow in the ViewModel. You can achieve this by doing one (or more) of the following:
Remove OutputWindow.IsEnabled = true; in the constructor of ViewModel.
Don't call Show(); in the constructor of OutputWindow.
Don't instantiate your ViewModel implicitly using <Window.DataContext><local:ViewModel />.
I think it's fair to say that it's just plain wrong to implicitly show a window if you just create an instance of a simple ViewModel. So, if you break this unlucky chain of implicit events then your problem will be gone forever.
I'm currently working on enabling drag-and-drop support for our company app.
The error I keep getting seems weird to me.
What this says is that
The property 'DragDropHelper.IsDragSource' does not exist in XML namespace 'clr-namespace:DragAndDrop;assembly=DragAndDrop'. Line 61 Position 83
The property is an attached property in the class I found in the Internet in the samples and modified it a bit. Here is property declaration:
namespace DragAndDrop {
public class DragDropHelper
{
public static readonly DependencyProperty IsDragSourceProperty =
DependencyProperty.RegisterAttached("IsDragSource", typeof (bool), typeof (DragDropHelper),
new UIPropertyMetadata(false, IsDragSourceChanged));
public static bool GetIsDragSource(DependencyObject obj)
{
return (bool) obj.GetValue(IsDragSourceProperty);
}
public static void SetIsDragSource(DependencyObject obj, bool value)
{
obj.SetValue(IsDragSourceProperty, value);
}
It seems to me that attached property is completely valid, isn't it?
This DragDropHelper is included into a class library, that I reference from the main client app.
When I try to set the value of the property in a client app:
<ListView x:Uid="list" x:Name="CurrentFolderItemsControl" drag:DragDropHelper.IsDragSource="true" />
VS2010 says that property doesn't exist in XML namespace. The XAML document is a resource dictionary
which is merged into main client app resources, because it contains styles for our control.
It's even more weird because I created a class within main app that has attached property, then set property value in XAML markup - app compiled OK
Sorry for the necro... just thought I'd share what happened in my similar scenario. I copied my View from an external project, which referenced the behaviour's namespace internally:
xmlns:b="clr-namespace:MyCompany.Common.Behaviours"
b:WindowBehaviours.Close="{Binding ClosingView}"
Two things needed to happen for this to work. Firstly, the xmlns needed to explicitly reference the assembly for the behaviour class, like so:
xmlns:b="clr-namespace:MyCompany.Common.Behaviours;assembly=Common"
Secondly, I temporarily removed the second line and rebuilt my solution first. VS2012 sometimes gets its knickers in a knot when you clone WPF projects, and a good Clean & Rebuild without the offending line often fixes things (it recognised the behaviour after I added the line back again).
Problem solved. My error was I didn't recompile the library after having added attached property. After I did so, everything works as expected. Sorry everybody :(
I had exactly the same problem as you did. I got it to work by placing the [AttachedPropertyBrowsableForChildren] attribute above my setter method. Don't ask me why, it just worked...
I got this error when I defined the control (button in my case) before defining the handler. I had to delete the button and redefine it after creating the method to clear the error.
I have the following defined at the top of my XAML:
<controls:ChildWindow
x:Class="MyProject.SilverlightUI.Views.CharacterGenerator"
xmlns:my="clr-namespace:MyProject.SilverlightUI.ViewModels"
>
<controls:ChildWindow.Resources>
<my:AlignmentsViewModel x:Key="AlignmentsVM" ></my:AlignmentsViewModel>
<CollectionViewSource x:Key="AlignmentListViewSource" Source="{Binding Path=Alignments, Source={StaticResource AlignmentsVM}}"></CollectionViewSource>
</controls:ChildWindow.Resources>
Then later on I use the AlignmentListViewSource as an ItemSource that I bind to a ComboBox. Everything seems to work correctly except the Designer doesn't like the AlignmentsVM resource. It complains like this:
[Xml_CannotFindFileInXapPackage]
Arguments: ServiceReferences.ClientConfig
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See [broken hyperlink]
at System.Xml.XmlXapResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
I verified that the ServiceReferences.ClientConfig file IS part of the SilverlightUI project and is marked as "Content".
The AlignmentsViewModel calls a webservice in it's constructor, but I'm pretty sure this is a problem with the VS designer, not the class. I can compile and run the project and it runs exactly like I want it to from cassini, populating the combo box. it's just the VS designer that isn't cooperating.
Can anyone advise me as to how I can get the designer to recognize the fact that ServiceReferences.ClientConfig IS available?
The Visual Studio Designer actually calls the Control's constructor when showing it. If you access resources from the constructor or the UserControl_Loaded event handler, an exception occurs. I've worked around this (reasonable) limitations by providing save values while in designer.
Field declaration
static bool isInDesignMode;
Constructor logic
// Check for design mode (the Visual Studio Designer is unable to find the AppSettings.xml)
bool isInDesignMode = DesignerProperties.GetIsInDesignMode(this);
Method logic
string cultures = isInDesignMode ? "en,de" : ConfigurationManager.AppSettings["supportedCultures"];
The generated App.xaml.cs file contains this
private void Application_Startup(object sender, StartupEventArgs e)
{
// This will enable you to bind controls in XAML files to WebContext.Current
// properties
this.Resources.Add("WebContext", WebContext.Current);
but I cannot figure out how to refer to this object. It seems like I ought to set the page DataContext = "WebContext" and then use it like this
... property="{Binding Path=User.IsAuthenticated}
I'm sure it's straightforward. Can someone clue me in?
Well done, and of course I can't believe I didn't see that. With the addition of a value converter suddenly a tangled mess of state management code becomes declarative, showing and hiding various UI elements according to whether the user is logged in or in various roles without me having to litter my code with trivial conditionals at myriad logical inflection points.
Try:-
property="{Binding Path=User.IsAuthenticated, Source={StaticResource WebContext}}"
that ought to find it. Its not an object you'd want directly assigned to the DataContext normally, the similarity of the names is co-incidental.
I'm trying to create a UserControl that inherits from a generic class. It does not directly inherit from a generic class, but through an intermediate class that does not use generics. This compiles and works at runtime, but I get an error at design time.
Here's my generic parent class:
Public Class GenericParent(Of T)
Inherits UserControl
End Class
Here's my non-generic parent class:
Public Class NonGenericParent
Inherits GenericParent(Of String)
End Class
Here's my XAML:
<local:NonGenericParent x:Class="SilverlightApplication5.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightApplication5"
Width="400" Height="300">
<StackPanel>
<Button Content="Hello"/>
</StackPanel>
</local:NonGenericParent>
The IntelliSense parser gives the following errors:
The property 'Width' was not found in type 'NonGenericParent'.
The property 'Height' was not found in type 'NonGenericParent'.
The type 'NonGenericParent' does not support direct content.
It is as though IntelliSense can't see up the inheritance tree past the GenericParent class. I've tried specifying the ContentPropertyAttribute directly on the SilverlightApplication5.Page class, the NonGenericParent class, and it does not work.
I've read that the TypeArguments attribute is not supported in Silverlight 2.0. That is why I've created the intermediate NonGenericParent class.
If anybody has any ideas how to silence these errors I'd be eager to hear them.
Update: We've opened a support ticket with MSFT, I'll update this with whatever their solution is.
We've received word from Microsoft that this is not likely to be fixed in future versions. After they bounced the problem around trying to find the responsible group, it appears that this problem belongs to their WPF developer group, which is where the 'not going to fix it' answer came from.
In the meantime, we've updated our code to yank out the generics from the parent classes until I guess XAML 2009.
Not sure about silverlight, but this compiles and runs as expected in c#:
class GenericObject[T] : UserControl
{
}
class StaticObject : GenericObject[Int32]
{
public Int32 wide { get { return this.Width; } }
}
private void Form1_Load(object sender, EventArgs e)
{
StaticObject so = new StaticObject();
this.Text = so.wide.ToString();
}
So if it compiles against the clr, it should work just fine.
Could be just an intellisense bug as you're suggesting. Normally I'd advise against ignoring comiler warnings, but in this case it seems that the warning is not valid.
edit: substituted angle brackets with square brackets cause SO stripped them.
Despite being at 2.0 silverlight (and especially the VS2008 tweaks for silverlight) are still very young. There are still quirks in the IDE stuff.
Do you still have the problem even after a sucessful build?
This blog post seems to be related to your issue:
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/01/22/known-issue-controls-deriving-from-a-generic-base-class-must-be-in-separate-assembly.aspx
For Silverlight it seems that you must have 3 classes for this to work.