Prism v7.2.0.1233 Release Notes

Release Date: 2019-05-16 // almost 5 years ago
  • This is the 3rd public preview for Prism 7.2.

    ๐Ÿ›  This preview provides a number of bug fixes and enhancements.

    Prism Core

    • #1668: Expands IoC abstractions to allow checking for registered services and naming instances or singletons.
    • ๐Ÿ›  #1680: Fixes Ambiguous match when using DelegateCommand.ObserveProperty
    • #1677: Changes IContainerRegistry to provide a Fluent API

    Prism.WPF

    • ๐Ÿ‘ #1296: Support for IDestructilbe in WPF projects
    • ๐Ÿ‘ #1544: .Net Core 3 Support
    • #1601: InitializeModules() should be called even if there's no shell
    • #1676: Ambiguous match found when ObserveProperty
    • ๐Ÿ”Š #1666: A New IDialogService for WPF

    ๐Ÿ”Š A New IDialogService for WPF

    ๐Ÿ Currently, the only way to show any type of dialog with Prism is by using the PopupWindowAction in combination with System.Windows.Interactivity. To be honest, I really dislike this approach. It's over complex, highly verbose, difficult to implement, and is very limited. The limitations are covered pretty well in Issue #864

    ๐Ÿ”Š Instead, I created a new IDialogService API that will replace the PopupWindowAction altogether. This service will allow developers to show any dialog they want either modal, or non-modal, and have complete control over their dialog logic.

    The implementation looks like this:

        public interface IDialogService
        {
            void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback);
            void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback);
        }
    

    ๐Ÿ”Š The idea here is that Prism will no longer provide any built-in dialogs like Notification or Confirmation. Mainly because the Prism implementations are UGLY and will never match the styling of your beautiful WPF application. So, it's important that you are able to register your own dialogs.

    Create Your Dialog View

    ๐Ÿ‘‰ Your dialog view is a simple UserContro l that can be designed anyway you please. The only requirement it has a ViewModel that implements IDialogAware set as it's DataContext. Preferably, it will utilize the ViewModelLocator

    <UserControl x:Class="HelloWorld.Dialogs.NotificationDialog"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:prism="http://prismlibrary.com/"
                 prism:ViewModelLocator.AutoWireViewModel="True"
                 Width="300" Height="150">
        <Grid x:Name="LayoutRoot" Margin="5">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
    
            <TextBlock Text="{Binding Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" TextWrapping="Wrap" />
            <Button Command="{Binding CloseDialogCommand}" CommandPrameter="true" Content="OK" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,10,0,0" Grid.Row="1" IsDefault="True" />
        </Grid>
    </UserControl>
    

    Create Your Dialog ViewModel

    Next you need a ViewModel that implements IDialogAware which is defined as follows

        public interface IDialogAware
        {
            bool CanCloseDialog();
            void OnDialogClosed();
            void OnDialogOpened(IDialogParameters parameters);
            string Title { get; set; }
            event Action<IDialogResult> RequestClose;
        }
    

    Here is a simple example of what an IDialogAware ViewModel may look like.

        public class NotificationDialogViewModel : BindableBase, IDialogAware
        {
            private DelegateCommand<string> _closeDialogCommand;
            public DelegateCommand<string> CloseDialogCommand =>
                _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));
    
            private string _message;
            public string Message
            {
                get { return _message; }
                set { SetProperty(ref _message, value); }
            }
    
            private string _title = "Notification";
            public string Title
            {
                get { return _title; }
                set { SetProperty(ref _title, value); }
            }
    
            public event Action<IDialogResult> RequestClose;
    
            protected virtual void CloseDialog(string parameter)
            {
                bool? result = null;
    
                if (parameter?.ToLower() == "true")
                    result = true;
                else if (parameter?.ToLower() == "false")
                    result = false;
    
                RaiseRequestClose(new DialogResult(result));
            }
    
            public virtual void RaiseRequestClose(IDialogResult dialogResult)
            {
                RequestClose?.Invoke(dialogResult);
            }
    
            public virtual bool CanCloseDialog()
            {
                return true;
            }
    
            public virtual void OnDialogClosed()
            {
    
            }
    
            public virtual void OnDialogOpened(IDialogParameters parameters)
            {
                Message = parameters.GetValue<string>("message");
            }
        }
    

    Register the Dialog

    To register a dialog, you must have a View (UserControl) and a corresponding ViewModel (which must implement IDialogAware). In the RegisterTypes method, simply register your dialog like you would any other service by using the IContainterRegistery.RegisterDialog method.

     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
         containerRegistry.RegisterDialog<NotificationDialog, NotificationDialogViewModel>();
     }
    

    Using the Dialog Service

    To use the dialog service you simply ask for the service in your VM ctor.

    public MainWindowViewModel(IDialogService dialogService)
    {
        _dialogService = dialogService;
    }
    

    ๐Ÿ”Š Then call either Show or ShowDialog providing the name of the dialog, any parameters your dialogs requires, and then handle the result via a call back

    private void ShowDialog()
    {
        var message = "This is a message that should be shown in the dialog.";
        //using the dialog service as-is
        _dialogService.ShowDialog("NotificationDialog", new DialogParameters($"message={message}"), r =>
        {
            if (!r.Result.HasValue)
                Title = "Result is null";
            else if (r.Result == true)
                Title = "Result is True";
            else if (r.Result == false)
                Title = "Result is False";
            else
                Title = "What the hell did you do?";
        });
    }
    

    Simplify your Application Dialog APIs

    ๐Ÿ”Š The intent of the dialog API is not to try and guess exactly what type of parameters your need for all of your dialogs, but rather to just create and show the dialogs. To simplify common dialogs in your application the guidance will be to create an extension methods to simplify your applications dialogs.

    For example:

    public static class DialogServiceEstensions
    {
        public static void ShowNotification(this IDialogService dialogService, string message, Action<IDialogResult> callBack)
        {
            dialogService.ShowDialog("NotificationDialog", new DialogParameters($"message={message}"), callBack);
        }
    }
    

    Then to call your Notifications use the new and improved API that you created specifically for your app.

        _dialogService.ShowNotification(message, r =>
        {
            if (!r.Result.HasValue)
                Title = "Result is null";
            else if (r.Result == true)
                Title = "Result is True";
            else if (r.Result == false)
                Title = "Result is False";
            else
                Title = "What the hell did you do?";
        });
    

    Register a Custom Dialog Window

    ๐Ÿ”Š It's very common to be using a third-party control vendor such as Infragistics. In these cases, you may want to replace the standard WPF Window control that hosts the dialogs with a custom Window class such as the Infragistics XamRibbonWindow control.

    In this case, just create your custom Window, and implement the IDialogWindow interface:

    public partial class MyRibbonWindow: XamRibbonWindow, IDialogWindow
    {
        public IDialogResult Result { get; set; }
        โ€ฆ.
    }
    

    Then register your dialog window with the IContainerRegistry.

     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
         containerRegistry.RegisterDialogWindow<MyRibbonWindow>();
     }
    

    ๐Ÿ’… Style the DailogWindow

    ๐Ÿ’… You can control the properties of the DialogWindow by using a style via an attatched property on the Dialog UserControl

        <prism:Dialog.WindowStyle>
            <Style TargetType="Window">
                <Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
                <Setter Property="ResizeMode" Value="NoResize"/>
                <Setter Property="ShowInTaskbar" Value="False"/>
                <Setter Property="SizeToContent" Value="WidthAndHeight"/>
            </Style>
        </prism:Dialog.WindowStyle>
    

    ๐Ÿšš To clarify, this is to replace the PopupWindowAction. I want to remove that mess completely from Prism

    Prism.Forms

    • #1589: OnNavigatedFrom method is not called when changing detail on MasterDetailPage
    • #1615: Add the ability to switch tabs
    • #1625: Master Detail to Tab Page Navigation broken in 7.1
    • #1683: Add navigation animation or modal navigation with XAML Navigation Extensions
    • ๐Ÿ”จ #1663: Refactored platform navigation methods
    • #1669: Allow Create/Destroy Action Sheet buttons with only a name. Allow passing tuples to Navigation Service
    • #1700: Platform Specific View Registration not respected
    • #1704: Make XAML Navigation Extensions inherit from BindableObject. Make properties Bindable
    • #1748: [BREAKING] Make INavigatingAware Obsolete and introduce IInitialize, IInitializeAsync & IAutoInitialize
    • #1757: Add Automatic View Registration

    View/ViewModel Initialization

    ๐Ÿ— After numerous user survey's and interviews it became apparent that the intent of INavigatingAware had been become unclear and that users were actually overwhelmingly asking for a breaking change. INavigatingAware is no longer supported. For those who may be using OnNavigatingTo with INavigationAware this will be most impactful as a behavior change as INavigatingAware has been removed from INavigationAware meaning that it will no longer be called. For those who have implemented INavigatingAware directly you will see a build error. The impact should be minimal by simply renaming all instances of INavigatingAware to Initialize.

    Using IAutoInitialize

    0๏ธโƒฃ IAutoInitialize is designed for those cases where you are passing objects around. By default we do a Non-Case-Sensitive lookup between the Navigation Parameters and the Properties in your ViewModel. If one is found we will automatically try to set the property for you.

    \_navigationService.NavigateAsync("ViewB" ("title", "Hello World"), ("fooBar", "some other value"));public class ViewBViewModel : IAutoInitialize{ public string Title { get; set; } public string FooBar { get; set; } }
    

    In the above example your ViewModel will be initialized with the values passed in the NavigationParameters. If these are static variables (meaning the won't change during the lifecycle of your View/ViewModel you would not need to implement INotifyPropertyChanged for them.

    public class ViewBViewModel{ [NavigationParameter(true) public string Title { get; set; } [NavigationParameter("fooBar") public string Foo { get; set; } }
    

    ๐Ÿ‘ป In this example you'll notice that we have some slight variations where we have added NavigationParameter attributes. The attributes allow you to decorate your properties to make them Required or specify a different name that you will use in your NavigationParameters. Note that if you make a property Required and you fail to pass the parameter this will throw an Exception that you will need to check for in the NavigationResult.

    Automatic View Registration

    Automatic Registration can be used for either an Application or Module. Note that there is an optional Automatic property. When Automatic is set to true Prism will not look for any attributes but will simply look for any contained types that are a Page type. If there is a Page type it will automatically be registered.

    [AutoRegisterForNavigation]public class App : PrismApplication{ } [AutoRegisterForNavigation(Automatic = true)]public class AwesomeModule : Imodule{ }
    

    In the above example when the App is initializing Prism will reflect on the assembly of the App to look for any RegisterForNavigation Attributes first at an assembly level and then at a page level.

    [assembly: RegisterForNavigation(ViewType = typeof(ViewA))] [assembly: RegisterForNavigation(ViewType = typeof(ViewB), Name = "viewB")] [assembly: RegisterForNavigation(ViewType = typeof(ViewC), Name = "platformSpecific", RuntimePlatform = RuntimePlatform.Android)] [assembly: RegisterForNavigation(ViewType = typeof(ViewD), Name = "platformSpecific", RuntimePlatform = RuntimePlatform.iOS)]// OR// Registered as ViewA[RegisterForNavigation]public class ViewA : ContentPage { } [RegisterForNavigation(Name = "viewB")]public class ViewB : ContentPage { }// Ignored if no attribute exists unless Automatic Registration was enabledpublic class ViewB : ContentPage { }// Only Registered on iOS[RegisterForNavigation(RuntimePlatform = RuntimePlatform.iOS)]public class ViewD : ContentPage { }