How to implement a custom presenter in Windows UWP (Xamarin, MvvmCross)

I have the following code in an Android app, it basically uses one page (using NavigationDrawer) and swaps fragments to / from the central view. This allows you to navigate on one page instead of multiple pages:

Setup.cs:

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        var customPresenter = new MvxFragmentsPresenter();
        Mvx.RegisterSingleton<IMvxFragmentsPresenter>(customPresenter);
        return customPresenter;
    }

ShellPage.cs

    public class ShellPage : MvxCachingFragmentCompatActivity<ShellPageViewModel>, IMvxFragmentHost
    {
        .
        .
        .

        public bool Show(MvxViewModelRequest request, Bundle bundle)
        {
            if (request.ViewModelType == typeof(MenuContentViewModel))
            {
                ShowFragment(request.ViewModelType.Name, Resource.Id.navigation_frame, bundle);
                return true;
            }
            else
            {
                ShowFragment(request.ViewModelType.Name, Resource.Id.content_frame, bundle, true);
                return true;
            }
        }

        public bool Close(IMvxViewModel viewModel)
        {
            CloseFragment(viewModel.GetType().Name, Resource.Id.content_frame);
            return true;
        }

        .
        .
        .
    }

How can I achieve the same behavior in a Windows UWP application? Rather, is there any example that exists for a Windows MvvmCross application that implements CustomPresenter? This may at least give me a start on how to implement it.

Thanks!

UPDATE:

Finally, I'm starting to figure out how to do this with the presenter:

    public class CustomPresenter : IMvxWindowsViewPresenter
    {
        IMvxWindowsFrame _rootFrame;

        public CustomPresenter(IMvxWindowsFrame rootFrame)
        {
            _rootFrame = rootFrame;
        }

        public void AddPresentationHintHandler<THint>(Func<THint, bool> action) where THint : MvxPresentationHint
        {
            throw new NotImplementedException();
        }

        public void ChangePresentation(MvxPresentationHint hint)
        {
            throw new NotImplementedException();
        }

        public void Show(MvxViewModelRequest request)
        {
            if (request.ViewModelType == typeof(ShellPageViewModel))
            {
                //_rootFrame?.Navigate(typeof(ShellPage), null);    // throws an exception

                ((Frame)_rootFrame.UnderlyingControl).Content = new ShellPage();
            }
        }
    }

ShellPage, . , Content ShellPage, , ShellMage ViewModel , . , ViewModels MvvmCross, OnNavigatedTo???

+4
2

UWP. Android, -, . :

, - ViewModels. .

: MvvmCross 4.0.0-beta3

Presenter

using System;
using Cirrious.CrossCore;
using Cirrious.CrossCore.Exceptions;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.Views;
using Cirrious.MvvmCross.WindowsUWP.Views;
using xxxxx.WinUniversal.Extensions;

namespace xxxxx.WinUniversal.Presenters
{
    public class MvxWindowsMultiRegionViewPresenter
        : MvxWindowsViewPresenter
    {
        private readonly IMvxWindowsFrame _rootFrame;

        public MvxWindowsMultiRegionViewPresenter(IMvxWindowsFrame rootFrame)
            : base(rootFrame)
        {
            _rootFrame = rootFrame;
        }

        public override async void Show(MvxViewModelRequest request)
        {
            var host = _rootFrame.Content as IMvxMultiRegionHost;
            var view = CreateView(request);

            if (host != null && view.HasRegionAttribute())
            {
                host.Show(view as MvxWindowsPage);
            }
            else
            {
                base.Show(request);
            }
        }

        private static IMvxWindowsView CreateView(MvxViewModelRequest request)
        {
            var viewFinder = Mvx.Resolve<IMvxViewsContainer>();

            var viewType = viewFinder.GetViewType(request.ViewModelType);
            if (viewType == null)
                throw new MvxException("View Type not found for " + request.ViewModelType);

            // Create instance of view
            var viewObject = Activator.CreateInstance(viewType);
            if (viewObject == null)
                throw new MvxException("View not loaded for " + viewType);

            var view = viewObject as IMvxWindowsView;
            if (view == null)
                throw new MvxException("Loaded View is not a IMvxWindowsView " + viewType);

            view.ViewModel = LoadViewModel(request);

            return view;
        }

        private static IMvxViewModel LoadViewModel(MvxViewModelRequest request)
        {
            // Load the viewModel
            var viewModelLoader = Mvx.Resolve<IMvxViewModelLoader>();

            return viewModelLoader.LoadViewModel(request, null);
        }
    }
}

