Xaml 使用MvvmCross在UWP应用程序中打开新窗口

Xaml 使用MvvmCross在UWP应用程序中打开新窗口,xaml,mvvm,uwp,mvvmcross,viewmodel,Xaml,Mvvm,Uwp,Mvvmcross,Viewmodel,我尝试在我的UWP应用程序的新窗口中打开一个页面。MvvmCross提供的IMvxNavigationService是否可以实现这一点?现在它只替换当前窗口中的页面 导航流 视图模型 public class MyViewModel: MvxViewModel { private readonly IMvxNavigationService _navigationService; public MyViewModel(IMvxNavigationService navigat

我尝试在我的UWP应用程序的新窗口中打开一个页面。MvvmCross提供的
IMvxNavigationService
是否可以实现这一点?现在它只替换当前窗口中的页面

导航流

视图模型

public class MyViewModel: MvxViewModel
{
    private readonly IMvxNavigationService _navigationService;

    public MyViewModel(IMvxNavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    public MvxCommand MyCommand { get; set; }

    public override void Prepare()
    {
        base.Prepare();
        MyCommand = new MvxCommand<>(MyEvent);
    }

    private void MyEvent()
    {
        _navigationService.Navigate<OtherViewModel>();
    }
}
公共类MyViewModel:MvxViewModel
{
专用只读IMvxNavigationService\u navigationService;
公共MyViewModel(IMvxNavigationService导航服务)
{
_导航服务=导航服务;
}
公共MvxCommand MyCommand{get;set;}
公共覆盖无效准备()
{
base.Prepare();
MyCommand=新的MvxCommand(MyEvent);
}
私有void MyEvent()
{
_navigationService.Navigate();
}
}

如果您计划在UWP中打开新窗口,建议使用更传统的方式

公共静态异步任务TryOpenNewWindow(类型页)
{
CoreApplicationView newView=CoreApplication.CreateNewView();
int newViewId=0;
等待newView.Dispatcher.RunAsync(CoreDispatcherPriority.High,()=>
{
框架=新框架();
框架。导航(第页);
Window.Current.Content=frame;
//您必须激活该窗口,以便稍后显示。
Window.Current.Activate();
newViewId=ApplicationView.GetForCurrentView().Id;
});
bool VIEWSHOWED=等待应用程序ViewSwitcher.TryShowAsStandaloneAsync(newViewId);
返回显示的视图;
}
用法

bool isShow=wait TryOpenNewWindow(typeof(MyPage));
使用MVVMCross时,仅在当前窗口中导航而不在新窗口中导航的原因可能是您尚未注册新窗口的框架

如果遵循,则在创建新窗口时可能需要重新注册
公共类设置:MvxWindowsSetup


致以最诚挚的问候。

我现在意识到我可以实现一个自定义演示者并使用新的

MvxWindowPresentationAttribute.cs

public class MvxWindowPresentationAttribute : MvxBasePresentationAttribute
{
}
public class CustomMvxWindowsViewPresenter : MvxWindowsViewPresenter
{
    public CustomMvxWindowsViewPresenter(IMvxWindowsFrame rootFrame) : base(rootFrame)
    {
    }

    public override void RegisterAttributeTypes()
    {
        base.RegisterAttributeTypes();
        AttributeTypesToActionsDictionary.Register<MvxWindowPresentationAttribute>(ShowWindow, CloseWindow);
    }

    private Task<bool> CloseWindow(IMvxViewModel viewModel, MvxWindowPresentationAttribute attribute)
    {
        return base.ClosePage(viewModel, attribute);
    }

    private Task<bool> ShowWindow(Type viewType, MvxWindowPresentationAttribute attribute,
        MvxViewModelRequest request)
    {
        try
        {
            return Task.Run(async () =>
            {
                var requestText = GetRequestText(request);
                var viewsContainer = Mvx.IoCProvider.Resolve<IMvxViewsContainer>();

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
                    async () => {
                        var appWindow = await AppWindow.TryCreateAsync();
                        var appWindowContentFrame = new Frame();
                        appWindowContentFrame.Navigate(viewType, requestText);
                        ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowContentFrame);
                        await appWindow.TryShowAsync();
                        HandleBackButtonVisibility();
                    });
                return true;
            });
        }
        catch (Exception exception)
        {
            return Task.FromResult(false);
        }
    }
}
protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)
{
    return new CustomMvxWindowsViewPresenter(rootFrame);
}
[MvxWindowPresentation]
public sealed partial class OtherView : MvxWindowsPage 
{
   // ...
}
CustomMvxWindowsViewPresenter.cs

