Skip to content

Commit

Permalink
Add Reactive Page
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Mar 2, 2024
1 parent 75d47d0 commit 28a14ef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"version": "8.0.10",
"rollForward": "latestMinor"
"rollForward": "latestFeature"
},
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "3.0.44"
Expand Down
74 changes: 74 additions & 0 deletions src/CrissCross.WPF.UI/Controls/Page/ReactivePage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Windows;
using System.Windows.Controls;
using ReactiveUI;

namespace CrissCross.WPF.UI.Controls;

/// <summary>
/// A <see cref="Page"/> that is reactive.
/// </summary>
/// <remarks>
/// <para>
/// This class is a <see cref="Page"/> that is also reactive. That is, it implements <see cref="IViewFor{TViewModel}"/>.
/// You can extend this class to get an implementation of <see cref="IViewFor{TViewModel}"/> rather than writing one yourself.
/// </para>
/// <para>
/// Note that the XAML for your control must specify the same base class, including the generic argument you provide for your view
/// model. To do this, use the <c>TypeArguments</c> attribute as follows:
/// <code>
/// <![CDATA[
/// <ui:ReactivePage
/// x:Class="Foo.Bar.Views.YourView"
/// x:TypeArguments="vms:YourViewModel"
/// xmlns:ui="https://github.com/ChrisPulman/CrissCross.ui"
/// xmlns:vms="clr-namespace:Foo.Bar.ViewModels"
/// xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
/// xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
/// xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
/// xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
/// mc:Ignorable="d">
/// <!-- view XAML here -->
/// </ui:ReactivePage>
/// ]]>
/// </code>
/// </para>
/// </remarks>
/// <typeparam name="TViewModel">
/// The type of the view model backing the view.
/// </typeparam>
public class ReactivePage<TViewModel> :
Page, IViewFor<TViewModel>
where TViewModel : class
{
/// <summary>
/// The view model dependency property.
/// </summary>
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register(
nameof(ViewModel),
typeof(TViewModel),
typeof(ReactivePage<TViewModel>),
new PropertyMetadata(null));

/// <summary>
/// Gets the binding root view model.
/// </summary>
public TViewModel? BindingRoot => ViewModel;

/// <inheritdoc/>
public TViewModel? ViewModel
{
get => (TViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}

/// <inheritdoc/>
object? IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel?)value;
}
}

0 comments on commit 28a14ef

Please sign in to comment.