Since ReactiveUI’s ReactiveList<T> has recently been deprecated, I’ve been moving to DynamicData.
One feature I used from ReactiveList was item change tracking. It might look something like this:
var myList = new ReactiveList<MyClass>();
myList.ChangeTrackingEnabled = true;
myList.ItemChanged
.Where(x => x.PropertyName == nameof(MyClass.SomeProperty))
.Select(x => x.Sender)
.Subscribe(myObject =>
{
// Do some stuff
});
In DynamicData it goes like this:
var myList = new SourceList<MyClass>();
myList
.Connect()
.WhenPropertyChanged(myObject => myObject.SomeProperty)
.Subscribe(propValue =>
{
MyClass myObject = propValue.Sender;
string newValue = propValue.Value;
// Do some stuff
});
There is also WhenValueChanged, which just gives you the new property value as a straight IObservable:
myList
.Connect()
.WhenValueChanged(myObject => myObject.SomeProperty)
.Subscribe(newValue =>
{
// Do some stuff
});
The most powerful is WhenAnyPropertyChanged, which can tell you when any of the properties changed:
var myList = new SourceList<MyClass>();
myList
.Connect()
.WhenAnyPropertyChanged()
.Subscribe(myObject =>
{
// Do stuff with myObject
});
Or when any set of specified properties has changed:
var myList = new SourceList<MyClass>();
myList
.Connect()
.WhenAnyPropertyChanged(nameof(MyClass.FirstProperty), nameof(MyClass.SecondProperty))
.Subscribe(myObject =>
{
// Do stuff with myObject
});
My project is MVVM and I had been using ReactiveList<T> as my go-to observable collection for viewmodel properties. But ReactiveUI deprecated ReactiveList in 8.6.1. So I needed to get on to the new recommended library: DynamicData.
But there is no direct drop-in replacement you can do like ObservableCollection<T> <-> ReactiveList<T>. I grabbed the NuGet package, dropped in SourceList<T>, but that doesn’t work. SourceList<T> does not implement INotifyCollectionChanged, which is what WPF looks for when binding to ListView, GridView, ItemsControl, etc. So how do we bind to this thing? After some fruitless searches I dug through the source code unit tests and found the answer. Add this to the viewmodel class:
private readonly SourceList<string> myList = new SourceList<string>();
public IObservableCollection<string> MyListBindable { get; } = new ObservableCollectionExtended<string>();
It’s a two-stage affair. The SourceList<T> is where all the operations happen. Adds, clears, deletes and all that. But you don’t directly observe the source list. To do that, you need to call Connect() to get an IObservable<IChangeSet<T>> . If you follow this observable you can see everything that’s changed about the collection by looking at the change sets.
But you still need to do one more thing: get it in a format that the WPF UI can bind to, that is an object that implements INotifyCollectionChanged. ObservableCollectionExtended is DynamicData’s implementation of that. The Bind() call “slaves” it to the SourceList. Any changes made in the SourceList are now reflected in the ObservableCollectionExtended. A final call to Subscribe() activates it. Now since we’re exposing it as a property, we can bind our UI to it:
The important thing here is to remember that this property is here ONLY so the UI can bind to it. That’s why I’ve named it Bindable: so I’ll realize I’m doing something wrong if I try to modify it directly.
The ObservableCollectionExtended property also allows you to get batches to avoid update churn from making individual edits to the collection. When you call Edit() on the SourceList<T>:
this.myList.Edit(innerList =>
{
innerList.Clear();
foreach (string name in names)
{
innerList.Add(name);
}
});
It will batch up all the changes made into one event under INotifyCollectionChanged, meaning the UI only needs to react once.
In this guide I will walk through how to get the .NET framework to download and install on-the-fly in an Inno Setup installer.
It works in 3 steps:
Detect if the desired .NET framework is installed
Download the .NET Framework bootstrap installer with Inno Download Plugin
Run the bootstrap installer in quiet mode, which will download and install the .NET Framework. This is better than downloading the full installer since it only downloads the files it needs for your platform.
Here’s the full code:
#include <idp.iss>
// Other parts of installer file go here
[CustomMessages]
IDP_DownloadFailed=Download of .NET Framework 4.7.2 failed. .NET Framework 4.7 is required to run VidCoder.
IDP_RetryCancel=Click 'Retry' to try downloading the files again, or click 'Cancel' to terminate setup.
InstallingDotNetFramework=Installing .NET Framework 4.7.2. This might take a few minutes...
DotNetFrameworkFailedToLaunch=Failed to launch .NET Framework Installer with error "%1". Please fix the error then run this installer again.
DotNetFrameworkFailed1602=.NET Framework installation was cancelled. This installation can continue, but be aware that this application may not run unless the .NET Framework installation is completed successfully.
DotNetFrameworkFailed1603=A fatal error occurred while installing the .NET Framework. Please fix the error, then run the installer again.
DotNetFrameworkFailed5100=Your computer does not meet the requirements of the .NET Framework. Please consult the documentation.
DotNetFrameworkFailedOther=The .NET Framework installer exited with an unexpected status code "%1". Please review any other messages shown by the installer to determine whether the installation completed successfully, and abort this installation and fix the problem if it did not.
[Code]
var
requiresRestart: boolean;
function NetFrameworkIsMissing(): Boolean;
var
bSuccess: Boolean;
regVersion: Cardinal;
begin
Result := True;
bSuccess := RegQueryDWordValue(HKLM, 'Software\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', regVersion);
if (True = bSuccess) and (regVersion >= 461308) then begin
Result := False;
end;
end;
procedure InitializeWizard;
begin
if NetFrameworkIsMissing() then
begin
idpAddFile('http://go.microsoft.com/fwlink/?LinkId=863262', ExpandConstant('{tmp}\NetFrameworkInstaller.exe'));
idpDownloadAfter(wpReady);
end;
end;
function InstallFramework(): String;
var
StatusText: string;
ResultCode: Integer;
begin
StatusText := WizardForm.StatusLabel.Caption;
WizardForm.StatusLabel.Caption := CustomMessage('InstallingDotNetFramework');
WizardForm.ProgressGauge.Style := npbstMarquee;
try
if not Exec(ExpandConstant('{tmp}\NetFrameworkInstaller.exe'), '/passive /norestart /showrmui /showfinalerror', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
begin
Result := FmtMessage(CustomMessage('DotNetFrameworkFailedToLaunch'), [SysErrorMessage(resultCode)]);
end
else
begin
// See https://msdn.microsoft.com/en-us/library/ee942965(v=vs.110).aspx#return_codes
case resultCode of
0: begin
// Successful
end;
1602 : begin
MsgBox(CustomMessage('DotNetFrameworkFailed1602'), mbInformation, MB_OK);
end;
1603: begin
Result := CustomMessage('DotNetFrameworkFailed1603');
end;
1641: begin
requiresRestart := True;
end;
3010: begin
requiresRestart := True;
end;
5100: begin
Result := CustomMessage('DotNetFrameworkFailed5100');
end;
else begin
MsgBox(FmtMessage(CustomMessage('DotNetFrameworkFailedOther'), [IntToStr(resultCode)]), mbError, MB_OK);
end;
end;
end;
finally
WizardForm.StatusLabel.Caption := StatusText;
WizardForm.ProgressGauge.Style := npbstNormal;
DeleteFile(ExpandConstant('{tmp}\NetFrameworkInstaller.exe'));
end;
end;
function PrepareToInstall(var NeedsRestart: Boolean): String;
begin
// 'NeedsRestart' only has an effect if we return a non-empty string, thus aborting the installation.
// If the installers indicate that they want a restart, this should be done at the end of installation.
// Therefore we set the global 'restartRequired' if a restart is needed, and return this from NeedRestart()
if NetFrameworkIsMissing() then
begin
Result := InstallFramework();
end;
end;
function NeedRestart(): Boolean;
begin
Result := requiresRestart;
end;
Detecting if the desired .NET Framework is installed
I wrote mine to check for .NET 4.7. (See the NetFrameworkIsMissing method)
Downloading the bootstrapper
Next we need to find out where to download the installer from. The .NET Framework Deployment Guide for Developers has a great list of stable download links for the bootstrapper (web) installers. I picked 4.7.2, as it still supports code targeting .NET 4.7, and we might as well give the users the latest we can. This link should prompt you to download an .exe file directly; if it’s bringing you to a download webpage, it won’t work.
Now install Inno Download Plugin. This will make the idpAddFile and idpDownloadAfter calls inside InitializeWizard work.
This “InitializeWizard” method is a special one that InnoSetup calls when first setting up its wizard pages. We call our helper function to detect if the framework is installed, then schedule a download step after the “Ready to install” page. We include our direct download link determined earlier, and save it in a temp folder, with a file name of “NetFrameworkInstaller.exe”. This name I picked arbitrarily; we just need to refer to it later when we’re installing and cleaning up.
Installing the bootstrapper
Our code is activated on PrepareToInstall. When this function returns a string, that string is shown as an error message that stops the install from happening. We call into InstallFramework and return its result.
Inside InstallFramework we’re running the bootstrapper we downloaded earlier, with a flag to make the install passive (non interactive). The /showrmui option prompts the user to close applications to avoid a system restart. /showfinalerror tells the installer to show an error message if the install fails. Our main installer will show this screen while the framework is getting downloaded and installed:
Then the .NET installer UI will show alongside the first window:
If you’d like to keep it “cleaner” and just show the first screen you can swap out the /passive argument for /q. However I like showing the user the .NET install progress since it can take a long time and it reassures them that work is still really happening.
After running the installer, the bootstrapper is deleted (whether or not the install succeeded).
And now your installer is done!
Testing the installer
To test a .NET 4 or 4.7 install, I’d recommend setting up a Windows 7 virtual machine in Hyper-V. Windows 7 comes with .NET 2, 3 and 3.5 out of the box, but not .NET 4 or 4.7. After it installs the framework you can go to Programs and Features to cleanly remove it and test it all over again.
To test an earlier .NET version you should just be able to use a modern OS like Windows 8.1 or 10.
Thanks to Antony Male for suggesting updates to the error handling code.
It becomes necessary to deal with dates and times in most .NET programs. A lot of programs use DateTime but that structure is frought with potential issues when you start serializing, parsing, comparing and displaying dates from different time zones and cultures. In this post I will go over these issues and the APIs and practices you should use to avoid them.
Background
The DateTime structure stores only two pieces of information: Ticks and Kind. 1 tick is 100 nanoseconds (10,000 ticks in a millisecond). The ticks counter represents how many 100ns ticks you are away from 1/1/0001 12:00 AM. It’s the part that determines that it’s April 6th, 2012, 3:32:07 PM.
Another bit of data is the Kind: represented as a DateTimeKind enumeration. Utc, Local and Unspecified are the possible values. Utc means that the ticks counter represents a Coordinated Universal Time (that doesn’t change due to daylight savings or time zones). Local means that it represents the local time of whatever time zone the computer is set to. This one is sensitive to daylight savings. Note that a timezone offset is not built in to the DateTime structure. It can only get back to a real UTC time by checking the current time zone settings on the computer. ToUniversalTime() and ToLocalTime() will do these conversions. It will return a new DateTime value with the Ticks adjusted to get the correct year/month/day and with the requested type. Calling ToUniversalTime() on a UTC DateTime or ToLocalTime() on a Local DateTime has no effect. Remember that these functions are generating a new DateTime value and not modifying the original.
There’s also a third type: Unspecified. This means we don’t know whether it’s local or UTC. All we know is the year, month, day, etc. When working with DateTime values, Unspecified is not very helpful since we don’t know what to do when we want to get a local or UTC time from it. If you call ToUniversalTime() on one of these, it assumes that the type must have been Local and converts based on that. If you call ToLocalTime() it assumes that the type must have been UTC and converts in that direction.
DateTimeOffset is a newer structure. It is also based on ticks but instead of storing a Kind, it keeps track of the offset from UTC as a TimeSpan. A DateTimeOffset always knows what time zone it’s in. Calling ToUniversalTime() will always result in a TimeSpan.Zero offset, and ToLocalTime() will convert and result in an offset of the user’s current time zone.
Use DateTimeOffset, not DateTime
DateTime has countless traps in it that are likely to give your code bugs:
DateTime values with DateTimeKind.Unspecified are bad news. It’s common for code to call ToUniversalTime() or ToLocalTime() to make sure it’s got the date properly ready for storage or display respectively. If you call ToUniversalTime() it converts X hours in one direction, but if you call ToLocalTime() on it, it converts X hours in the opposite direction. Both of those can’t possibly be correct.
DateTime doesn’t care about UTC/Local when doing comparisons. It only cares about the number of Ticks on the objects. It could not care less that you’ve lovingly specified that value A is most certainly UTC and value B is local; it won’t do any conversions for you. 7:30 AM Local is always going to be evaluated as less than 8:22 AM UTC, even if your timezone is EDT and really the first time was 11:30 AM UTC.
DateTime values are not aware of standard format strings. For instance the “r” (RFC1123, the format used in HTTP headers) and “u” (universal sortable) formats both put UTC markers in the string you serialize to. If you call .ToString(“u”) on a local time, DateTime will happily label your local time as UTC without doing any conversion. The basically guarantees disaster when it gets parsed back in again.
Parsing a string that has a UTC marker with DateTime does not guarantee a UTC time. If you run DateTime.Parse(“2012-04-06 23:46:23Z”) you might expect the result to be a UTC DateTime since the “Z” UTC flag is right there in the string, but you do not. It will correctly note that it is dealing with a UTC time, then promptly convert it to a Local time. It’s still technically correct, but it can result in the hours count changing when you are not expecting it to.
But DateTimeOffset doesn’t have any of these problems! It’s the newer, more robust version; think of it as DateTime v2. They couldn’t fix the old DateTime object because that might break existing code that was relying on the bad behavior.
So you’ll want to use DateTimeOffset in any situation that you’re referring to a specific point in time. You might still use DateTime for things like general dates (July 4th, 1776) or store hours (9AM-5PM), since they are not affected by time zones.
Pick the right format when serializing
It’s common to convert DateTimeOffset values to strings for storage or sending them somewhere else. The list of standard date and time format strings is helpful when you are contemplating how to do this. These are used with the DateTimeOffset.ToString method: for example myDate.ToString(“u”). Note how with most of the standard formats you get different values for different cultures: different month names, swapped month/day places, etc. If you use one of these to serialize and Parse back in, it might work if you have the same culture on both sides, but will break if one side uses a different culture. You can get failed parses or swapped month/day values. Only the “o”, “r”, “s” and “u” formats are culture-invariant. Use one of those for serialization:
Not a great format for this use as it has no timezone or UTC marker and can change based on what offset you have
Conclusion
Use DateTimeOffset and be careful with serialization. If you’re dealing with legacy code that’s using DateTime, be aware of all the timezone-related pitfalls when comparing, serializing, parsing and converting.
One final issue…
If it is Coordinated Universal Time, why is the acronym UTC and not CUT? When the English and the French were getting together to work out the notation, the french wanted TUC, for Temps Universel Coordonné. UTC was a compromise: it fit equally badly for each language. 🙂
When writing an MVVM application, you want to separate from the UI. However you also need to make sure that UI updates happen on the UI thread. Changes made through INotifyPropertyChanged get automatically marshaled to the UI thread, so in most cases you’ll be fine. However, when using INotifyCollectionChanged (such as with an ObservableCollection), these changes are not marshaled to the UI thread.
This means that unless you modify your collection on the UI thread, you’ll get a cross threading error. Thus the problem. We’re in the ViewModel and we don’t have access to the Dispatcher. That’s on a View object. How do we update the collection on the UI thread?
I came up with a small static class to help with this:
When your ViewModel gives the DispatchService actions, it can run them on the correct thread. Which is, in the case of unit tests, always the current thread. A call to the DispatchService may look like this:
A common user interface tweak is to save the size and location of a window and restore that state when the program starts up again. That way, the users don’t need to re-do their work of resizing and moving the windows to where they want them. However I’ve found that there isn’t really a convenient way to do with in C#. Many solutions focus on recording the window width, height, x and y coordinates and current state (maximized, restored, etc), then applying all those values again on startup. But then you have to deal with edge cases: What if the user has changed resolutions since your program last ran? What if it was present on a monitor that’s now disconnected?
Fortunately, there are a couple of native Windows functions that can do all this work for us: SetWindowPlacement and GetWindowPlacement. MSDN has a sample demonstrating how to PInvoke to these functions in a WPF project, but it’s got a few issues with it. For one, it’s trying to store the placement as a custom type in settings, which can cause strange errors every time you open your Settings file. Here’s my take on it:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace WindowPlacementExample
{
// RECT structure required by WINDOWPLACEMENT structure
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left, int top, int right, int bottom)
{
this.Left = left;
this.Top = top;
this.Right = right;
this.Bottom = bottom;
}
}
// POINT structure required by WINDOWPLACEMENT structure
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
// WINDOWPLACEMENT stores the position, size, and state of a window
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINT minPosition;
public POINT maxPosition;
public RECT normalPosition;
}
public static class WindowPlacement
{
private static Encoding encoding = new UTF8Encoding();
private static XmlSerializer serializer = new XmlSerializer(typeof(WINDOWPLACEMENT));
[DllImport("user32.dll")]
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll")]
private static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
public static void SetPlacement(IntPtr windowHandle, string placementXml)
{
if (string.IsNullOrEmpty(placementXml))
{
return;
}
WINDOWPLACEMENT placement;
byte[] xmlBytes = encoding.GetBytes(placementXml);
try
{
using (MemoryStream memoryStream = new MemoryStream(xmlBytes))
{
placement = (WINDOWPLACEMENT)serializer.Deserialize(memoryStream);
}
placement.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
placement.flags = 0;
placement.showCmd = (placement.showCmd == SW_SHOWMINIMIZED ? SW_SHOWNORMAL : placement.showCmd);
SetWindowPlacement(windowHandle, ref placement);
}
catch (InvalidOperationException)
{
// Parsing placement XML failed. Fail silently.
}
}
public static string GetPlacement(IntPtr windowHandle)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
GetWindowPlacement(windowHandle, out placement);
using (MemoryStream memoryStream = new MemoryStream())
{
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8))
{
serializer.Serialize(xmlTextWriter, placement);
byte[] xmlBytes = memoryStream.ToArray();
return encoding.GetString(xmlBytes);
}
}
}
}
}
To get the placement of an existing window, you give it the native handle for the window, and get back a string of XML that represents the placement for the window. You save this string somewhere, then when the program starts back up again, apply it with SetPlacement. The above code works for both WinForms and WPF. And in both cases you can open your Properties/Settings.settings file and add a new setting to store the string:
This will store the window state locally, per-user. Of course you could also store the string wherever you wanted to.
What I like about this approach is that the WindowPlacement class does the XML serialization for you and gives you a nice, tidy string containing all the placement data.
But where in the program code should we hook in to save and re-apply the window placements? The answer is a bit different between WinForms and WPF.
WPF:
You might benefit by throwing a couple of extension methods on the WindowPlacement class to make things a bit easier:
This one is a bit more straightforward. Save on the FormClosing event and restore the window state in the Load event handler. The Form’s native handle is accessible from its Handle property.
Now we have a robust and relatively painless way of persisting window sizes and positions, and we only need to keep track of one string per window.