如何从WPF Prism中的另一个视图获取视图/区域的参考底图viewmodel

如何从WPF Prism中的另一个视图获取视图/区域的参考底图viewmodel,wpf,mvvm,prism,Wpf,Mvvm,Prism,我有3个区域,即菜单、工具栏和内容。当在菜单视图中单击模块(即客户)时,它将导航到内容区域中的相应视图,编辑文本框中的值后,我需要将其保存到DB。在工具栏视图中单击“保存”按钮时,如何获取活动视图的参考底图视图模型并发送到DAL?。 Shell.xaml: <Grid DockPanel.Dock="Left" Width="65" Margin="1"> <Border CornerRadius="1" BorderBrush="Black" BorderTh

我有3个区域,即菜单、工具栏和内容。当在菜单视图中单击模块(即客户)时,它将导航到内容区域中的相应视图,编辑文本框中的值后,我需要将其保存到DB。在工具栏视图中单击“保存”按钮时,如何获取活动视图的参考底图视图模型并发送到DAL?。 Shell.xaml:

<Grid DockPanel.Dock="Left" Width="65" Margin="1">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1">
            <StackPanel Orientation="Vertical" Margin="1" >
                <ContentControl Name="menuControl" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.MenuRegion}"/>
            </StackPanel>
        </Border>
    </Grid>
    <Grid DockPanel.Dock="Top" Margin="1">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1" >
            <StackPanel Orientation="Vertical" Margin="2">
                <ContentControl Name="toolbarControl" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.ToolbarRegion}"/>
            </StackPanel>
        </Border>
    </Grid>
    <Grid DockPanel.Dock="Right" Margin="2">
        <Border CornerRadius="1" BorderBrush="Black" BorderThickness="1">
    <StackPanel Margin="5" >
            <ContentControl Name="contentControl"  DockPanel.Dock="Top" PrismRegions:RegionManager.RegionName="{x:Static infra:RegionNames.ContentRegion}"/>
    </StackPanel>
        </Border>
    </Grid>

Menu.xaml:

<Button Grid.Row="0" Name="btnHome" Background="Transparent" Margin="1" ToolTip="Home" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:HomeView}">
        <TextBlock Height="32" Width="32" Text ="">
            <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/home_32.png"/>
            </TextBlock.Background>
        </TextBlock>
    </Button>
    <Button Grid.Row="1" Name="btnBank" Background="Transparent" Margin="1" ToolTip="Bank" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:BankView}">
        <TextBlock Height="32" Width="32" Text ="">
            <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/bank_64.png"/>
            </TextBlock.Background>
        </TextBlock>
    </Button>
    <Button Grid.Row="2" Name="btnCustomer" Background="Transparent" Margin="1" ToolTip="Customer" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:CustomerView}">
    <TextBlock Height="32" Width="32" Text ="">
        <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/customer_128.ico"/>
        </TextBlock.Background>
    </TextBlock>
    </Button>
    <Button Grid.Row="3" Name="btnEmployee" Background="Transparent" Margin="1" ToolTip="Employee" Command="{x:Static inf:ApplicationCommands.NavigationCommand}" CommandParameter="{x:Type mod:EmployeeView}">
    <TextBlock Height="32" Width="32" Text ="">
        <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/employee_128.png"/>
        </TextBlock.Background>
    </TextBlock>
    </Button>

Toolbar.xaml:

<Button Grid.Column="0" Margin="1" Name="btnAdd" Height="35" Width="35"  ToolTip="Add" Background="Transparent" Command="{Binding AddCommand}" CommandParameter="Add" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/plus_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="1" Margin="1" Name="btnEdit" Height="35" Width="35" ToolTip="Edit" Background="Transparent" Command="{Binding EditCommand}" CommandParameter="Edit"  >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/pencil_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="2" Margin="1" Name="btnSave" Height="35" Width="35" ToolTip="Save" Background="Transparent" Command="{Binding SaveCommand}" CommandParameter="Save" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/save_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>
    <Button Grid.Column="3" Margin="1" Name="btnPrint" Height="35" Width="35" ToolTip="Print" Background="Transparent" Command="{Binding PrintCommand}" CommandParameter="Print" >
            <TextBlock Height="30" Width="30" Text ="">
                <TextBlock.Background>
                    <ImageBrush Stretch="Fill" ImageSource="/Modules;component/Icons/print_32.png"/>
                </TextBlock.Background>
            </TextBlock>
        </Button>

用户界面设计:


基本上,视图的状态完全存在于viewmodel中。在按钮位于外侧的场景中,要保存表单的viewmodel,您可以执行以下操作:

在工具栏视图模型中: 在单击“保存”按钮时引发事件

myEventAggregator.RaiseEvent<SaveButtonClicked>().Publish(args);
myEventAggregator.RaiseEvent().Publish(args);
在viewmodel构造函数上: 订阅当您单击工具栏区域中的“保存”按钮时引发的事件

myEventAggregator.GetEvent<SaveButtonClicked>().Subscribe(yourSubscriberDelegate);
myEventAggregator.GetEvent().Subscribe(yourSubscriberDelegate);
退出表格或取消表格时: 取消订阅该活动。(这可确保您不会有多个活动表单同时保存数据)

myEventAggregator.GetEvent().UnSubscribe(yourSubscriberDelegate);

为此,您需要事件聚合器关于发布和订阅的知识。查找更多详细信息

能否发布内容区域视图代码和视图模型代码。
myEventAggregator.GetEvent<SaveButtonClicked>().UnSubscribe(yourSubscriberDelegate);