forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveContentPage.cs
More file actions
60 lines (54 loc) · 2.01 KB
/
ReactiveContentPage.cs
File metadata and controls
60 lines (54 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using ReactiveUI;
using Xamarin.Forms;
namespace ReactiveUI.XamForms
{
/// <summary>
/// This is an <see cref="ContentPage"/> that is also an <see cref="IViewFor{T}"/>.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model.</typeparam>
/// <seealso cref="Xamarin.Forms.ContentPage" />
/// <seealso cref="ReactiveUI.IViewFor{TViewModel}" />
public class ReactiveContentPage<TViewModel> : ContentPage, IViewFor<TViewModel>
where TViewModel : class
{
/// <summary>
/// The view model bindable property.
/// </summary>
public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(
nameof(ViewModel),
typeof(TViewModel),
typeof(ReactiveContentPage<TViewModel>),
default(TViewModel),
BindingMode.OneWay,
propertyChanged: OnViewModelChanged);
/// <summary>
/// Gets or sets the ViewModel to display.
/// </summary>
public TViewModel ViewModel
{
get => (TViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
/// <inheritdoc/>
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel)value;
}
/// <inheritdoc/>
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
ViewModel = BindingContext as TViewModel;
}
private static void OnViewModelChanged(BindableObject bindableObject, object oldValue, object newValue)
{
bindableObject.BindingContext = newValue;
}
}
}