For the past two weeks, I have a problem which I cannot resolve. Believe me, I have been looking for answers all over the internet without finding any solution let alone hints.
I am currently working on a Windows 8.1 Universal App. I have developed a pattern lock control (control A) similar to the one known from android devices. The control is composed of 9 points which are represented by instances of a different control (control B). Control A as well as controls B have their own dependency properties. Some of which have concrete values (e.g. StrokeBrushThickness="2") while others have a value that is defined within the app resources (e.g. Brushes).
The problem I am facing is that everything works well for the Windows Phone project. However, when I reference control A inside the Windows project I receive following XAML error for the A's dependency properties that have concrete values:
Unknown error: Cannot find a resource with the given key.
If I remove the aforementioned properties from the control, the app compiles but throws a Windows.UI.Xaml.Markup.XamlParseException during runtime:
WinRT-Informationen: Failed to assign to property < PROPERTY NAME >
Moreover, I get the following XAML error for a some resources I have defined within the app, e.g.:
<Color x:Key="ColorBackground">#F4F2F4</Color>
<SolidColorBrush x:Key="AppBackgroundBrush" Color="{ThemeResource ColorBackground}"/>
I can sort of get the whole thing to work if I remove control A and place the point controls (B) directly into a grid. However, I hope to avoid this workaround as you can image control A encapsulates a lot of functionality that is needed repeatedly across the app.
I hope I have made myself clear. If not, I'll be more than happy to provide further information.
Thank you in advance!
Probably you have failed to register your dependency properties correctly.
Check the parameters you pass to DependencyProperty.Register method.
Probably you throw an exception from your property changed callback, you shouldn't do that.
P.S. It is sometimes easier to use Expression Blend to edit those XAMLs. You will spend some time learning, the tool is not targeted towards programmers; it is for designers, so the user experience is completely different. However, if you will invest some time, it is very unlikely you will ever face problems like “Cannot find a resource with the given key”.
Related
I have a Winforms project with a FormMain.cs. I can build the project without error and it functions as a start up project.
However, when I click on FormMain.cs to access the design view, I see the following design-time error list:
Could not find type
'UserControlsTabbed.UserControlTabPanel'.
Please make sure that the assembly that contains this type is
referenced. If this type is a part of your development project, make
sure that the project has been successfully built using settings for
your current platform or Any CPU.
and
The variable 'userControlTabPanel1' is either undeclared or was
never assigned.
I certainly recall using the design view of FormMain.cs. I recall placing userControlTabPanel1 in a tableLayoutPanel cell within FormMain.cs. I did not have any design-view errors at that time. That was perhaps 100-150 version ago in my code repository. I have not had any need to access the design view of FormMain.cs until now.
The second error message is especially confusing. Opening FormMain.Designer.cs I see that the auto-generated code contains both the declaration and assignment.
I am confused as to why the winforms application executes without issue but the design-view has errors. Any assistance would be appreciated.
There are a number of issues causing this problem.
Project Platform target must be set to x86.
TableLayoutPanel do not support visual inheritance. Instead, use containers that support visual inheritance such as GroupBox, Panel, SplitContainer, or TabControl.
Inheriting usercontrols that do not conform to 1. and 2. of this list causes errors in the design-view.
By implementing the above 3 observations in my solution I was able to successfully remove the Design-time errors.
This might be a very stupid question but from what i understand quite a substantial amount of SILVERLIGHT is a subset of WPF. I've also made a side to side comparison of the references in a new SILVERLIGHT application and WPF application. Judging by the names, many can be applied to the other.
But there's a particular reference I'm looking for to place in my 'PF app that's not present in the list of available references but is there in the SILVERLIGHT NAVIGATION APPLICATION' references.
System.Windows.Controls.Navigation
This should allow me to (In theory anyways) change a source page on the main window by button click which is (from what I've researched) only available by default in a SILVERLIGHT NAVIGATION APPLICATION'. e.g click button 1; MainWindow shows page A. Click button 2; MainWindow shows Page B.
So my question is it it possible to add this reference to WPF like so many others seem to be? Or am I misleading myself by assuming that just because they're named the same they're the same thing? Knowledge much appreciated.
Silverlight uses a different CLR than WPF. The CLR used by Silverlight is much smaller and lacks many features that WPF enjoy. Occasionally it also has some features that the main .net framework does not have.
Due to this difference any DLL cannot be directly interchangeable between Silverlight and WPF unless they are created as so. By default the following libraries are interchangeable ...
Mscorlib
System
System.Core
System.ComponentModel.Composition
Microsoft.VisualBasic
In your case it might not be possible to reference that dll from WPF
More information
As Tanmoy says, the actual assemblies to reference even for features common to both WPF and Silverlight may differ.
That being said it is my understanding that WPF and Silverlight share many of the navigation concepts and you can even write a WPF browser application that would behave and look like a Silverlight application in a similar way.
The entry point for reading on this subject for WPF is here.
Is it possible to visualize an object (its properties along with their values) and print it out (dump it - similar to serialization) to a WPF control, such as TreeView or PropertyGrid to inspect the object?
The goal is to display the contents of any arbitary object (not only for debugging purposes).
For further clarification: I'm not looking for any debugging tools or ways to show the WPF Visual Tree. This question has only partially something to do with WPF -> WPF is only the media to display the object dump because controls may vary between WPF and WinForms.
The output should be hierarchical for nested object instances, lists etc.
I think you should take a look at Snoop
http://snoopwpf.codeplex.com/
This program will allow you to navigate the WPF tree of any running application. Debugging is not required for this tool and it's possible the tool doesn't work with debugging. Typically I use it in non-debugging scenarios to see how my WPF controls are actually laid out and what values they have for various properties
I believe what you are looking for is the System.Diagnostics.DebuggerDisplayAttribute
You mean besides the WPF Tree Visualizer? there is Mole, which is not free anymore, but very good.
Edit:
Reading your edited question. You are explicitly naming the PropertyGrid, I take it you've already tried Extended WPF Toolkit's PropertyGrid?
So you want a control that displays at runtime the fields of a class. You will find plenty of articles regarding that by looking for "Property grid". Its not directly what you want but its a start. You basically iterate via reflection over the fields of a class, and display them in a ListView/TreeView. But, and this is were the difficult part starts, determining which fields to show and which to hide, handling with very different types and primitives and allow to edit them with type conversion (like string to Rect, point, color etc) is a very complex matter.
This control might give you a good starting point.
There are various existing controls that lets you view properties as Property Grid.
http://www.codeproject.com/Articles/87715/Native-WPF-4-PropertyGrid
https://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid
https://wpg.codeplex.com/
Based on complexity, License and features they present, you will have to choose one, all of them are free for sure.
I've been searching for the answer to this for months; Snoop, Spy and all the others didn't work for me, due to thread ownership violations.
Microsoft has a windows-tool that allows you to select any running UI Element and view the Element's accessibility data:
inspect.exe
https://msdn.microsoft.com/en-us/library/dd318521(VS.85).aspx
It's available in the windows Software Development Kits which need to be downloaded and installed, and located in:
C:\Program Files\x86\(win-version)\bin\(cpu-architecture)\inspect.exe
I'm creating a UserControl that exposes some dependency properties, which must be populated for the control to work.
When I use the control in XAML, I can provide values for the dependency properties. But if I fail to set all values, it won't be apparent until run-time (exception will be thrown).
Is there any way for a UserControl to mark certain dependencies as required? If this were a plain C# class, I would make its dependencies as constructor parameters. But I'm not sure what's the right way to something similar in the WPF/XAML world.
We're using .NET 3.5 and VisualStudio 2008.
A possible solution would be to let the control render an error message in its visual area, that during design time hints the developer at these properties and urges him to assign values to them.
An example of such a behaviour is the Bing Maps Control, which requires its CredentialsProvider property to be set by the application programmer. As long as the CredentialsProvider isn't set, the map shows a large label in its center with an appropriate message.
In contrast to the Bing Maps Control you may consider to show such a message at design time only and during runtime throw an exception instead.
I am new to WPF. I have been reading a lot about WPF and dependency properties. My understanding of Dependency Properties is to bind properties to XAML (XAML being the target). I have envisioned a program in which a single configuration window would control all visual elements of the rest of the program (font, font size, colors, etc).
In my first attempt, I created a separate window that would contain all the different configuration options. This failed because I was unable to figure out how to use the dependency properties in separate XAML files.
Also, the dependency properties can only be registered in a class that implements directly or indirectly the DependecyObject class. How do I know which wpf objects implement it? It appears the window does not, but I could be wrong. My second attempt was this, to register the dependencies in the top window, so the whole visual tree would have access to it. I had exceptions being thrown and the only thing I could think of was that the window does not implement to DependencyObject. (I could be mistaken, and the visual tree thing is still sort of a mystery to me. I am learning though)
So now I'm at a loss. Is what I am attempting even possible? The whole purpose of this project was to help me learn WPF and now I'm wondering if I should lower my expectations of what I can accomplish with WPF.
Edit:
What kind of exception? What are you trying to do? Could you paste some code?
public partial class MainWindow : Window {
public Color BackColor {
get { return (Color)GetValue(BackColorProperty); }
set { SetValue(BackColorProperty, value); }
}
public static readonly DependencyProperty BackColorProperty =
DependencyProperty.Register("BackColor",typeof(Color),typeof(MainWindow),
new UIPropertyMetadata(0));
It appears that Color was causing the exception. I changed it to int and no exception was thrown. So are primitive types only able to be registered?
WPF is a framework to help you bulit really good looking apps (it includes also Printing etc.) What are you trying to do? Should it be a part of Visual Tree?
Again, this project was only meant to get me to learn WPF. My idea was to create a main UI with different pages. A configuration button would bring up a window with options to change font, fontsize, background color, etc. These changes would be applied in the main UI as the user made the changes. My issue has been how do I access those dependency properties outside of the configuration class where they were registered? How do I have one window that can have slider and combo boxes, and any other class can access those configuration settings?
Put simply, I would like that a single configuration (fonts and colors) be somehow inherited by all UIs. And that that configuration can be changed and seen by the user.
Also, the dependency properties can only be registered in a class that implements directly or indeirectly the DependecyObject class. How do I know which wpf objects implement it? It appears the window does not, but I could be wrong.
http://msdn.microsoft.com/en-us/library/system.windows.window.aspx
look at the Inheritance Hierarchy it does implement DependecyObject
My second attempt was this, to register the dependencies in the top window, so the whole visual tree would have access to it. I had exceptions being thrown and the only thing I could thing of was that the window does not implement to DependencyObject.
What kind of exception? What are you trying to do? Could you paste some code?
So now I'm at a loss. Is what I am attempting even possible? The whole purpose of this project was to help me learn wpf and now I'm wondering if I should lower my expectations of what I can accomplish with wpf.
WPF is a framework to help you bulit really good looking apps (it includes also Printing etc.) What are you trying to do? Should it be a part of Visual Tree?
We cannot tackle here the big task of learning a whole new framework and designing your whole program. That will take time. It can be daunting but stick with it.
As for your exception, the problem is that the mysterious:
UIPropertyMetadata(0)
is actually providing the default value for the dependency property and this value must match the type specified in the second argument to Register. Since 0 is an integer and your property is a Color the dependency property subsystem throws an exception. Instead you can use:
UIPropertyMetadata(new Color())
or any other color as the default.
Dependency properties are a newer design specifically created for WPF. One of the problems they solve is to create faster resolves of the property values because they don't use older, slower, techniques (Read up on why they were created).
They are hard at first, to understand, because it's not exactly clear why they are needed when there is support for the Interface named INotifyPropertyChanged. But one main reason (among others) is that if you implement a DependencyProperty correctly, you will be able to change the design time property values of that property in the property window! Now this is a very cool thing to do because you can very easily set all of your default properties that way.
Doing this for the sake of design time property editing allows you to learn a lot more about DPs and why they are used. In fact, some prefer them everywhere, as they just are not that hard to code up.
DPs are also used when creating custom controls using the "Generic" folder method. This is how Microsoft themselves create all their controls.
There are some caveats to be aware of when using them. You cannot see the design time properties of a DPs until that particular control is contained by something else. In other words a UserControl with DPs will not show those Dps in the UserControl design. They will; however, show up in the control that contains that user control.
DPs bring maximum control using Metadata, PropertyChanged callbacks, default values, and Cohersion and other techniques which give you 100% control of what they contain. The DP is the ultimate in property control.
Keep scouring the internet as there are tons of articles out there on the topic.