Vous êtes sur la page 1sur 5

Resources:

---------- Resources are shared by default, Also lazy loaded


--------------------------------------------------------Template Reuse:
- Each instance of template generates its own copy, They are generally a factor
y which creates a visual each time asked
- templates are saved as resources,
----------Any control can be separated into a separate xaml , with that UI and reused as a
UserControl.
- change the code behind inherited from page to that control.
- you can change the ui from designer,
- import as a class into another controls.
- Another way of creating the usercontrol.
----------UserControls
- Template can not be used
- XAML of usercontrol can only be set once
-----------XamlReader, XamlWriter classes to manipulate xaml at runtime,
-----------Decorator - this provide bounds for the adorners, the contain a list of decorato
rs
Adorner - Visual elements attached to some other elements, adorners alwasy appea
r on top, they always follow the target object
Panel, Shape, FrameworkElement => Derive directly from non control element type
---------------Commands in CustomControl
- CommandManager.RegisterCommandBinding to register command
- static command, with static method, and static ctor to bind the command, clas
s level binding is more efficient that instance level
Events in CustomControl
- EventManager.RegisterRoutedEvent, as a static registration
- RoutedEventHandler, calling addHandler, removerHandler
- RaiseEvent with RoutedEventArgs
- Bubbling and Tunnling events can be provided by a pair of events
- Also a class level eventHandler can be registered using RegisterClassHandler
event
------CustomControl
- should also define a template for the control using TemplatePart attribute at
the class level, multiple parts allowed
-------------------Triggers:
- on Elements, only Event triggers can be set, not the properties triggers. One
can use style triggers to do the same as work
around.
------Themes in CustomControls
- Themes should be in separate xaml files
- Must have a generic theme even if you have xaml for all the files
- ThemeInfo attribute must be there in the assembly
- Other themes can be in other assemblies, with this assembly ResourceDictionar
yLocation set to ExternalAssembly.
- Ext theme dll should be named ending with theme names.
- Allowed to give any template
-----------------------------------------------------------------

WPF Styles
- Only be applied to DPs
- Inline/ through ref to Style Property
- Self Key type:- If you dont specity key, it you can control x:Type to apply t
o all instances of that type,
- Can be used to Skin the applications, by defining, multiple skins each set of
styles defined for each type
- Styles can be extended by using BasedOn attribute pointing to another style,
This wires up both the styler
- Application gets to apply JUST one style to an element, Scoped from Narrower
to Wider
- Styles cascade down to the children of the applied element
- Styles can have properties which do not match target type, non matchin proper
ties are ignored
- Styles can have triggers, which only apply to the target of the element
- When binding data one can't provide your own containers in which case, ItemCo
ntainerStyle should be applied.
- Controls default look comes from styles coming from theme
- Controls default style coming from default theme, this is called DefaultTheme
Style
- Another Style comes from Application, called Application style,
- These 2 combine to create the look for the control, However Control can have
only one Style coming from Application
- Implicit style (coming from ancestors), explicit styles(directly applied), de
fault styles (theme style)
---------------------------------------------------------------------Template vs Styles
- Templates should be used to project properties from one form to another
- Styles should be used to configure the look on any form.
- Templates should not set the look propertie, only project properties which of
values and data.
- However, one can set the look related properties from templates as well.
-----------------------------------------------------------------------------------------------------------------Binding
- Async = true for not holding the thread on Waiting UI, it mean while gets th
e Fallbackvalue
------------------------------------------------------------DispatcherObject:
- thread affinity class in wpf, to implement STA,
- Only the thread that created the dispatcher may access a dispatcher object. U
I thread can only be assessed through dispatcher
- VerifyAccess and CheckAccess
--------------------------------------------------WPF Dispatcher:
- In WPF, a DispatcherObject can only be accessed by the Dispatcher it is assoc
iated with.
- The Dispatcher maintains a prioritized queue of work items for a specific thr
ead.
- UI handling class in wpf, all bg thread can push a message to dispatcher to e
xecute on ui thrad.
- dispatcher executes them based on priorities.
- its implemented using Priority queue
- There are different priorities. Normal is preety high priority.
---------------------------------------------------------------------------Freezable:
- Freezables are a class derived from Dispatcher obeject with 2 states. Unfroze

