A common topic I see on StackOverflow is how do I persist my window location in a WPF-friendly manner?
First things first, let's create some default values:
“The .NET Framework 2.0 allows you to create and access values that are persisted between application execution sessions.” – MSDN
Once we have some defaults, all we have to do is bind to them from our window! Sounds easy, huh?
First, include the default namespace…
xmlns:local="clr-namespace:OpenPOS.Properties"
And then bind to them:
<Window x:Class="OpenPOS.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:OpenPOS.Properties"
Height="{Binding Source={x:Static local:Settings.Default}, Path=Height, Mode=TwoWay}"
Width="{Binding Source={x:Static local:Settings.Default}, Path=Width, Mode=TwoWay}"
Left="{Binding Source={x:Static local:Settings.Default}, Path=Left, Mode=TwoWay}"
Top="{Binding Source={x:Static local:Settings.Default}, Path=Top, Mode=TwoWay}"
WindowState="{Binding Source={x:Static local:Settings.Default}, Path=WindowState, Mode=TwoWay}">
</Window>
and that’s it!
Another common question I see often is how do I handle dialog boxes, etc? The easy answer to this is just call MessageBox.Show
, ShowDialog
, etc! and it will work in simple applications but what happens if you need to test this? Now you depend on a Dialog box that pops up or some user interaction! In OpenPOS, I create services for these kind of interactions! I created a VERY simple NotifyService
for sending notifications:
public interface INotifyService
{
void Notify(string message);
}
And this is how the notification looks:
BTW – this uses wpf-notifyicon (and here is a CodeProject article on how to use it).
For more information about OpenPOS…
And you can download ALL the source from CodePlex here.
CodeProject