区域管理器找不到自定义弹出窗口

时间:2016-12-18 21:06:42

标签: prism

我有一个使用具有棱镜的PopupWindowAction的交互对象的ShellView来显示我的自定义设置视图。我的ShellViewModel包含InteractionRequest对象和一个将触发用户交互的Delegate Command。用户触发交互后,自定义设置视图(DataFeedManagerView)将显示在ShellView的中心。在My DataFeedManagerView中,左侧有一个DataFeeds列表(ListBox控件),右侧有datafeed特定设置视图(ContentControl,通过RegionManager设置Region)。首先,我使用RegisterViewWithRegion注册了所有视图。然后我尝试做的是通过Region的Activate方法激活内容控件中的相关对象设置视图。当我尝试这样做时,我收到错误"找不到地区"。所以我们不能在自定义弹出窗口中使用区域???

PS1:也许这是如此简单的要求,但包含很多步骤,因为我的解释有点复杂。我希望代码更具描述性。

PS2:我对使用简单绑定到ContentControl的内容属性满足了期望。但我担心在自定义交互弹出窗口中使用区域的错误和/或正确解决方案是什么。

.. ::壳牌:: ..

<Window x:Class="PrismUnityApp.Views.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:views="clr-namespace:PrismUnityApp.Views"
    xmlns:constants="clr-namespace:PrismUnityApp.Constants"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Title="{Binding Title}" Height="480" Width="640">
<DockPanel LastChildFill="True">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest}">
            <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
                <prism:PopupWindowAction.WindowContent>
                    <views:DataFeedManagerView/>
                </prism:PopupWindowAction.WindowContent>
            </prism:PopupWindowAction>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
    <Button Content=" Show Data Feed Manager" Command="{Binding ShowDataFeedManagerCommand}"/>
    <ContentControl prism:RegionManager.RegionName="{x:Static constants:WellKnownRegionNames.ContentRegion}" />
</DockPanel>

using System.Windows.Input;
using Prism.Commands;
using Prism.Interactivity.InteractionRequest;
using Prism.Mvvm;

namespace PrismUnityApp.ViewModels
{
    public class ShellViewModel : BindableBase
    {
        private string _title = "Prism Unity Application";
        public ICommand ShowDataFeedManagerCommand { get; }
        public InteractionRequest<IConfirmation> ConfirmationRequest { get; }
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public ShellViewModel()
        {
            ConfirmationRequest = new InteractionRequest<IConfirmation>();
            ShowDataFeedManagerCommand = new DelegateCommand(ShowDataFeedManager);
        }
        public void ShowDataFeedManager()
        {
            ConfirmationRequest.Raise(
                new Confirmation {Title = "Data Feed Manager", Content = string.Empty},
                confirmation =>
                {
                });
        }
    }
}

.. :: DataFeedManager :: ..

<UserControl x:Class="PrismUnityApp.Views.DataFeedManagerView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:constants="clr-namespace:PrismUnityApp.Constants"
         xmlns:prism="http://prismlibrary.com/"
         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
         prism:ViewModelLocator.AutoWireViewModel="True"
         Height="240" Width="320">
<DockPanel LastChildFill="True">
    <ListBox
        SelectedItem="{Binding Current, Mode=OneWay}"
        ItemsSource="{Binding DataFeeds}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <prism:InvokeCommandAction
                    Command="{Binding SelectionChangedCommand}"
                    CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}"></prism:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Key}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <ContentControl prism:RegionManager.RegionName="{x:Static constants:WellKnownRegionNames.DataFeedRegion}"></ContentControl>
</DockPanel>

using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using PrismUnityApp.Constants;
using PrismUnityApp.Interfaces;

namespace PrismUnityApp.ViewModels
{
    public class DataFeedManagerViewModel : BindableBase, IDataFeedManagerViewModel
    {
        private readonly IRegionManager _regionManager;
        public IDictionary<string, object> DataFeeds { get; }
        public ICommand SelectionChangedCommand { get; }
        public DataFeedManagerViewModel(IUnityContainer unityContainer, IRegionManager regionManager)
        {
            _regionManager = regionManager;

            SelectionChangedCommand = new DelegateCommand<SelectionChangedEventArgs>(SelectionChanged);

            DataFeeds = new Dictionary<string, object>
            {
                {WellKnownDataFeedNames.SimulationDataFeed, unityContainer.Resolve<ISimulationDataFeedView>()},
                {WellKnownDataFeedNames.BarchartDataFeed, unityContainer.Resolve<IBarchartDataFeedView>()}
            };

            foreach (var dataFeed in DataFeeds)
                _regionManager.RegisterViewWithRegion(WellKnownRegionNames.DataFeedRegion, () => dataFeed.Value);
        }
        public void SelectionChanged(SelectionChangedEventArgs e)
        {
            var addedItem = (KeyValuePair<string, object>) e.AddedItems[0];
            var region = _regionManager.Regions[WellKnownRegionNames.DataFeedRegion];
            region.Activate(addedItem.Value);
        }
    }
}

.. ::引导程序:: ..

using System.Windows;
using Microsoft.Practices.Unity;
using Prism.Unity;
using PrismUnityApp.Interfaces;
using PrismUnityApp.ViewModels;
using PrismUnityApp.Views;

namespace PrismUnityApp
{
    class Bootstrapper : UnityBootstrapper
    {
        #region Overrides of UnityBootstrapper

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            Container.RegisterType<ISimulationDataFeedView, SimulationDataFeedView>();
            Container.RegisterType<ISimulationDataFeedViewModel, SimulationDataFeedViewModel>();
            Container.RegisterType<IBarchartDataFeedView, BarchartDataFeedView>();
            Container.RegisterType<IBarchartDataFeedViewModel, BarchartDataFeedViewModel>();
            Container.RegisterType<IDataFeedManagerView, DataFeedManagerView>();
            Container.RegisterType<IDataFeedManagerViewModel, DataFeedManagerViewModel>();
        }

        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<ShellView>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

        #endregion
    }
}

1 个答案:

答案 0 :(得分:5)

可能你需要手动设置区域管理器,在弹出视图的代码后面(构造函数),如下所示:

RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );

您必须为托管该区域的内容控件指定一个名称,然后以某种方式获取区域管理器(ServiceLocator.Current.GetInstance<IRegionManager>())。