Skip to content

Commit b51e2eb

Browse files
author
Sam Storie
committed
Updated with OAPH on MainWindow and hard-wired view model
1 parent 2ed2858 commit b51e2eb

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

reactiveui-simple-user-control/SimpleApp/SimpleApp/MainWindow.xaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66
xmlns:local="clr-namespace:SimpleApp"
77
mc:Ignorable="d"
88
Title="MainWindow" Height="350" Width="525">
9-
<Grid>
10-
9+
<Grid Margin="8">
10+
<Grid.RowDefinitions>
11+
<RowDefinition Height="*"></RowDefinition>
12+
<RowDefinition Height="*"></RowDefinition>
13+
</Grid.RowDefinitions>
14+
15+
<DockPanel Grid.Row="0" LastChildFill="True">
16+
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Center">MainWindow Property</TextBlock>
17+
18+
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="CurrentValue" FontSize="48"></TextBlock>
19+
</DockPanel>
20+
21+
<DockPanel Grid.Row="1" LastChildFill="True">
22+
<TextBlock DockPanel.Dock="Top" HorizontalAlignment="Center">Embedded User Control</TextBlock>
23+
24+
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Nothing yet...</TextBlock>
25+
</DockPanel>
1126
</Grid>
1227
</Window>

reactiveui-simple-user-control/SimpleApp/SimpleApp/MainWindow.xaml.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,33 @@
1212
using System.Windows.Media.Imaging;
1313
using System.Windows.Navigation;
1414
using System.Windows.Shapes;
15+
using ReactiveUI;
1516

1617
namespace SimpleApp
1718
{
1819
/// <summary>
1920
/// Interaction logic for MainWindow.xaml
2021
/// </summary>
21-
public partial class MainWindow : Window
22+
public partial class MainWindow : Window, IViewFor<MainWindowViewModel>
2223
{
2324
public MainWindow()
2425
{
26+
ViewModel = new MainWindowViewModel();
2527
InitializeComponent();
28+
DataContext = ViewModel;
29+
30+
this.WhenActivated(d =>
31+
{
32+
d(this.OneWayBind(ViewModel, vm => vm.CurrentValue, v => v.CurrentValue.Text));
33+
});
2634
}
35+
36+
object IViewFor.ViewModel
37+
{
38+
get { return ViewModel; }
39+
set { ViewModel = (MainWindowViewModel)value; }
40+
}
41+
42+
public MainWindowViewModel ViewModel { get; set; }
2743
}
2844
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reactive.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using ReactiveUI;
8+
9+
namespace SimpleApp
10+
{
11+
public class MainWindowViewModel : ReactiveObject
12+
{
13+
readonly ObservableAsPropertyHelper<long> _currentValue;
14+
public long CurrentValue => _currentValue.Value;
15+
16+
public MainWindowViewModel()
17+
{
18+
19+
Observable.Interval(TimeSpan.FromSeconds(1))
20+
.ToProperty(this, x => x.CurrentValue, out _currentValue, 0, false, RxApp.MainThreadScheduler);
21+
}
22+
}
23+
}

reactiveui-simple-user-control/SimpleApp/SimpleApp/SimpleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
</Compile>
9090
</ItemGroup>
9191
<ItemGroup>
92+
<Compile Include="MainWindowViewModel.cs" />
9293
<Compile Include="Properties\AssemblyInfo.cs">
9394
<SubType>Code</SubType>
9495
</Compile>

0 commit comments

Comments
 (0)