1. .NET 4.5 Infoday, Graz, November 8th 2012
Rainer Stropek
software architects gmbh
WPF Mail
Web
Twitter
rainer@timecockpit.com
http://www.timecockpit.com
@rstropek
What’s New in .NET 4.5?
Saves the day.
2. Overview
Ribbon
Performance improvements
Data Binding enhancements
Live shaping
Binding to static properties
Delay changes to data source
Better support for async programming
Access to collections in non-UI threads
INotifyDataErrorInfo
API enhancements for Dispatcher
For a complete list of all enhancements see MSDN
3. .NET Infoday, Graz
Ribbon
System.Windows.Controls.Ribbon
(MSDN Link)
Use Ribbon for WPF for previous
versions of .NET
(MS Download Link)
Ribbon
Built-in ribbon in .NET 4.5
7. .NET Infoday, Graz
…
<CollectionViewSource x:Key="LiveShapingViewSource"
Live Shaping
IsLiveSortingRequested="True">
<CollectionViewSource.SortDescriptions> Data in grouped/sorted/filtered
<scm:SortDescription PropertyName="StockValue„
lists is automatically rearranged.
Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
…
<DataGrid ItemsSource="{Binding Source=
{StaticResource LiveShapingViewSource}}" />
…
8. .NET Infoday, Graz
<TextBlock
Text="{Binding Path=(local:MainWindow.CurrentTickValue)}" />
Data Binding
…
private static int currentTickValue; Problem: Change notification for
public static int CurrentTickValue {
static properties.
INotifyPropertyChanged is
get {
no option
return currentTickValue;
} Solution in .NET 4.5: Add a static
set { event handler
currentTickValue = value;
if (CurrentTickValueChanged != null) {
CurrentTickValueChanged(null, new EventArgs());
}
}
}
public static event EventHandler CurrentTickValueChanged;
…
9. .NET Infoday, Graz
… Data Binding
<Slider Minimum="0" Maximum="100"
x:Name="Slider" Width="300“ Use new Delay property to delay
refresh of binding source
Value="{Binding ElementName=SliderValue,
Path=Text,
Delay=1000,
Mode=OneWayToSource}"/>
<TextBlock x:Name="SliderValue" />
…
10. .NET Infoday, Graz
Async
Example: Retrieve data from
database or webservice in a
background thread
Access Collections in Background
Threads
Problem: Access only allowed in UI thread
11. .NET Infoday, Graz
private readonly ObservableCollection<string> strings =
new ObservableCollection<string>();
Async
private readonly object stringsSyncObject = new object();
… Enable modification of collections
in background threads using
BindingOperations.EnableCollectionSynchronization(
EnableCollectionSynchronization
this.strings, this.stringsSyncObject);
…
var timer = new Timer(1000);
timer.Start();
timer.Elapsed += (___, ____) =>
{
this.strings.Add(DateTime.Now.Ticks.ToString());
};
12. .NET Infoday, Graz
this.Dispatcher.InvokeAsync( Async
() => /* manipulate UI here */);
New TPL-compatible API for WPF
Dispatcher
this.Dispatcher.BeginInvoke(
new Action(() => /* manipulate UI here */));
13. .NET Infoday, Graz
<TextBox Text="{Binding Path=ValidationText}" />
…
Async
public class MyViewModel : …, INotifyDataErrorInfo {
private async void ValidateAsync() {
INotifyDataErrorInfo
var valid = await DoAsyncValidation();
enables async validation of data
if (!valid) { Use this mechanism to keep UI
if (this.ErrorsChanged != null) { responsive
this.ErrorsChanged(this,
new DataErrorsChangedEventArgs("ValidationText"));
}
}
}
public event EventHandler<DataErrorsChangedEventArgs>
ErrorsChanged;
public IEnumerable GetErrors(string propertyName) {…}
public bool HasErrors {…}
}
14. .NET 4.5 Infoday, Graz, November 8th 2012
Rainer Stropek
software architects gmbh
Mail rainer@timecockpit.com
Q&A Web
Twitter
http://www.timecockpit.com
@rstropek
Thank You For Coming.
Saves the day.