Prism的RegionManager.RequestNavigate在WPF中不起作用

时间:2019-04-02 14:35:32

标签: c# wpf prism

我正在编写一个小型WPF应用程序,并使用Prism 7.1。似乎一切正常,但是唯一的_regionManager.RequestNavigate()无效。当我单击绑定到DelegateCommand的按钮时,调用了_regionManager.RequestNavigate(),但没有任何反应。这是当我使用具有参数RequestNavigate()navigationCallback为空的NavigationService重载时的图像:

enter image description here

_regionManager是从构造函数分配的。 这种情况不仅发生在一个地方,对_requestManager.RequestNavigate()的所有调用仍然像这样。

这是我的设置方式:

App.xaml.cs:

public partial class App: PrismApplication 
{
    protected override void OnStartup (StartupEventArgs e) 
    {
    base.OnStartup (e);
    }

    protected override void RegisterTypes (IContainerRegistry containerRegistry) 
    {
        containerRegistry.RegisterForNavigation<DangNhapView> ();

        containerRegistry.RegisterSingleton<IBusyMonitor, CompositeMainComponentsBusyMonitor> ();
        containerRegistry.RegisterSingleton<INetRequester, HttpNetService> ();
        containerRegistry.RegisterSingleton<IInternetConnectionChecker, MNBConnectionChecker> ();

    }

    protected override Window CreateShell () 
    {
        return Container.Resolve<MainWindow> ();
    }

    protected override void ConfigureModuleCatalog (IModuleCatalog moduleCatalog) {

    }
}

MainWindow.xaml:

<Window x:Class="XemDiemSinhVienMain.Views.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:local="clr-namespace:XemDiemSinhVienMain.Views"
        mc:Ignorable="d"
        Title="Xem điểm sinh viên" Height="520" Width="940"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:uc="clr-namespace:XemDiemSinhVien.Infrastructures.UserControls;assembly=XemDiemSinhVien.Infrastructures"
        xmlns:constants="clr-namespace:XemDiemSinhVien.Infrastructures.Constants;assembly=XemDiemSinhVien.Infrastructures"
        xmlns:xemDiemSinhVienMain="clr-namespace:XemDiemSinhVienMain"
        Icon="../app_icon.ico">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="1"
                      CornerRadius="0"
                      CaptionHeight="0"
                      UseAeroCaptionButtons="False"
                      ResizeBorderThickness="5"/>
    </WindowChrome.WindowChrome>
    <!-- https://stackoverflow.com/questions/2967218/window-out-of-the-screen-when-maximized-using-wpf-shell-integration-library/2975574 -->
    <Window.Template>
        <ControlTemplate TargetType="{x:Type local:MainWindow}">
            <Border BorderBrush="Green">
                <Border.Style>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="BorderThickness" Value="0"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=WindowState}" Value="Maximized">
                                <Setter Property="BorderThickness" Value="8"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <!-- Window's content -->
                <Grid Background="White"> 
                    <Grid.RowDefinitions>
                        <RowDefinition Height="32"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <ContentControl Grid.Row="1"
                                    prism:RegionManager.RegionName="{x:Static constants:RegionNames.MainContentRegion}"/>
                    <Button Content="Switch to NavigationTestView"
                            Command="{Binding SwitchToNavigationTestViewCommand}"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Top"
                            Margin="10"
                            Grid.Row="1"/>
                    <uc:WindowTitleBar Grid.Row="0"
                                       MinimizeClick="WindowTitleBar_OnMinimizeClick"
                                       CloseClick="WindowTitleBar_OnCloseClick"
                                       MouseLeftButtonDown="WindowTitleBar_OnMouseLeftButtonDown"/>
                </Grid>
            </Border>
        </ControlTemplate>
    </Window.Template>
</Window>

MainWindowViewModel:

public class MainWindowViewModel : BindableBase 
{
    private IRegionManager _regionManager;
    public DelegateCommand SwitchToNavigationTestViewCommand { get; private set; }

    public MainWindowViewModel (IRegionManager regionManager) {
        _regionManager = regionManager;

        SwitchToNavigationTestViewCommand = new DelegateCommand (() => {
            _regionManager.RequestNavigate (RegionNames.MainContentRegion, "DangNhapView");
        });
    }
}

那么我该如何解决此问题?感谢您阅读我的问题。

0 个答案:

没有答案