コンストラクタでごにょごにょやるのは嫌だ
Views/MainWindow
<Window x:Class="WpfApp2.Views.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp2"
xmlns:prism="http://prismlibrary.com/"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="MainWindow" Height="450" Width="800">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<TextBlock Text="{Binding Message.Value}"></TextBlock>
</Grid>
</Window>
ViewModels/MainWindowViewModel
using Prism.Mvvm;
using Reactive.Bindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp2.ViewModels
{
internal class MainWindowViewModel : BindableBase
{
public ReactiveCommand LoadedCommand { get; set; } = new ReactiveCommand();
public ReactiveProperty<string> Message { get; set; } = new ReactiveProperty<string>();
public MainWindowViewModel()
{
LoadedCommand.Subscribe(() => {
Message.Value = $"Loaded {DateTime.Now:yyyy/MM/dd HH:mm:ss}";
});
}
}
}
前に同じような記事を書いた気がするのはきっと気のせい。