前提・実現したいこと
年月日を入力するModuleをRegionで一つの画面に二つ表示したときにそれぞれ別のインスタンスを注入したい
発生している問題・エラーメッセージ
Gridで表示している
prism:RegionManager.RegionName="DateTime"と
prism:RegionManager.RegionName="DateTime2"に
それぞれ別のインスタンスを渡したいのだが
InfoViewModelで注入された二つの引数が同一のものになっているため
別々の表示をすることができない
DataTimeとDataTime2でそれぞれ別のRegionとして登録しているためそこからアクセスする方法、または何とか別のインスタンスを注入する方法はないか
<ContentControl Grid.Column = "1" Grid.Row = "4" prism:RegionManager.RegionName="DateTime" Margin="5,0,5,0" VerticalAlignment="Top"/> <TextBox Grid.Column = "1" Grid.Row = "5" Margin="5,0,5,0"/> <TextBlock Grid.Column = "2" Grid.Row = "0" Text=""/> <TextBox Grid.Column = "3" Grid.Row = "0" Margin="5,0,5,0"/> <ContentControl Grid.Column = "3" Grid.Row = "4" prism:RegionManager.RegionName="DateTime2" Margin="5,0,5,0" VerticalAlignment="Top"/>
該当のソースコード
Info.xmal
1 <Grid> 2 <Grid.ColumnDefinitions> 3 <ColumnDefinition Width="Auto"/> 4 <ColumnDefinition Width="1*"/> 5 <ColumnDefinition Width="Auto"/> 6 <ColumnDefinition Width="1*"/> 7 </Grid.ColumnDefinitions> 8 <Grid.RowDefinitions> 9 <RowDefinition Height="1*"/> 10 <RowDefinition Height="1*"/> 11 <RowDefinition Height="1*"/> 12 <RowDefinition Height="1*"/> 13 <RowDefinition Height="1*"/> 14 <RowDefinition Height="1*"/> 15 </Grid.RowDefinitions> 16 <TextBlock Grid.Column = "0" Grid.Row = "0" Text=""/> 17 <TextBlock Grid.Column = "0" Grid.Row = "1" Text=""/> 18 <TextBlock Grid.Column = "0" Grid.Row = "2" Text=""/> 19 <TextBlock Grid.Column = "0" Grid.Row = "3" Text=""/> 20 <TextBlock Grid.Column = "0" Grid.Row = "4" Text=""/> 21 <TextBlock Grid.Column = "0" Grid.Row = "5" Text=""/> 22 <TextBox Grid.Column = "1" Grid.Row = "0" Margin="5,0,5,0"/> 23 <TextBox Grid.Column = "1" Grid.Row = "1" Margin="5,0,5,0"/> 24 <TextBox Grid.Column = "1" Grid.Row = "2" Margin="5,0,5,0"/> 25 <TextBox Grid.Column = "1" Grid.Row = "3" Margin="5,0,5,0"/> 26 <ContentControl Grid.Column = "1" Grid.Row = "4" prism:RegionManager.RegionName="DateTime" Margin="5,0,5,0" VerticalAlignment="Top"/> 27 <TextBox Grid.Column = "1" Grid.Row = "5" Margin="5,0,5,0"/> 28 <TextBlock Grid.Column = "2" Grid.Row = "0" Text=""/> 29 <TextBox Grid.Column = "3" Grid.Row = "0" Margin="5,0,5,0"/> 30 <ContentControl Grid.Column = "3" Grid.Row = "4" prism:RegionManager.RegionName="DateTime2" Margin="5,0,5,0" VerticalAlignment="Top"/> 31 32 </Grid>
public class InfoViewModel : BindableBase { IDateTime DateTime; IDateTime DateTime2; public DrInfoViewModel(IDateTime dataTime, IDateTime dataTime2) { DateTime = dataTime; DateTime2 = dataTime2; // dataTimeもdataTime2が共有されたインスタンスになっている } }
public class InfoModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionMan = containerProvider.Resolve<IRegionManager>(); regionMan.RegisterViewWithRegion("DrInfo", typeof(Views.DrInfoView)); } public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterInstance<IDateTime>(new DateTimeModel()); } }
public class DateTimeViewModel : BindableBase { IDateTime DateTime; public int YearValue { get; set; } public int MonthValue { get; set; } public int DayValue { get; set; } public List<int> Years { get; set; } = new List<int>(); public List<int> Months { get; set; } = new List<int>(); public List<int> Days { get; set; } = new List<int>(); public DateTimeViewModel(IDateTime dateTime) { DateTime = dateTime; for (var i = System.DateTime.Now.Year - 20; i <= System.DateTime.Now.Year; i++) { Years.Add(i); } Years = Years.OrderByDescending(x => x).ToList(); YearValue = dateTime.Years; for (var i = 1; i <= 12; i++) { Months.Add(i); } MonthValue = dateTime.Months; for (var i = 1; i <= 31; i++) { Days.Add(i); } DayValue = dateTime.Days; } } public interface IDateTime { public int Years { get; set; } public int Months { get; set; } public int Days { get; set; } } public class DateTimeModel : IDateTime { public int Years { get; set; } = System.DateTime.Now.Year; public int Months { get; set; } = System.DateTime.Now.Month; public int Days { get; set; } = System.DateTime.Now.Day; }
public class DateTimeModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionMan = containerProvider.Resolve<IRegionManager>(); regionMan.RegisterViewWithRegion("DateTime", typeof(Views.DateTimeView)); regionMan.RegisterViewWithRegion("DateTime2", typeof(Views.DateTimeView)); } public void RegisterTypes(IContainerRegistry containerRegistry) { } }
試したこと
補足情報(FW/ツールのバージョンなど)
Prism.Unity 7.2
Prism.Wpf 7.2
回答1件
あなたの回答
tips
プレビュー