n and Frozen, when its unfrozen it behaves like any other obejct and shows threa
d affinity
- When its frozen it stops taking part in wpf notification system and its freez
es its thread affinity, upon which it can be passed to different thread.
- Its mostly used for grephics level objects.
- Graphics systems deal with many low level resources and upon changing the val
ue, those low level objects needs regeneration.
- On freezing it stops regeneration of those objects.
- not all freezable can be frozen, Check Canfreeze before freesing, to avoid in
valid operatione ex.
- Objects with Animation, sub-objects which can't be frozen, or a property set
by a dynamic resource can't be frozen.
- Once frozen it can never be unforzen, but and unfrozen clone can be created.
---------------------------------------------------------------------------Dependency Properties:
- Properties which take pare in WPF property system
- Over Dependency Object
- Traditionaly, each control lot of properties at the object instance level, mo
st of the values would remain default still an independet object would exist, th
is used to waste so much memory and space.
- In WPF, came up with idea of storing only changed values and a system to disc
over the value of the property at runtime,
- which is called WPF property system
- DPs are implemented using space data structure, (if null if it does not exist
. here default)
system properties such as themes and user preference,
just-in-time property determination mechanisms such as data binding and
animations/storyboards,
multiple-use templates such as resources and styles, or
values known through parent-child relationships with other elements in t
he element tree
self-contained validation,
default values,
callbacks that monitor changes to other properties, and
a system that can coerce property values based on potentially runtime in
formation
- The registered handle of a DP is calld DP Identifier, this can be used a para
meter to interact with WPF property system
- DP indentifier is stored as static member of the class
- We also get a CLR wrapper for DP, so rest of the system does not know about i
t.
- WPF property system have have precedence to follow, a local value has the hig
hest precedence except a running animation.
- In order, Property system coercion
- Active animations,
- Local value.
- TemplatedParent template properties.
- triggers
- property sets
- Implicit styles
- style triggers
- Template triggers
- Style setters
- Default Theme styles
- Inherited properties
- Default values from DP Metadata.
-** Dynamic resources and binding operations sets the precedence of the
location they are set.
- Clearvalue will on DP only clears the local value, so that Property system pr
ecedence can come into the play.

- DPs allows various call backs to the implementing class


- validation: to validate the value being set. callbacks are of signatur
e [bool]CallBack(object value)
- Coercion : call backs for coercion, when the values of a property is a
ffected by the values of other related properties.
- a DP can be of collection type, but changes to subproperties are not notified
in this case. To still notify changes of the subproperties uses FreezableCollec
tion<T> as collection type.
------------------------------------------------------------------An element has a TemplatedParent if it was created as part of a template (a Cont
rolTemplate or DataTemplate).
The properties applied for TP does not come in effect in standard markup, as TPs
only come live when at runtime their objects are created
-------------------------------------Visual:
- extremely light-weight,
- Visual object is a core WPF object, whose primary role is to provide renderin
g support- Visual is really the entry point to the WPF composition system
- supports: output display, clipping, bounding, hit testing, transformation etc
.
-----------------------------------------UIElement:
- defines core subsystems including Layout, Input, and Events.
- support layout using Measure and Arrange passes.
- Measure: Allows each component to measure its own size.
- Arrange: Allows parent to position and decide final size of each compo
nent.
- UIElement supports commandbinding
----------------------------------------------------------FrameworkElement
- FrameworkElement introduced policies and customizations on the subsystems.
- Idea of this is to provide powerful features exposed through a consistent set
of property driven layout semantics.
- It introduces properties of layout, animation, DataBinding, Styles
--------------------------------------------------------------------------System.Windows.Controls.Control
- Most significant feature of templating.
- Look less.
- Provides templating.
- provides forground, background, padding etc properties.
- Control povides
- Data Model: properties
- Interaction model: commands and events
- display model: templates
--------------------------------------------------------------------------Exceptions in WPF application
- AppDomain.UnhandledException From all threads in the AppDomain.
- Dispatcher.UnhandledException From a single specific UI dispatcher thread.
- Application.DispatcherUnhandledException From the main UI dispatcher thread i
n your WPF application.
- TaskScheduler.UnobservedTaskException from within each AppDomain that uses a
task scheduler for asynchronous operations.
Winforms
- Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptio
nFunction);
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHand
ler(UnhandledExceptionFunction);
-----------------------------------------------------------------------------

WPF best practices:


- MVVM
- ModelFirst
- Datatemplates: Themes\Generic.xaml , shared in whole app, used by prism engin
e
- Resources: resource dictionaries for separate items and use MergeDictionary t
o merger them into single file.
- Templates:
- Generalise at the top, localise at the bottom.
- Use projects folders
- Organize resources by types,
- It there is a Company wide, need of those resources one can create a separate
project as well.
- Leverage on local -> global precedence mode properly so overriding becomes ea
sy.
- Keep validating ur structure. Keep xaml files workable in blend
- Wireup using app.xaml, 3 or more level deep put a dummry default so solve a b
ug.
------------------------------------------------------------------------WPF and Winforms Mix
- ElementHost:
- to Host WPF control on a WinForms control.
- ElementHost.Child and Controls.Add(child) must be done to add the wpf
control.
- WinFormsHost:
- to host WinForms control in WPF
- WinFormHost.Child = WFControl must be done.
- PropertyMap can be used as a store to exchange property values between 2 cont
rols.
- Some properties are readily available in WinFormsHost.PropertyMap, one can ad
d/remove custom properties as well.
- PropertyTranslator can be used as a method to translate between 2 different t
ypes of values.

Vous aimerez peut-être aussi