Vector Databases 101 - An introduction to the world of Vector Databases
WPF Deep Dive
1. WPF Deep Dive Aniruddha Chakrabarti Sr. Solution Architect “If I have six hours to chop down a tree, I will use four hours to sharpen the axe” – Abraham Lincoln
2. Key benefits of WPF Broad integration Provides access to normal 2D graphics, controls, 3D, video, speech and rich document viewing with consistent programming model as well as tight integration Hardware acceleration Improved Visual Rendering Retained mode graphics Vector graphics Resolution independence (Device Independent Graphics) Declarative programming (aka XAML) Rich composition, layout and customization Easy deployment Incorporates best of Web and Windows Integrate developers and designers
5. WPF: Assemblies and Namespaces Primary Assemblies containing the WPF API PresentationFramework PresentationCore WindowsBase milcore (unmanaged) mil: Media Integration Layer Namespaces System.Windows Other child namespaces starting with System.Windows except System.Windows.Forms (Win Forms) System.Windows.Controls System.Windows.Data System.Windows.Documents System.Windows.Media (+ child namespaces) System.Windows.Shapes Managed Unmanaged/Native
6. WPF Base Class Hierarchy WindowsBase PresentationFramework PresentationCore
7.
8. Provides basic constructs for dealing with concurrency and threading. WPF is based on a messaging system implemented by the dispatcher - works much like familiar Win32 message pump
12. Provides for building a tree of visual objects. Visual is designed to be extremely lightweight and flexible, so most of the features have no public API exposure.
13. Visual is really the entry point to the WPF composition system. Visual is the point of connection between these two subsystems, the managed API and the unmanaged milcore.
30. XAML Spec defines rules that map .NET namespaces, types, properties, and events into XML namespaces, elements, and attributes.
31. Declaring an XML element in XAML (known as an object element) is equivalent to instantiating the corresponding .NET object (always via a default constructor).
37. Mapping defined in PresentationFramework.dll assembly [assembly:XmlnsDefinition (“http://schemas.microsoft.com/winfx/2006/xaml/presentation”, “System.Windows.Controls”)]
38. Root object element in a XAML file must specify at least one XML namespace: used to qualify itself & any child elements.
39. Additional XML namespaces (on the root or on children) can be declared , but each one must be given a distinct prefix to be used on any identifiers from that namespace.
71. Order of Window events1. Constructor is called. 2. Window.Initialized event is raised. 3. Window.Activated event is raised.13 4. Window.Loaded event is raised. 5. Window.ContentRendered event is raised. 6. User interacts with the window. 7. Window.Closing event is raised. 8. Window.Unloaded event is raised. 9. Window.Closed event is raised.
76. Journal: responsible for tracking the navigation actions<Label>This is page 1</Label> <TextBlock> <HyperlinkNavigateUri="Page2.xaml">Go to Page2 </Hyperlink> </TextBlock>
121. Default unit of measurement in WPF is Device Independent Pixel / Logical Pixel
122. NOT in Pixel (as in WinForms) as it’s resolution dependant.
123.
124. No as it can parse NaN & Infinity but NOT in, cm, pt strings
125.
126. Converter Example System.ComponentModel namespace provides a base class called TypeConverter. All Converter classes ultimately derives from TypeConverter. .NET Framework TypeConverter Supported in: 3.5, 3.0 SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0
127.
128. Padding - Controls how much extra space gets placed around the inside edges of the element
129. Visibility – NOT boolean, rather a three-state System.Windows.Visibility enumeration
132. Hidden: The element is invisible yet still participates in layout.<StackPanel Background="LightBlue" Margin="40,30,20,10"> <Button Margin="50" Padding="20">Hello World</Button> </StackPanel>
133. Position Alignment HorizontalAlignment : Left, Center, Right, Stretch (default) VerticalAlignment: Top, Center, Bottom, Stretch (default) <StackPanel> <Button HorizontalAlignment="Left">Left Button</Button> <Button HorizontalAlignment="Center">Center Button</Button> <Button HorizontalAlignment="Right">Right Button</Button> <Button HorizontalAlignment="Stretch">Stretch Button</Button> </StackPanel>
148. ItemHeight, ItemWidth – uniform height & width can be set for all children (controls)<WrapPanel Orientation="Horizontal"> <Button MinWidth="150">Button 1</Button> <Button MinWidth="120">Button 2</Button> <Button MinWidth="100">Button 3</Button> <Button MinWidth="80">Button 4</Button> <Button MinWidth="170">Button 5</Button> <Button MinWidth="40">Button 6</Button> </WrapPanel>
149. Dock Panel Wraps its children (controls) to additional rows or columns when there’s not enough space <DockPanel> <Button DockPanel.Dock="Top">Button 1</Button> <Button DockPanel.Dock="Left">Button 2</Button> <Button DockPanel.Dock="Bottom">Button 3</Button> <Button DockPanel.Dock="Right">Button 4</Button> <Button>Center Button</Button> </DockPanel>
153. Similar to working with Table in HTML<Grid ShowGridLines="True"> <Grid.RowDefinitions> <RowDefinition Height="40" /> <RowDefinition Height="40" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="120"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Label Margin="7">First Name:</Label> <TextBox Grid.Column="1" Margin="7"></TextBox> <Label Margin="7" Grid.Row="1">Middle Name:</Label> <TextBox Grid.Row="1" Grid.Column="1" Margin="7"></TextBox> <Label Margin="7" Grid.Row="2">Last Name:</Label> <TextBox Grid.Row="2" Grid.Column="1" Margin="7"></TextBox> </Grid>
154. UniformGrid Special type of Grid Provides same height & width to all children (controls) <UniformGrid> <Button>Button 1</Button> <Button>Button 2</Button> <Button>Button 3</Button> <Button>Button 4</Button> <Button>Button 5</Button> <Button>Button 6</Button> </UniformGrid>
161. Resource Different from traditional .NET Resources which are binary resources (strings, images, icons etc) WPF Resources are Logical Resources. A Resource can be anything that should be reused Resources are stored in an object of type ResourceDisctionary. Each resource in resource dictionary must have unique key Three fundamental classes (FrameworkElement, FrameworkContentElement and Application) define a property named Resource of type ResourceDisctionary. Any element derived from FrameworkElement (such as panels, controls) can have a Resource collection.
169. Can be stored at different level (like individual control, panels, window or application level)
170. The markup extension class implements the ability to walk the logical tree to find the item. Element hierarchy Application.Resources Type theme System theme
173. Static vs. Dynamic Resource WPF provides two ways to access a logical resource: Statically with StaticResource, meaning the resource is applied only once (the first time it’s needed) Dynamically with DynamicResource, meaning the resource is reapplied every time it changes
174. System Resource WPF provides out of the box System Resources: System settings encapsulated by static fields on three classes in the System.Windows namespace: SystemColors, SystemFonts, and SystemParameters <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="System Resources Demo" Height="300" Width="300" Background="{x:StaticSystemColors.ActiveCaptionBrush}"> <StackPanel> <LabelFontFamily="{x:StaticSystemFonts.MessageFontFamily}">User:</Label> <TextBox>Aniruddha</TextBox> </StackPanel> </Window>
181. TargetType can be set to a specific type of controls like Button, Checkbox etc so that the Style is applied to those controls only.<Window.Resources> <Style x:Key="SpecialButton"TargetType="{x:Type Button}"> <Setter Property="Control.Background" Value="LightSlateGray"></Setter> <Setter Property="Control.FontSize" Value="18.5"></Setter> <Setter Property="Control.Foreground" Value="DarkBlue"></Setter> <Setter Property="Control.FontFamily" Value="Verdana"></Setter> </Style> </Window.Resources> <StackPanel> <Button Style="{StaticResourceSpecialButton}">Special Button</Button> <CheckBoxStyle="{StaticResourceSpecialButton}">Special Checkbox</CheckBox> </StackPanel>
187. FrameworkElement, Style, DataTemplate & ControlTemplate all have a Triggers collection, but whereas Style and the template classes accept all three types, FrameworkElement only accepts event triggers.
188.
189.
190. When the property no longer has this value, the property trigger “undoes” the Setters.<Window.Resources> <Style x:Key="magicButton" TargetType="Button"> <Setter Property="FontSize" Value="18"></Setter> <Setter Property="Foreground" Value="Green"></Setter> <Setter Property="Background" Value="LightBlue"></Setter> <Setter Property="FontStyle" Value="Italic"></Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="FontSize" Value="26"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Setter Property="Background" Value="Yellow"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <Button Style="{StaticResource magicButton}">Button 1</Button> <Button Style="{StaticResource magicButton}">Button 2</Button> </StackPanel>
191.
192.
193.
194. Graphics Overview Provides a new rendering and composition model – Enables to use advanced display techniques Takes advantage of the capabilities of modern graphics cards Retained-mode Graphics High Quality Rendering Painter’s Algorithm Retained-mode Graphics – no repainting All Shapes (Rectangle, Ellipse) are Vector based. Vector images do not get distorted when they are enlarged. Vector graphics formats work by specifying the primitive shapes that make up the picture, and describing their positions using coordinates (or "vectors").
195.
196. This is because GDI's graphics model is primarily a "pixel painter"—once a primitive like ellipse is rendered, GDI "forgets" the primitive.
197. The application must retain the state (geometry, formatting attributes, etc.) of all these graphic objects in order to repaint.
198. GDI programmers never have to worry about maintaining the images of user-interface controls because controls are retained-mode graphics—they are objects stored in the underlying graphics system
199. The burden of maintaining the image of the control is borne by the graphics platform itself, not by the app.
200. Retained-mode graphics is not new to GDI/Win32. In WPF ALLgraphic objects are retained. In addition to the UI controls and media primitives, 2-D Shape primitives (rectangles, ellipses, paths, etc.) and 3-D modeling primitives are retained objects.Paint (WM_PAINT) Not handled
204. Higher performance in drawing and refreshing, due to WPF's better use of the underlying graphics hardware WPF WinForm/GDI/GDI+
205.
206. This system works very well in memory constrained systems because when something changes you only have to touch the affected component – no two components ever contribute to the color of a single pixel.
207. WPF uses "painter's algorithm" painting model - Instead of clipping each component, each component is asked to render from the back to the front of the display. This allows each component to paint over the previous component's display.
208.
209. Brushes Overview WPF provides two types of brushes Three Color Brush SolidColorBrush , LinearGradientBrush, RadialGradientBrush Three Tile Brush Visual Brush, Drawing Brush, Image Brush
215. Pen A Pen is a brush with a thickness Important Pen properties StartLineCap and EndLineCap Line Join DashStyle <Line X1="20" Y1="40" X2="250" Y2="40" Stroke="Red" StrokeThickness="10" StrokeDashArray="1" StrokeDashCap="Flat" StrokeStartLineCap="Triangle" StrokeEndLineCap="Round"> </Line>
249. Trees in WPF Logical Tree or Element Tree Visual Tree <DockPanel> <ListBox DockPanel.Dock="Top"> <ListBoxItem>Dog</ListBoxItem> <ListBoxItem>Cat</ListBoxItem> <ListBoxItem>Fish</ListBoxItem> </ListBox> <Button Height="20" Width="100" DockPanel.Dock="Top">Buy a Pet</Button> </DockPanel> Logical Tree
259. a Path to the value in the binding source to use.
260. e.g if you want to bind the content of a TextBox to the Name property of an Employee object, your target object is the TextBox, the target property is the Text property, the value to use is Name, and the source object is the Employee object.
261.
262. If binding has correct settings & data provides proper notifications, then, when the data changes its value, elements bound to data reflect changes automatically.<Label>Enter your name:</Label> <TextBox x:Name="nameTextBox"></TextBox> <Label Margin="0,20,0,0">Name Entered by you:</Label> <Label x:Name="label" Content="{BindingElementName=nameTextBox, Path=Text}"></Label> Binding binding = new Binding(); // Set the Source object binding.Source = nameTextBox; // Set the Source property binding.Path =new PropertyPath("Text"); // Attach to Target property label.SetBinding(Label.ContentProperty, binding);
266. ElementName and Path are separated by ,<Label>Enter your name:</Label> <TextBox x:Name="nameTextBox"></TextBox> <Label Margin="0,20,0,0">Name Entered by you:</Label> <Label Content="{BindingElementName=nameTextBox,Path=Text}"> </Label> <Label Content="{BindingText,ElementName=nameTextBox}"> </Label> Alternate Property Element Syntax <Label>Enter your name:</Label> <TextBox x:Name="nameTextBox"></TextBox> <Label Margin="0,20,0,0">Name Entered by you:</Label> <Label x:Name="label"> <Label.Content> <BindingElementName="nameTextBox" Path="Text"></Binding> </Label.Content> </Label>
267.
268. OneWay: Changes to source property automatically update the target property, but changes to target property are not propagated back to source property.
269. TwoWay: Changes to either source property or target property to automatically update the other. This is default.
274. Explicit: When the application calls UpdateSource.<Label>Enter your name:</Label> <TextBox x:Name="nameTextBox"></TextBox> <Label Margin="0,20,0,0">Name Entered by you:</Label> <TextBox x:Name="label" Text="{BindingElementName=nameTextBox, Path=Text,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
275.
276. The property is of type RelativeSource, which also happens to be a markup extension.
277.
278. Binding to .NET primitive data public class PatientManager { public string PatientName { get; set;} public PatientManager() { PatientName ="John Smith"; } } <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:p="clr-namespace:WpfApplication1" Title="Data Binding - .NET primitive data" Height="300" Width="300"> <Window.Resources> <p:PatientManager x:Key="PatientSource"></p:PatientManager> </Window.Resources> <StackPanel> <Label>Patient Name:</Label> <TextBox Text="{Binding Source={StaticResourcePatientSource}, Path=PatientName}"></TextBox> </StackPanel> </Window>
279. Data Context public class PatientManager { public string PatientName { get; set; } public DateTime DateOfBirth {get; set; } public string Address { get; set; } public string City { get; set; } public PatientManager() { PatientName ="John Smith"; DateOfBirth =DateTime.Now; Address ="C.V.Raman Nagar"; City = "Bangalore"; } } <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:p="clr-namespace:WpfApplication1" Title="Data Binding - DataContext" Height="300" Width="300"> <Window.Resources> <p:PatientManager x:Key="PatientSource"></p:PatientManager> </Window.Resources> <StackPanel> <StackPanel.DataContext> <Binding Source="{StaticResourcePatientSource}"></Binding> </StackPanel.DataContext> <Label>Patient Name:</Label> <TextBox Text="{Binding Path=PatientName}" /> <Label>Date of Birth:</Label> <TextBox Text="{Binding Path=DateOfBirth}" /> <Label>Address:</Label> <TextBox Text="{Binding Path=Address}" /> <Label>City:</Label> <TextBox Text="{Binding Path=City}" /> </StackPanel> </Window>
307. NOT tied with button. Code written against the Click event depends only on a component that can be clicked. Decoupling of code to the action produced allows for more flexibility in the implementation of handlers.
308. Events suffer from a form of coupling that requires the method implementation to be of a specific signature. e.g. the delegate for Button.Click is defined as follows:public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);
316. Custom Command Implement ICommand interface Execute: The method that executes the command specific logic CanExecute: A method returning true if the command is enabled or false if it is disabled CanExecuteChanged: An event that is raised whenever the value of CanExecute changes
320. When a routed event is raised, it can travel up or down the visual and logical tree, getting raised on each element in a simple and consistent fashion, without the need for any custom code.
321. Helps Apps remain oblivious to details of the visual tree & is crucial to success of element composition.
322. Routing strategies: the wayinwhich event raising travels through element tree - Tunneling: The event is first raised on the root, then on each element down the tree until the source element is reached (or until a handler halts the tunneling by marking the event as handled). Bubbling: The event is first raised on the source element, then on each element up the tree until the root is reached (or until a handler halts the bubbling by marking the event as handled). Direct: The event is only raised on source element. This is same behavior as plain .NET event, except that such events can still participate in mechanisms specific to routed events.
333. Doesn’trefer to traditional object oriented class based inheritance, but rather the flowing of property values down the element tree.this.FontSize = 20;
334. Dependency Property Example // Declare a public object of type DependencyProperty public static readonlyDependencyPropertySpaceProperty; // .NET Property Wrapper (Optional) public intSpace { get { return (int)GetValue(SpaceProperty); } set { SetValue(SpaceProperty, value); } } // Declare metadata such as default value of the DP FrameworkPropertyMetadatametadata =new FrameworkPropertyMetadata(); metadata.DefaultValue = 1; metadata.AffectsMeasure =true; metadata.Inherits = true; metadata.PropertyChangedCallback += OnSpacePropertyChanged; // Register the Dependency Property SpaceProperty = DependencyProperty.Register("Space",typeof(int),typeof(SpaceButton), metadata,ValidateSpaceButton); static void OnSpacePropertyChanged(DependencyObjectobj,DependencyPropertyChangedEventArgsargs) { }