16.02 Xamarin Custom user controls for the First View (Xamarin) - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki
- Preparation steps to first run Dm04WebApp
- Set up default constructor of LitDbContext
public LitDbContext() : base("name=LitConnection") { }
- Set up connection strings in the Web.config
<add name="DefaultConnection"
connectionString="Data Source=SVR2016SQL2017;Initial Catalog=AspNetDbWebApplicationXamarinDemoSecurity;Persist Security Info=True;User ID=sa;Password=YOUR_PASSWORD"
providerName="System.Data.SqlClient" />
<add name="LitConnection"
connectionString="Data Source=SVR2016SQL2017;Initial Catalog=LitStorageDm04WebApp;Persist Security Info=True;User ID=sa;Password=YOUR_PASSWORD"
providerName="System.Data.SqlClient" />
- Db Migration is not used for this demo app. Instead the following code have been executed in the small console app to create Data Base on the SQL server
Database.SetInitializer(new DropCreateDatabaseAlways<LitDbContext>());
LitDbContext db = new LitDbContext();
db.LitGenreDbSet.FirstOrDefault();
MessageBox.Show("The database was successfully created.", "Done",
MessageBoxButton.OK, MessageBoxImage.Information);
Database.SetInitializer(new CreateDatabaseIfNotExists<LitDbContext>());
-
To Generate C# classes for the first View
- Run Visual Studio and Open “WpfDemo” solution
- Under ModelInterfacesClassLibrary-project create folder
- Literature/LitGenre
- Right Click the folder
- Literature/LitGenre
- And select “Wpf Forms Wizard” menu item to open the Wizard dialog
-
Note:
- On the first page of the dialog the destination folder is shown. The destination folder is the folder in which the generated file will be saved.
- Click “Next”-button
- On the second page of the dialog the developer should select existing DbContext file. Select:
- Dm02Context (project)
- LitDbContext (context)
- Click “Next”-button
- On the third page of the dialog the developer should select the View. Select:
- “LitGenreView“.
- In the left panel select “UI List Properties” node and check Shown for both:
- GenreId
- GenreName
- In the left panel of the same page of the dialog select “UI Form Properties” node and check Shown for both properties. Set “InputType” as it is shown on the slide.
- Click “Next”-button
- On the fourth page click “batch processing”-button. “Batch actions” dialog will be shown.
- Select “01100-Intefaces.json”
- Click “Start”-button
- Close both dialogs
- Under ModelServicesPrismModule-project create folder
- Literature/LitGenre
- Right Click the folder
- Literature/LitGenre
- And select “Wpf Forms Wizard” menu item to open the Wizard dialog
- Repeat all the steps until the “Batch Actions” dialog box appears.
- Note:
- The developer should not define again
- “UI List Properties”
- “UI Form Properties”
- All settings were saved during the first batch.
- The developer should not define again
- In “Batch actions” dialog
- Select “01400-ApiService.json”
- Click “Start”-button
- Select “01420-SForm.Xamarin.json”
- Click “Start”-button
- Select “01500-Eform.Xamarin.json”
- Click “Start”-button
- Select “01600-Lform.Xamarin.json”
- Click “Start”-button
- Select “01700-O2m.Xamarin.json”
- Click “Start”-button
- Select “01701-O2mPage.Xamarin.json”
- Click “Start”-button
- Select “01820-Redit.Xamarin.json”
- Click “Start”-button
- Select “01821-ReditPage.Xamarin.json”
- Click “Start”-button
- Select “01920-Rlist.Xamarin.json”
- Click “Start”-button
- Select “01921-RlistPage.Xamarin.json”
- Click “Start”-button
- Select “02020-Rdlist.Xamarin.json”
- Click “Start”-button
- Select “02021-RdlistPage.Xamarin.json”
- Click “Start”-button
- Close both dialogs
- Select “01400-ApiService.json”
- Note #1:
- Sform is a search form for the given View
- Eform is an Edit form for the given View
- Lform is a wrapper for the two forms above.
- Note #2:
- Sform and Eform are the basic forms with all the user interface logic for the given View.
- Other forms are wrappers for these two:
- Derived from ContentView (viewmodel derives IRegionAware in terms of Prism). Such a controls are used for navigation inside small child window, i.e. inside separate child windows of so called "Feature"-window:
- Lform
- O2m
- Redit
- Rlist
- Rdlist
- Derived from ContentPage (viewmodel derives INavigationAware in terms of Prism):
- O2mPage
- ReditPage
- RlistPage
- RdlistPage
- Derived from ContentView (viewmodel derives IRegionAware in terms of Prism). Such a controls are used for navigation inside small child window, i.e. inside separate child windows of so called "Feature"-window:
- Under ModelServicesPrismModule-project
- open the file "Literature/LitGenre/LitGenreViewService.cs" and take a look at the instruction at the beginning of the file. Do not follow the instructions:
/*
In the file of IModule-class of the project for the current service the following lines of code must be inserted:
public void RegisterTypes(IContainerRegistry containerRegistry)
{
...
containerRegistry.Register<ILitGenreViewService, LitGenreViewService>();
...
}
*/
- Under ModelServicesPrismModule-project
- open the file "Literature/LitGenre/ViewModels/LitGenreViewSformViewModel.cs" and take a look at the instruction at the beginning of the file
- open the file "Literature/LitGenre/ViewModels/LitGenreViewSdlgViewModel.cs" and take a look at the instruction at the beginning of the file
- open the file "Literature/LitGenre/ViewModels/LitGenreViewEformViewModel.cs" and take a look at the instruction at the beginning of the file
- open the file "Literature/LitGenre/ViewModels/LitGenreViewEdlgViewModel.cs" and take a look at the instruction at the beginning of the file
- open the file "Literature/LitGenre/ViewModels/LitGenreViewLformViewModel.cs" and take a look at the instruction at the beginning of the file
- open the file "Literature/LitGenre/ViewModels/LitGenreViewLdlgViewModel.cs" and take a look at the instruction at the beginning of the file
- continue until the end
- Under ModelServicesPrismModule-project check that the RegisterTypes()-method of the ModelServicesPrismModuleModule.cs becomes as follows
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<ModelInterfacesClassLibrary.Literature.LitGenre.ILitGenreViewService, ModelServicesPrismModule.Literature.LitGenre.LitGenreViewService>();
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewSformUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewSformViewModel>();
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewSformUserControl>("LitGenreViewSformUserControl");
containerRegistry.RegisterDialog<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewSdlgUserControl, CommonUserControlLibrary.ViewModels.SdlgViewModelBase>("LitGenreViewSdlgViewModel");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewEformUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewEformViewModel>();
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewEformUserControl>("LitGenreViewEformUserControl");
containerRegistry.RegisterDialog<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewEdlgUserControl, CommonUserControlLibrary.ViewModels.EdlgViewModelBase>("LitGenreViewEdlgViewModel");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewLformUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewLformViewModel>();
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewLformUserControl>("LitGenreViewLformUserControl");
containerRegistry.RegisterDialog<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewLdlgUserControl, CommonUserControlLibrary.ViewModels.LdlgViewModelBase>("LitGenreViewLdlgViewModel");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewO2mUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewO2mViewModel>();
containerRegistry.RegisterForRegionNavigation<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewO2mUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewO2mViewModel>("LitGenreViewO2mUserControl");
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewO2mUserControl>("LitGenreViewO2mUserControl");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewO2mPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewO2mPageViewModel>();
containerRegistry.RegisterForNavigation<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewO2mPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewO2mPageViewModel>("LitGenreViewO2mPage");
containerRegistry.Register<Xamarin.Forms.ContentPage, ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewO2mPage>("LitGenreViewO2mPage");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewReditUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewReditViewModel>();
containerRegistry.RegisterForRegionNavigation<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewReditUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewReditViewModel>("LitGenreViewReditUserControl");
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewReditUserControl>("LitGenreViewReditUserControl");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewReditPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewReditPageViewModel>();
containerRegistry.RegisterForNavigation<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewReditPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewReditPageViewModel>("LitGenreViewReditPage");
containerRegistry.Register<Xamarin.Forms.ContentPage, ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewReditPage>("LitGenreViewReditPage");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRlistUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRlistViewModel>();
containerRegistry.RegisterForRegionNavigation<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRlistUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRlistViewModel>("LitGenreViewRlistUserControl");
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRlistUserControl>("LitGenreViewRlistUserControl");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRlistPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRlistPageViewModel>();
containerRegistry.RegisterForNavigation<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRlistPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRlistPageViewModel>("LitGenreViewRlistPage");
containerRegistry.Register<Xamarin.Forms.ContentPage, ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRlistPage>("LitGenreViewRlistPage");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRdlistUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRdlistViewModel>();
containerRegistry.RegisterForRegionNavigation<ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRdlistUserControl, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRdlistViewModel>("LitGenreViewRdlistUserControl");
containerRegistry.Register<Xamarin.Forms.ContentView, ModelServicesPrismModule.Literature.LitGenre.UserControls.LitGenreViewRdlistUserControl>("LitGenreViewRdlistUserControl");
Prism.Mvvm.ViewModelLocationProvider.Register<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRdlistPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRdlistPageViewModel>();
containerRegistry.RegisterForNavigation<ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRdlistPage, ModelServicesPrismModule.Literature.LitGenre.ViewModels.LitGenreViewRdlistPageViewModel>("LitGenreViewRdlistPage");
containerRegistry.Register<Xamarin.Forms.ContentPage, ModelServicesPrismModule.Literature.LitGenre.Views.LitGenreViewRdlistPage>("LitGenreViewRdlistPage");
}
- Open "Views\MainFlyoutPage.xaml"-file of the PrismDemoApp-project
- Modify content of StackLayout as shown:
<StackLayout Padding="20" >
<Button BackgroundColor="Transparent" Text="Home" Command="{Binding NavigateCommand}" CommandParameter="HomePage" />
<Button BackgroundColor="Transparent" Text="Access Denied" Command="{Binding NavigateCommand}" CommandParameter="AccessDeniedPage" />
<Button BackgroundColor="Transparent" Text="Page Not Found" Command="{Binding NavigateCommand}" CommandParameter="PageNotFoundPage" />
<Button BackgroundColor="Transparent" Text="Register User" Command="{Binding NavigateCommand}" CommandParameter="RegisterUserPage" />
<Button BackgroundColor="Transparent" Text="Login User" Command="{Binding NavigateCommand}" CommandParameter="LoginUserPage" />
<Button BackgroundColor="Transparent" Text="Logout User" Command="{Binding NavigateCommand}" CommandParameter="LogoutUserPage" />
<Button BackgroundColor="Transparent" Text="Change Password" Command="{Binding NavigateCommand}" CommandParameter="ChngpswdUserPage" />
<Button BackgroundColor="Transparent" Text="Show Dialog" Command="{Binding ShowDialogCommand}" CommandParameter="MessageDlg" />
<Label Text="Genre"/>
<Button BackgroundColor="Transparent" Text="L Dialog" Command="{Binding ShowDialogCommand}" CommandParameter="LitGenreViewLdlgViewModel" />
<Button BackgroundColor="Transparent" Text="R Page" Command="{Binding NavigateCommand}" CommandParameter="LitGenreViewRlistPage" />
<Button BackgroundColor="Transparent" Text="Rd Page" Command="{Binding NavigateCommand}" CommandParameter="LitGenreViewRdlistPage" />
<Button BackgroundColor="Transparent" Text="O2m Page" Command="{Binding NavigateCommand}" CommandParameter="LitGenreViewO2mPage" />
</StackLayout>
- rebuild
- PrismDemoApp
- run PrismDemoApp
- In the app click "R Page"-menu item of "Genre"-block and add a pair of records
or UWP