Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

prism - Region Manager can not find region inside of the custom popupwindow

I have a ShellView using interactivity objects with prism's PopupWindowAction for show my custom settings view. And my ShellViewModel contain InteractionRequest object and a Delegate Command that will fire the user interaction. After user fired interaction, custom settings view (DataFeedManagerView) appear center of ShellView. In My DataFeedManagerView, there is a list of DataFeeds (ListBox control) on the left side and there is datafeed specific settings view (ContentControl with set Region via RegionManager) on the right side. First, i registered all Views with RegisterViewWithRegion. Then what i try to do is activate related object settings view within content control via Region's Activate method. When i try to do like this, i'm receiving an error "can not find region". So we can not use regions inside custom popup window???

PS1: Maybe this is so simple requirement but containing many steps because of that my explanation was a little bit complicated. I hope code will be more descriptive.

PS2: I meet expectations with using simple binding to ContentControl's content property. But i'm worry about what is my mistake and/or right solution for use regions inside custom interaction popup window.

..::Shell::..

<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);
        }
    }
}

..::Bootstrapper::..

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
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably you need to set the region manager manually, in the popup view's code behind (constructor), like this:

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

You'll have to assign a name to the content control hosting the region and somehow acquire the region manager (ServiceLocator.Current.GetInstance<IRegionManager>()).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...