IMvxMultiRegionHost

using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.WindowsUWP.Views;

namespace xxxxx.WinUniversal.Presenters
{
    public interface IMvxMultiRegionHost
    {
        void Show(MvxWindowsPage view);

        void CloseViewModel(IMvxViewModel viewModel);

        void CloseAll();
    }
}

RegionAttribute

using System;

namespace xxxxx.WinUniversal.Presenters
{
    [AttributeUsage(AttributeTargets.Class)]
    public sealed class RegionAttribute
        : Attribute
    {
        public RegionAttribute(string regionName)
        {
            Name = regionName;
        }

        public string Name { get; private set; }
    }
}

, . IMvxMultiRegionHost MvxWindowsPage.

, :

HomeView.xaml.cs

using System;
using System.Diagnostics;
using System.Linq;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.WindowsUWP.Views;
using xxxxx.Shared.Controls;
using xxxxx.WinUniversal.Extensions;
using xxxxx.WinUniversal.Presenters;
using xxxxx.Core.ViewModels;

namespace xxxxx.WinUniversal.Views
{
    public partial class HomeView
        : MvxWindowsPage
        , IMvxMultiRegionHost
    {
        public HomeView()
        {
            InitializeComponent();
        }

        // ...

        public void Show(MvxWindowsPage view)
        {
            if (!view.HasRegionAttribute())
                throw new InvalidOperationException(
                    "View was expected to have a RegionAttribute, but none was specified.");

            var regionName = view.GetRegionName();

            RootSplitView.Content = view;
        }

        public void CloseViewModel(IMvxViewModel viewModel)
        {
            throw new NotImplementedException();
        }

        public void CloseAll()
        {
            throw new NotImplementedException();
        }
    }
}

- , xaml . , SplitView Content , ShowView HomeView.

HomeView.xaml

<SplitView x:Name="RootSplitView"
           DisplayMode="CompactInline"
           IsPaneOpen="false"
           CompactPaneLength="48"
           OpenPaneLength="200">
    <SplitView.Pane>
        // Some ListView with menu items.
    </SplitView.Pane>
    <SplitView.Content>
        // Initial content..
    </SplitView.Content>
</SplitView>

EDIT:

, , [Region].

public static class RegionAttributeExtentionMethods
{
    public static bool HasRegionAttribute(this IMvxWindowsView view)
    {
        var attributes = view
            .GetType()
            .GetCustomAttributes(typeof(RegionAttribute), true);

        return attributes.Any();
    }

    public static string GetRegionName(this IMvxWindowsView view)
    {
        var attributes = view
            .GetType()
            .GetCustomAttributes(typeof(RegionAttribute), true);

        if (!attributes.Any())
            throw new InvalidOperationException("The IMvxView has no region attribute.");

        return ((RegionAttribute)attributes.First()).Name;
    }
}

, .

+7

@Stephanvs , -, , - :

Windows 10 UWP MvvmCross 18 2015 MvvmCross, Xamarin, UWP, Windows 10, Presenter Windows

Windows Store Windows 10 Universal Windows Platform. MvvmCross UWP 4.0 beta2.

UWP - SplitView. , , . (in).

MvvmCross , SplitView, ViewModels. , - , . , , . MultiRegionPresenter

Setup.cs UWP CreateViewPresenter .

`protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)  
{
    return new MvxWindowsMultiRegionViewPresenter(rootFrame);
}`

, . Frame, .

`<mvx:MvxWindowsPage ...>  
    <Grid>
        <!-- ... -->

        <SplitView>
            <SplitView.Pane>
                <!-- Menu Content as ListView or something similar -->
            </SplitView.Pane>
            <SplitView.Content>
                <Frame x:Name="MainContent" />
            </SplitView.Content>
        </SplitView>
    </Grid>
</mvx:MvxWindowsPage> ` 

, ShowViewModel (...) , MainContent.

MvxRegionAttribute, , , . Frame .

`[MvxRegion("MainContent")]
public partial class PersonView  
{
    // ...
}`

. .

, .

`<Frame x:Name="MainContent">  
    <Frame.ContentTransitions>
        <TransitionCollection>
            <NavigationThemeTransition>
                  <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                      <EntranceNavigationTransitionInfo />
                </NavigationThemeTransition.DefaultNavigationTransitionInfo>
            </NavigationThemeTransition>
        </TransitionCollection>
    </Frame.ContentTransitions>
</Frame>`  

.

, , Stephanvs

+1

Source: https://habr.com/ru/post/1611650/


All Articles