Skip to content

Commit f010aa4

Browse files
authored
0503 - Intro to WPF (#82)
* Added WPF samples * Fixed image reference * Added Databinding sample * Added image and update to README for Databinding demo
1 parent e27dd98 commit f010aa4

36 files changed

+870
-0
lines changed
Loading
22.8 KB
Loading
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Session 0503 - Introduction to WPF
2+
3+
In this session, we'll introduce the basics of the Windows Presentation Foundation (WPF) user-interface framework. WPF has been available to develop with since its initial release in 2006 as part of .NET Framework 3.0 and since then has been updated and released as [open-source](https://github.com/dotnet/wpf) starting with the release of .NET Core 3.0 and continuing through .NET 5 and .NET 6.
4+
5+
## What is WPF?
6+
7+
From the [official documentation](https://docs.microsoft.com/visualstudio/designers/getting-started-with-wpf):
8+
9+
> Windows Presentation Foundation (WPF) is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security. The framework is part of .NET, so if you have previously built applications with .NET using ASP.[]()NET or Windows Forms, the programming experience should be familiar. WPF uses the Extensible Application Markup Language (XAML) to provide a declarative model for application programming.
10+
11+
> WPF is resolution-independent and uses a vector-based rendering engine, built to take advantage of modern graphics hardware. WPF provides a comprehensive set of application-development features that include controls, data binding, layout, 2D and 3D graphics, animation, styles, templates, documents, media, text, and typography. WPF is part of .NET, so you can build applications that incorporate other elements of the .NET API.
12+
13+
## What is XAML?
14+
15+
Extensible Application Markup Language (XAML) is a declarative language that's based on XML. XAML is used in the following types of applications to build user interfaces:
16+
17+
- Windows Presentation Foundation (WPF) apps
18+
- Universal Windows Platform (UWP) apps
19+
- Xamarin.Forms apps
20+
- .NET MAUI apps _(starting with .NET 6)_
21+
22+
From the [official documentation](https://docs.microsoft.com/dotnet/desktop/wpf/xaml/):
23+
24+
> XAML is a declarative markup language. As applied to the .NET Core programming model, XAML simplifies creating a UI for a .NET Core app. You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files that are joined to the markup through partial class definitions. XAML directly represents the instantiation of objects in a specific set of backing types defined in assemblies. This is unlike most other markup languages, which are typically an interpreted language without such a direct tie to a backing type system. XAML enables a workflow where separate parties can work on the UI and the logic of an app, using potentially different tools.
25+
26+
> When represented as text, XAML files are XML files that generally have the .xaml extension. The files can be encoded by any XML encoding, but encoding as UTF-8 is typical.
27+
28+
> The following example shows how you might create a button as part of a UI. This example is intended to give you a flavor of how XAML represents common UI programming metaphors.
29+
30+
```xaml
31+
<StackPanel>
32+
<Button Content="Click Me"/>
33+
</StackPanel>
34+
```
35+
36+
### Markup and Code-Behind
37+
38+
WPF lets you develop an application using both markup and code-behind, an experience with which ASP.NET developers should be familiar. You generally use XAML markup to implement the appearance of an application while using managed programming languages (code-behind) to implement its behavior. This separation of appearance and behavior has the following benefits:
39+
40+
- Development and maintenance costs are reduced because appearance-specific markup isn't tightly coupled with behavior-specific code.
41+
42+
- Development is more efficient because designers can implement an application's appearance simultaneously with developers who are implementing the application's behavior.
43+
44+
- Globalization and localization for WPF applications is simplified.
45+
46+
## Hello World with WPF
47+
48+
![A messagebox saying 'Welcome WPF'](FirstApp.PNG)
49+
50+
Let's create our first application with a simple button that triggers a message box. We'll create a new WPF application with the command-line command:
51+
52+
```
53+
dotnet new wpf -o FirstApp
54+
```
55+
56+
Next, we'll add some content to the `MainWindow.xaml` to add a button that can be clicked:
57+
58+
```
59+
<Window x:Class="FirstApp.MainWindow"
60+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
61+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
62+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
63+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
64+
xmlns:local="clr-namespace:FirstApp"
65+
mc:Ignorable="d"
66+
Title="Window with Button" Height="100" Width="250">
67+
68+
<!-- The new button we added -->
69+
<Button Name="button" Click="button_Click">Click Me!</Button>
70+
71+
</Window>
72+
```
73+
74+
Next, we'll give the button some code to trigger by adding the `button_Click` method to the **Code Behind** in `MainWindow.xaml.cs`
75+
76+
```
77+
public void button_Click(object sender, RoutedEventArgs e) {
78+
79+
MessageBox.Show("Welcome to WPF!");
80+
81+
}
82+
```
83+
84+
Finally, we can run the app by clicking the Play button or pressing F5 in Visual Studio. At the command-line, we can instead execute the command `dotnet run` to start the application.
85+
86+
## Building your first application: the Names application
87+
88+
The Names application is a simple application walkthrough in the [official documentation](https://docs.microsoft.com/dotnet/desktop/wpf/get-started/create-app-visual-studio) and is available with some additional code TODOs in the `src/Names` folder.
89+
90+
## WPF Controls available
91+
92+
The built-in WPF controls are listed here:
93+
94+
- **Buttons:** Button and RepeatButton.
95+
96+
- **Data Display:** DataGrid, ListView, and TreeView.
97+
98+
- **Date Display and Selection:** Calendar and DatePicker.
99+
100+
- **Dialog Boxes:** OpenFileDialog, PrintDialog, and SaveFileDialog.
101+
102+
- **Digital Ink:** InkCanvas and InkPresenter.
103+
104+
- **Documents:** DocumentViewer, FlowDocumentPageViewer, FlowDocumentReader, FlowDocumentScrollViewer, and StickyNoteControl.
105+
106+
- **Input:** TextBox, RichTextBox, and PasswordBox.
107+
108+
- **Layout:** Border, BulletDecorator, Canvas, DockPanel, Expander, Grid, GridView, GridSplitter, GroupBox, Panel, ResizeGrip, Separator, ScrollBar, ScrollViewer, StackPanel, Thumb, Viewbox, VirtualizingStackPanel, Window, and WrapPanel.
109+
110+
- **Media:** Image, MediaElement, and SoundPlayerAction.
111+
112+
- **Menus:** ContextMenu, Menu, and ToolBar.
113+
114+
- **Navigation:** Frame, Hyperlink, Page, NavigationWindow, and TabControl.
115+
116+
- **Selection:** CheckBox, ComboBox, ListBox, RadioButton, and Slider.
117+
118+
- **User Information:** AccessText, Label, Popup, ProgressBar, StatusBar, TextBlock, and ToolTip.
119+
120+
## Working with a Web Service
121+
122+
![The VisualSearch sample, searching Bing for "csharpfritz"](visualsearch.png)
123+
124+
Let's walk through the image search sample from the Learn Module ["Write Internet Connected Windows Apps"](https://docs.microsoft.com/learn/modules/build-internet-connected-windows10-apps/1-building-app-that-uses-cloud-service?pivots=wpf). We'll write a simple application that searches Bing for an image and displays it in an image box.
125+
126+
## Databinding with Entity Framework Sample
127+
128+
![Parent-Child Databinding form sample showing two related grids](Databinding.PNG)
129+
130+
The Entity Framework team shared [a sample](https://docs.microsoft.com/ef/core/get-started/wpf) that shows how to bind to a simple Parent-Child form to a data source. This sample shows how to use Lazy Loading and CollectionViewSource objects to define and automatically bind a DataGrid to a database table
131+
132+
## Migrating from .NET Framework to .NET Core
133+
134+
WPF for .NET 5.0 provides new features and enhancements over .NET Framework. To learn how to migrate an app, see [How to migrate a WPF desktop app to .NET 5.](https://docs.microsoft.com/dotnet/desktop/wpf/migration/convert-project-from-net-framework)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net5.0-windows/Databinding.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach"
24+
}
25+
]
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/Databinding.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/Databinding.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/Databinding.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="Databinding.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Databinding"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace Databinding
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly:ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
4+
namespace Databinding
5+
{
6+
public class Category
7+
{
8+
public int CategoryId { get; set; }
9+
public string Name { get; set; }
10+
11+
public virtual ICollection<Product> Products { get; private set; } =
12+
new ObservableCollection<Product>();
13+
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net5.0-windows</TargetFramework>
6+
<UseWPF>true</UseWPF>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.8" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.8" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)