Navigate to other page IocContainers and MVVM light
我正在用mvvm-light制作一个WindowsUniversal10应用程序。
但是现在,如果我单击
下面你可以找到我的代码。我使用ioccontainers,对于每个页面都使用一个viewmodel,并且只使用命令绑定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | IocContainerpublic class IocContainer { static IocContainer() { SimpleIoc.Default.Register<ApplicationViewModel>(false); SimpleIoc.Default.Register<ShowWeatherViewModel>(false); SimpleIoc.Default.Register<ShowWeatherPage>(false); SimpleIoc.Default.Register<ShowWeatherDetailPage>(false); SimpleIoc.Default.Register<ShowWeatherDetailViewModel>(false); } public static ShowWeatherPage ShowWeatherPage { get { return SimpleIoc.Default.GetInstance<ShowWeatherPage>(); } } public static ShowWeatherViewModel ShowWeatherViewModel { get { return SimpleIoc.Default.GetInstance<ShowWeatherViewModel>(); } } public static ApplicationViewModel ApplicationViewModel { get { return SimpleIoc.Default.GetInstance<ApplicationViewModel>(); } } public static ShowWeatherDetailPage ShowWeatherDetailPage { get { return SimpleIoc.Default.GetInstance<ShowWeatherDetailPage>(); } } public static ShowWeatherDetailViewModel ShowWeatherDetailViewModel { get { return SimpleIoc.Default.GetInstance<ShowWeatherDetailViewModel>(); } } } <hr/>View modelsApplicationViewModelpublic class ApplicationViewModel: ViewModelBaseClass { private Page _currentPage = IocContainer.ShowWeatherPage; public Page CurrentPage { get { return _currentPage; } set { if (_currentPage != value) { _currentPage = value; OnPropertyChanged(); } } } public void Navigate(Page page, object attribs) { CurrentPage = page; } } <hr/>ShowWeatherViewModelpublic class ShowWeatherViewModel: ViewModelBaseClass { #region variables private Item _selectedVillage = null; #endregion variables #region properties public Item SelectedVillage { get { return _selectedVillage; } set { if (_selectedVillage != value) { _selectedVillage = value; ShowDetailPage(); } } } #endregion properties #region constructor public ShowWeatherViewModel() { } #endregion constructor #region methodes private void ShowDetailPage() { ApplicationViewModel appVm = new ApplicationViewModel(); appVm.Navigate(IocContainer.ShowWeatherPage, SelectedVillage); } #endregion methodes } <hr/>ShowWeatherDetailViewModelpublic class ShowWeatherDetailViewModel: ViewModelBaseClass { } <hr/>ViewModelBaseClasspublic class ViewModelBaseClass: INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName ="") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } <hr/>PagesMainPage<Page x:Class="BALaboVoorbeeld.UWP.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BALaboVoorbeeld.UWP" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" DataContext="{Binding Source={StaticResource ioc}, Path=ApplicationViewModel}" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page Content="{Binding CurrentPage, Mode=TwoWay}" /> </Grid> </Page> <hr/>ShowWeatherPage<Page x:Class="BALaboVoorbeeld.UWP.Pages.ShowWeatherPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BALaboVoorbeeld.UWP.Pages" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" DataContext="{Binding Source={StaticResource ioc}, Path=ShowWeatherViewModel}" mc:Ignorable="d" Width="450"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="240" /> <ColumnDefinition Width="60" /> <ColumnDefinition Width="1*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="1" /> <RowDefinition Height="40" /> <RowDefinition Height="1*" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <TextBlock Text="Village:" HorizontalAlignment="Right" Margin="4" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" /> <Button HorizontalAlignment="Stretch" Margin="4" VerticalAlignment="Center" Grid.Row="1" Grid.Column="2" Command="{Binding ShowWeahter}"> <SymbolIcon Symbol="Find" /> </Button> <ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" ItemContainerStyle="{StaticResource lstidflt}" SelectedItem="{Binding SelectedVillage, Mode=TwoWay}" ItemTemplate="{StaticResource weatheritemdt}" ItemsSource="{Binding VillageList}" /> </Grid> </Page> <hr/>ShowWeatherDetailPage<Page x:Class="BALaboVoorbeeld.UWP.Pages.ShowWeatherDetailPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BALaboVoorbeeld.UWP.Pages" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="Yes we did it ?"/> </Grid> </Page> |
您可以使用MVVM Light的导航服务导航到另一个视图。
网址:http://www.mvmlight.net/doc/nav1.cshtml
https://marcominerva.wordpress.com/2014/10/10/navigationservice-in-mvvvm-light-v5/