区域管理器无法在自定义弹出窗口中找到区域
Region Manager can not find region inside of the custom popupwindow
我有一个 ShellView 使用交互对象与 prism 的 PopupWindowAction 来显示我的自定义设置视图。我的 ShellViewModel 包含 InteractionRequest 对象和一个将触发用户交互的委托命令。用户触发交互后,自定义设置视图 (DataFeedManagerView) 出现在 ShellView 的中心。在我的 DataFeedManagerView 中,左侧有一个 DataFeed 列表(ListBox 控件),右侧有数据馈送特定设置视图(通过 RegionManager 设置区域的 ContentControl)。首先,我使用 RegisterViewWithRegion 注册了所有视图。然后我尝试做的是通过 Region 的 Activate 方法激活内容控件中的相关对象设置视图。当我尝试这样做时,我收到一个错误 "can not find region"。所以我们不能在自定义弹出窗口中使用区域 window???
PS1:也许这个要求很简单,但是步骤很多,所以我的解释有点复杂。我希望代码会更具描述性。
PS2:我使用简单绑定到 ContentControl 的内容达到了预期 属性。但我担心我的错误 and/or 在自定义交互弹出窗口中使用区域的正确解决方案 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);
}
}
}
..::引导程序::..
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
}
}
可能你需要手动设置区域管理器,在弹出视图的代码后面(构造函数),像这样:
RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );
您必须为托管该区域的内容控件指定一个名称,并以某种方式获取区域管理器 (ServiceLocator.Current.GetInstance<IRegionManager>()
)。
我有一个 ShellView 使用交互对象与 prism 的 PopupWindowAction 来显示我的自定义设置视图。我的 ShellViewModel 包含 InteractionRequest 对象和一个将触发用户交互的委托命令。用户触发交互后,自定义设置视图 (DataFeedManagerView) 出现在 ShellView 的中心。在我的 DataFeedManagerView 中,左侧有一个 DataFeed 列表(ListBox 控件),右侧有数据馈送特定设置视图(通过 RegionManager 设置区域的 ContentControl)。首先,我使用 RegisterViewWithRegion 注册了所有视图。然后我尝试做的是通过 Region 的 Activate 方法激活内容控件中的相关对象设置视图。当我尝试这样做时,我收到一个错误 "can not find region"。所以我们不能在自定义弹出窗口中使用区域 window???
PS1:也许这个要求很简单,但是步骤很多,所以我的解释有点复杂。我希望代码会更具描述性。
PS2:我使用简单绑定到 ContentControl 的内容达到了预期 属性。但我担心我的错误 and/or 在自定义交互弹出窗口中使用区域的正确解决方案 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);
}
}
}
..::引导程序::..
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
}
}
可能你需要手动设置区域管理器,在弹出视图的代码后面(构造函数),像这样:
RegionManager.SetRegionName( theNameOfTheContentControlInsideThePopup, WellKnownRegionNames.DataFeedRegion );
RegionManager.SetRegionManager( theNameOfTheContentControlInsideThePopup, theRegionManagerInstanceFromUnity );
您必须为托管该区域的内容控件指定一个名称,并以某种方式获取区域管理器 (ServiceLocator.Current.GetInstance<IRegionManager>()
)。