ADD MVVM Sample.

NTFY-24

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@198 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2013-11-22 23:37:09 +00:00
parent 6ae24d3c23
commit e0f4d731ca
9 changed files with 180 additions and 6 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -0,0 +1,37 @@
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Samples.Tutorials.MvvmSample.ClockPopup"
x:Name="UserControl"
Height="141"
Width="304">
<UserControl.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#FFA8A8A8"
Offset="0" />
<GradientStop Color="White"
Offset="1" />
</LinearGradientBrush>
</UserControl.Background>
<Grid x:Name="LayoutRoot">
<Rectangle Stroke="#FF727272"/>
<Image Source="Clock.png"
Stretch="Fill"
Height="128"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="128"
Margin="10,10,0,0" />
<TextBlock HorizontalAlignment="Left"
Margin="143,0,0,0"
TextWrapping="Wrap"
Text="{Binding Timestamp}"
VerticalAlignment="Center"
FontSize="40"
Foreground="#F2346172" />
</Grid>
</UserControl>

View File

@@ -0,0 +1,15 @@
using System.Windows.Controls;
namespace Samples.Tutorials.MvvmSample
{
/// <summary>
/// Interaction logic for ClockPopup.xaml
/// </summary>
public partial class ClockPopup : UserControl
{
public ClockPopup()
{
this.InitializeComponent();
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
namespace Samples.Tutorials.MvvmSample
{
public class MvvmSampleViewModel : INotifyPropertyChanged
{
private DispatcherTimer timer;
public string Timestamp
{
get { return DateTime.Now.ToLongTimeString(); }
}
public MvvmSampleViewModel()
{
timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal, OnTimerTick, Application.Current.Dispatcher);
}
private void OnTimerTick(object sender, EventArgs e)
{
//fire a property change event for the timestamp
Application.Current.Dispatcher.BeginInvoke(new Action(() => OnPropertyChanged("Timestamp")));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View File

@@ -0,0 +1,37 @@
<Window x:Class="Samples.Tutorials.MvvmSample.MvvmSampleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Samples.Tutorials.MvvmSample"
xmlns:tb="http://www.hardcodet.net/taskbar"
Title="MvvmSampleWindow"
Height="300"
Width="300">
<Window.DataContext>
<local:MvvmSampleViewModel />
</Window.DataContext>
<Grid>
<!--
the NotifyIcon automatically derives the DataContext of it's
declaring view. Alternatively, you could just assign it manually
-->
<tb:TaskbarIcon IconSource="/Icons/Inactive.ico"
ToolTipText="{Binding Timestamp}">
<tb:TaskbarIcon.TrayPopup >
<!-- the popup, here a custom user control, will also get the DataContext of the NotifyIcon -->
<local:ClockPopup Opacity="0.8" />
</tb:TaskbarIcon.TrayPopup>
</tb:TaskbarIcon>
<TextBlock TextWrapping="Wrap"
Text="MVVM is quite simple - content of the NotifyIcon derive its DataContext. Sample: Hover over the icon in order to see the bound ToolTipText. Click for a richer Popup that is bound to the same property."
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,10,10,0" />
</Grid>
</Window>

View File

@@ -0,0 +1,15 @@
using System.Windows;
namespace Samples.Tutorials.MvvmSample
{
/// <summary>
/// Interaction logic for MvvmSampleWindow.xaml
/// </summary>
public partial class MvvmSampleWindow : Window
{
public MvvmSampleWindow()
{
InitializeComponent();
}
}
}