Viewを内部に持つViewを複数生成したいです。
ListBoxの数値を選択すると、選択された数値を受け取ったViewを表示するプログラムを作っています。
以前表示したViewは残して、異なる数値が選択された場合、新しくViewを生成するようにしたいです。
Viewの内部にViewを持っていると1→2に変えたときに下記のエラーが出ます。
ArgumentException: Region with the given name is already registered: ViewBRegion
WPF
.NET Core 3
Prism.Unity(7.2.0.1367)
ReactiveProperty(6.1.4)
<フォルダ構成>
ViewModelsフォルダ
- MainWindowViewModel.cs
- ViewAViewModel.cs
- ViewBViewModel.cs
Viewsフォルダ
- UserControls/ViewA.xaml
- UserControls/ViewB.xaml
- MainWindow.xaml
App.xaml
MainWindowViewModel.cs
C#
1using Prism.Mvvm; 2using Prism.Regions; 3using Reactive.Bindings; 4using System; 5using System.Collections.Generic; 6using Test.Views.UserControls; 7using Unity; 8 9namespace Test.ViewModels 10{ 11 public class MainWindowViewModel : BindableBase 12 { 13 [Dependency] 14 public IRegionManager RegionManager { get; set; } 15 public List<int> NumberList { get; private set; } 16 public ReactiveProperty<int> SelectedNumber { get; set; } 17 public MainWindowViewModel() 18 { 19 this.NumberList = new List<int> { 1, 2, 3 }; 20 this.SelectedNumber = new ReactiveProperty<int>(); 21 22 this.SelectedNumber.Subscribe(_=>this.ChangeView()) ; 23 } 24 25 private void ChangeView() 26 { 27 if(RegionManager != null) 28 { 29 this.RegionManager.RequestNavigate("ContentRegion", nameof(ViewA), new NavigationParameters($"id={this.SelectedNumber.Value}")); 30 } 31 } 32 } 33}
ViewAViewModel.cs
C#
1using Prism.Commands; 2using Prism.Mvvm; 3using Prism.Regions; 4using Reactive.Bindings; 5using System; 6using System.Collections.Generic; 7using System.Linq; 8 9namespace Test.ViewModels 10{ 11 public class ViewAViewModel : BindableBase, INavigationAware 12 { 13 public ReactiveProperty<string> Id { get; private set; } 14 public ViewAViewModel() 15 { 16 Id = new ReactiveProperty<string>(); 17 } 18 19 public bool IsNavigationTarget(NavigationContext navigationContext) 20 { 21 return this.Id.Value == navigationContext.Parameters["id"] as string; 22 } 23 24 public void OnNavigatedFrom(NavigationContext navigationContext) 25 { 26 27 } 28 29 public void OnNavigatedTo(NavigationContext navigationContext) 30 { 31 this.Id.Value = navigationContext.Parameters["id"] as string; 32 } 33 } 34}
ViewBViewModel.cs
C#
1using Prism.Commands; 2using Prism.Mvvm; 3using System; 4using System.Collections.Generic; 5using System.Linq; 6 7namespace Test.ViewModels 8{ 9 public class ViewBViewModel : BindableBase 10 { 11 public ViewBViewModel() 12 { 13 14 } 15 } 16}
MainWindow.xaml
xaml
1<Window x:Class="Test.Views.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 Title="{Binding Title}" Height="350" Width="525" > 7 <StackPanel> 8 <ListBox ItemsSource="{Binding NumberList}" SelectedItem="{Binding SelectedNumber.Value}"> 9 <ListBox.ItemTemplate> 10 <DataTemplate> 11 <TextBlock Text="{Binding}"/> 12 </DataTemplate> 13 </ListBox.ItemTemplate> 14 <ListBox.ItemsPanel> 15 <ItemsPanelTemplate> 16 <StackPanel Orientation="Horizontal"/> 17 </ItemsPanelTemplate> 18 </ListBox.ItemsPanel> 19 </ListBox> 20 <ContentControl prism:RegionManager.RegionName="ContentRegion" /> 21 </StackPanel> 22</Window>
ViewA.xaml
xaml
1<UserControl x:Class="Test.Views.UserControls.ViewA" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 Loaded="UserControl_Loaded"> 7 <StackPanel> 8 <TextBlock Text="{Binding Id.Value}"/> 9 <ContentControl prism:RegionManager.RegionName="ViewBRegion" /> 10 </StackPanel> 11</UserControl>
ViewA.xaml.cs
C#
1using Prism.Ioc; 2using Prism.Regions; 3using System.Windows.Controls; 4using Unity; 5 6namespace Test.Views.UserControls 7{ 8 /// <summary> 9 /// Interaction logic for ViewA 10 /// </summary> 11 public partial class ViewA : UserControl 12 { 13 [Dependency] 14 public IContainerExtension ContainerExtension { get; set; } 15 [Dependency] 16 public IRegionManager RegionManager { get; set; } 17 public ViewA() 18 { 19 InitializeComponent(); 20 } 21 22 private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e) 23 { 24 RegionManager.AddToRegion("ViewBRegion", ContainerExtension.Resolve<ViewB>()); 25 26 } 27 } 28}
ViewB.xaml
xaml
1<UserControl x:Class="Test.Views.UserControls.ViewB" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True"> 6 <Grid> 7 <TextBlock Text="ViewB"/> 8 </Grid> 9</UserControl>
app.xaml.cs
C#
1using Prism.Ioc; 2using Test.Views; 3using System.Windows; 4using Test.Views.UserControls; 5using Prism.Mvvm; 6using Test.ViewModels; 7 8namespace Test 9{ 10 /// <summary> 11 /// Interaction logic for App.xaml 12 /// </summary> 13 public partial class App 14 { 15 protected override Window CreateShell() 16 { 17 return Container.Resolve<MainWindow>(); 18 } 19 20 protected override void RegisterTypes(IContainerRegistry containerRegistry) 21 { 22 containerRegistry.RegisterForNavigation<ViewA>(); 23 } 24 protected override void ConfigureViewModelLocator() 25 { 26 base.ConfigureViewModelLocator(); 27 ViewModelLocationProvider.Register<ViewA, ViewAViewModel>(); 28 ViewModelLocationProvider.Register<ViewB, ViewBViewModel>(); 29 30 } 31 } 32} 33
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/11 07:44 編集
2019/12/11 07:52
2019/12/11 13:21