Skip to content

Instantly share code, notes, and snippets.

@nathanpovo
Created May 26, 2021 12:51
Show Gist options
  • Save nathanpovo/739e58d2cd60dd8bced2b9bb4b25f3b5 to your computer and use it in GitHub Desktop.
Save nathanpovo/739e58d2cd60dd8bced2b9bb4b25f3b5 to your computer and use it in GitHub Desktop.
MahApps.Metro dialogs with ReactiveUI
<Application x:Class="ReactiveUIMahAppsDialog.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Theme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.FlatButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
using System.Reflection;
using System.Windows;
using ReactiveUI;
using Splat;
namespace ReactiveUIMahAppsDialog
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
}
}
}
<reactiveUi:ReactiveUserControl
x:Class="ReactiveUIMahAppsDialog.ChildView"
x:TypeArguments="viewModels:ChildViewModel"
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"
xmlns:reactiveUi="http://reactiveui.net"
xmlns:viewModels="clr-namespace:ReactiveUIMahAppsDialog"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button x:Name="Test" Content="Show Dialog" Width="100" Height="100"/>
</Grid>
</reactiveUi:ReactiveUserControl>
using System.Reactive;
using System.Reactive.Disposables;
using System.Windows;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using ReactiveUI;
using Splat;
namespace ReactiveUIMahAppsDialog
{
public partial class ChildView
{
public ChildView()
{
InitializeComponent();
this.WhenActivated(disposables =>
{
ViewModel ??= Locator.Current.GetService<ChildViewModel>() ?? new ChildViewModel();
this.BindCommand(ViewModel,
viewModel => viewModel.LogInUser,
view => view.Test)
.DisposeWith(disposables);
this.ViewModel.Message
.RegisterHandler(async interaction =>
{
MetroWindow window = (MetroWindow) Window.GetWindow(this);
await window.ShowMessageAsync("test", "test");
interaction.SetOutput(Unit.Default);
})
.DisposeWith(disposables);
});
}
}
}
using System.Reactive;
using System.Reactive.Linq;
using ReactiveUI;
namespace ReactiveUIMahAppsDialog
{
public class ChildViewModel : ReactiveObject
{
public ChildViewModel()
{
Message = new Interaction<Unit, Unit>();
LogInUser = ReactiveCommand.CreateFromTask(async _ => await Message.Handle(Unit.Default));
}
public ReactiveCommand<Unit, Unit> LogInUser { get; }
public Interaction<Unit, Unit> Message { get; }
}
}
<mah:MetroWindow
x:Class="ReactiveUIMahAppsDialog.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:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:reactiveUiMahAppsDialog="clr-namespace:ReactiveUIMahAppsDialog"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
WindowStartupLocation="CenterScreen">
<Grid>
<reactiveUiMahAppsDialog:ChildView/>
</Grid>
</mah:MetroWindow>
namespace ReactiveUIMahAppsDialog
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.4.5" />
<PackageReference Include="ReactiveUI.WPF" Version="13.2.18" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment