gpt4 book ai didi

prism - 区域管理器在自定义弹出窗口内找不到区域

转载 作者:行者123 更新时间:2023-12-05 00:53:32 25 4
gpt4 key购买 nike

我有一个 ShellView,它使用带有 prism 的 PopupWindowAction 的交互对象来显示我的自定义设置 View 。我的 ShellViewModel 包含 InteractionRequest 对象和一个将触发用户交互的委托(delegate)命令。用户触发交互后,自定义设置 View (DataFeedManagerView)出现在 ShellView 的中心。在 My DataFeedManagerView 中,左侧有一个 DataFeeds 列表(ListBox 控件),右侧有特定于数据馈送的设置 View (ContentControl with set Region via RegionManager)。首先,我使用 RegisterViewWithRegion 注册了所有 View 。然后我尝试做的是通过 Region 的 Activate 方法激活内容控件中的相关对象设置 View 。当我尝试这样做时,我收到一个错误“找不到区域”。所以我们不能在自定义弹出窗口中使用区域???

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

PS2:我使用简单绑定(bind)到 ContentControl 的 content 属性达到了预期。但我担心我的错误和/或在自定义交互弹出窗口中使用区域的正确解决方案是什么。

..::贝壳::..

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

..:: Bootstrap ::..
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
}
}

最佳答案

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

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

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

关于prism - 区域管理器在自定义弹出窗口内找不到区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com