public class MvxWindowPresentationAttribute : MvxBasePresentationAttribute
{
}
public class CustomMvxWindowsViewPresenter : MvxWindowsViewPresenter
{
    public CustomMvxWindowsViewPresenter(IMvxWindowsFrame rootFrame) : base(rootFrame)
    {
    }

    public override void RegisterAttributeTypes()
    {
        base.RegisterAttributeTypes();
        AttributeTypesToActionsDictionary.Register<MvxWindowPresentationAttribute>(ShowWindow, CloseWindow);
    }

    private Task<bool> CloseWindow(IMvxViewModel viewModel, MvxWindowPresentationAttribute attribute)
    {
        return base.ClosePage(viewModel, attribute);
    }

    private Task<bool> ShowWindow(Type viewType, MvxWindowPresentationAttribute attribute,
        MvxViewModelRequest request)
    {
        try
        {
            return Task.Run(async () =>
            {
                var requestText = GetRequestText(request);
                var viewsContainer = Mvx.IoCProvider.Resolve<IMvxViewsContainer>();

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
                    async () => {
                        var appWindow = await AppWindow.TryCreateAsync();
                        var appWindowContentFrame = new Frame();
                        appWindowContentFrame.Navigate(viewType, requestText);
                        ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowContentFrame);
                        await appWindow.TryShowAsync();
                        HandleBackButtonVisibility();
                    });
                return true;
            });
        }
        catch (Exception exception)
        {
            return Task.FromResult(false);
        }
    }
}
protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)
{
    return new CustomMvxWindowsViewPresenter(rootFrame);
}
[MvxWindowPresentation]
public sealed partial class OtherView : MvxWindowsPage 
{
   // ...
}
剩下要做的唯一一件事就是用创建的属性
MvxWindowPresentation
注释视图

OtherView.xaml.cs

public class MvxWindowPresentationAttribute : MvxBasePresentationAttribute
{
}
public class CustomMvxWindowsViewPresenter : MvxWindowsViewPresenter
{
    public CustomMvxWindowsViewPresenter(IMvxWindowsFrame rootFrame) : base(rootFrame)
    {
    }

    public override void RegisterAttributeTypes()
    {
        base.RegisterAttributeTypes();
        AttributeTypesToActionsDictionary.Register<MvxWindowPresentationAttribute>(ShowWindow, CloseWindow);
    }

    private Task<bool> CloseWindow(IMvxViewModel viewModel, MvxWindowPresentationAttribute attribute)
    {
        return base.ClosePage(viewModel, attribute);
    }

    private Task<bool> ShowWindow(Type viewType, MvxWindowPresentationAttribute attribute,
        MvxViewModelRequest request)
    {
        try
        {
            return Task.Run(async () =>
            {
                var requestText = GetRequestText(request);
                var viewsContainer = Mvx.IoCProvider.Resolve<IMvxViewsContainer>();

                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
                    async () => {
                        var appWindow = await AppWindow.TryCreateAsync();
                        var appWindowContentFrame = new Frame();
                        appWindowContentFrame.Navigate(viewType, requestText);
                        ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowContentFrame);
                        await appWindow.TryShowAsync();
                        HandleBackButtonVisibility();
                    });
                return true;
            });
        }
        catch (Exception exception)
        {
            return Task.FromResult(false);
        }
    }
}
protected override IMvxWindowsViewPresenter CreateViewPresenter(IMvxWindowsFrame rootFrame)
{
    return new CustomMvxWindowsViewPresenter(rootFrame);
}
[MvxWindowPresentation]
public sealed partial class OtherView : MvxWindowsPage 
{
   // ...
}