|
| 1 | +# Implementing a Dialog View |
| 2 | + |
| 3 | +GitHub for Visual Studio has a common dialog which is used to show the login, clone, create repository etc. operations. To add a new view to the dialog and show the dialog with this view, you need to do the following: |
| 4 | + |
| 5 | +## Create a View Model and Interface |
| 6 | + |
| 7 | +- Create an interface for the view model that implements `IDialogContentViewModel` in `GitHub.Exports.Reactive\ViewModels\Dialog` |
| 8 | +- Create a view model that inherits from `NewViewModelBase` and implements the interface in `GitHub.App\ViewModels\Dialog` |
| 9 | +- Export the view model with the interface as the contract and add a `[PartCreationPolicy(CreationPolicy.NonShared)]` attribute |
| 10 | + |
| 11 | +A minimal example that just exposes a command that will dismiss the dialog: |
| 12 | + |
| 13 | +```csharp |
| 14 | +using System; |
| 15 | +using ReactiveUI; |
| 16 | + |
| 17 | +namespace GitHub.ViewModels.Dialog |
| 18 | +{ |
| 19 | + public interface IExampleDialogViewModel : IDialogContentViewModel |
| 20 | + { |
| 21 | + ReactiveCommand<object> Dismiss { get; } |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +```csharp |
| 27 | +using System; |
| 28 | +using System.ComponentModel.Composition; |
| 29 | +using ReactiveUI; |
| 30 | + |
| 31 | +namespace GitHub.ViewModels.Dialog |
| 32 | +{ |
| 33 | + [Export(typeof(IExampleDialogViewModel))] |
| 34 | + [PartCreationPolicy(CreationPolicy.NonShared)] |
| 35 | + public class ExampleDialogViewModel : ViewModelBase, IExampleDialogViewModel |
| 36 | + { |
| 37 | + [ImportingConstructor] |
| 38 | + public ExampleDialogViewModel() |
| 39 | + { |
| 40 | + Dismiss = ReactiveCommand.Create(); |
| 41 | + } |
| 42 | + |
| 43 | + public string Title => "Example Dialog"; |
| 44 | + |
| 45 | + public ReactiveCommand<object> Dismiss { get; } |
| 46 | + |
| 47 | + public IObservable<object> Done => Dismiss; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Create a View |
| 53 | + |
| 54 | +- Create a WPF `UserControl` under `GitHub.VisualStudio\Views\Dialog` |
| 55 | +- Add an `ExportViewFor` attribute with the type of the view model interface |
| 56 | +- Add a `PartCreationPolicy(CreationPolicy.NonShared)]` attribute |
| 57 | + |
| 58 | +Continuing the example above: |
| 59 | + |
| 60 | +```xml |
| 61 | +<UserControl x:Class="GitHub.VisualStudio.Views.Dialog.ExampleDialogView" |
| 62 | + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
| 63 | + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
| 64 | + <Button Command="{Binding Dismiss}" HorizontalAlignment="Center" VerticalAlignment="Center"> |
| 65 | + Dismiss |
| 66 | + </Button> |
| 67 | +</UserControl> |
| 68 | +``` |
| 69 | + |
| 70 | +```csharp |
| 71 | +using System.ComponentModel.Composition; |
| 72 | +using System.Windows.Controls; |
| 73 | +using GitHub.Exports; |
| 74 | +using GitHub.ViewModels.Dialog; |
| 75 | + |
| 76 | +namespace GitHub.VisualStudio.Views.Dialog |
| 77 | +{ |
| 78 | + [ExportViewFor(typeof(IExampleDialogViewModel))] |
| 79 | + [PartCreationPolicy(CreationPolicy.NonShared)] |
| 80 | + public partial class ExampleDialogView : UserControl |
| 81 | + { |
| 82 | + public ExampleDialogView() |
| 83 | + { |
| 84 | + InitializeComponent(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Show the Dialog! |
| 91 | + |
| 92 | +To show the dialog you will need an instance of the `IShowDialogService` service. Once you have that, simply call the `Show` method with an instance of your view model. |
| 93 | + |
| 94 | +```csharp |
| 95 | +var viewModel = new ExampleDialogViewModel(); |
| 96 | +showDialog.Show(viewModel) |
| 97 | +``` |
| 98 | + |
| 99 | +## Optional: Add a method to `DialogService` |
| 100 | + |
| 101 | +Creating a view model like this may be the right thing to do, but it's not very reusable or testable. If you want your dialog to be easy reusable, add a method to `DialogService`: |
| 102 | + |
| 103 | +```csharp |
| 104 | +public async Task ShowExampleDialog() |
| 105 | +{ |
| 106 | + var viewModel = factory.CreateViewModel<IExampleDialogViewModel>(); |
| 107 | + await showDialog.Show(viewModel); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Obviously, add this method to `IDialogService` too. |
| 112 | + |
| 113 | +Note that these methods are `async` - this means that if you need to do asynchronous initialization of your view model, you can do it here before calling `showDialog`. |
0 commit comments