UPDATED LIST VIEW XAML in Visual Basic .NET

Printing Quick Response Code in Visual Basic .NET UPDATED LIST VIEW XAML

UPDATED LIST VIEW XAML
Create Denso QR Bar Code In Visual Basic .NET
Using Barcode encoder for .NET Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
QR-Code Recognizer In Visual Basic .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
The following code includes the updates to the view to bind the DataGrid to the Employees collection and the SelectedEmployee property:
Universal Product Code Version A Drawer In VB.NET
Using Barcode generation for VS .NET Control to generate, create UPC Code image in .NET framework applications.
www.OnBarcode.com
Linear Creator In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create Linear 1D Barcode image in VS .NET applications.
www.OnBarcode.com
<sdk:DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" Margin="12,55,119,12" x:Name="EmployeesGrid" />
Paint QR-Code In Visual Basic .NET
Using Barcode maker for .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Painting Barcode In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
Encoding GTIN - 13 In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create EAN / UCC - 13 image in .NET applications.
www.OnBarcode.com
Printing Identcode In VB.NET
Using Barcode generation for VS .NET Control to generate, create Identcode image in VS .NET applications.
www.OnBarcode.com
Structuring and testing with the MVVM/ViewModel pattern
Print QR Code In Java
Using Barcode creator for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
Painting QR Code JIS X 0510 In None
Using Barcode creation for Software Control to generate, create QR Code image in Software applications.
www.OnBarcode.com
The updates to the DataGrid element involved first setting the ItemsSource to the Employees collection on the ViewModel and then binding the SelectedItem to the SelectedEmployee property of the ViewModel. Note that the binding on SelectedEmployee is TwoWay, so both the DataGrid and code may update this value.
Scanning QR-Code In Visual C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
EAN128 Generation In Java
Using Barcode generator for Android Control to generate, create UCC.EAN - 128 image in Android applications.
www.OnBarcode.com
UPDATED LIST VIEW CODE-BEHIND
Making Barcode In .NET
Using Barcode generator for VS .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Generating UPC Code In Java
Using Barcode printer for Eclipse BIRT Control to generate, create UPC Code image in BIRT applications.
www.OnBarcode.com
With the addition of the ViewModel and the changes to the XAML, you need to make some changes to the code-behind for the EmployeeList page. Listing 16.19 includes the new code-behind with those changes included.
Printing Barcode In None
Using Barcode generation for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com
Scan PDF 417 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Listing 16.9 EmployeeList view code-behind
EAN-13 Drawer In None
Using Barcode drawer for Software Control to generate, create EAN / UCC - 13 image in Software applications.
www.OnBarcode.com
Scan Code-39 In VS .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
public partial class EmployeeList : Page { public EmployeeList() { InitializeComponent(); NavigationCacheMode = NavigationCacheMode.Enabled; AddMoreVacation.Click += new RoutedEventHandler(AddMoreVacation_Click); EditEmployee.Click += new RoutedEventHandler(EditEmployee_Click); } private EmployeeListViewModel _viewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { if (_viewModel == null) { _viewModel = new EmployeeListViewModel(); _viewModel.EmployeesLoaded += (s, ea) => { LoadingProgress.Visibility = Visibility.Collapsed; };
Barcode Reader In Java
Using Barcode Control SDK for BIRT Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Code 39 Recognizer In VB.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Cache this Page
DataContext = _viewModel; LoadingProgress.Visibility = Visibility.Visible; _viewModel.LoadEmployees(); } } private EmployeeDetail _employeeDetail = new EmployeeDetail(); void EditEmployee_Click(object sender, RoutedEventArgs e) { _employeeDetail.Employee = _viewModel.SelectedEmployee; _employeeDetail.Show(); } void AddMoreVacation_Click(object sender, RoutedEventArgs e) { _viewModel.AddVacationBonusToSelectedEmployee(); } }
Download from Wow! eBook <www.wowebook.com>
Factoring out reusable code
The OnNavigatedTo function B now includes code to create the ViewModel C if it s not already present, and to call the LoadEmployees method D on the ViewModel. In addition, the event handler for the Add More Vacation! button now calls directly into the ViewModel to execute the code E. Now you have the same functionality as the code-behind solution, but with quite a bit more code. In fact, you have just as much code-behind as you did in the codebehind-only solution! Keep in mind, this is just the first layer of the onion, so you haven t received all the benefits of MVVM yet. What you have gained is subtle:
The DataGrid is now divorced from the code-behind, using solely binding to get its items and synchronize the selected item. A designer could now change the DataGrid to be a ListBox or some other type of control if desired, and the code wouldn t need to change. The data access (service call) is now removed from the page, giving you the potential to substitute a different type of service call without making any changes to the page code. You ll find an even better spot for it later in this chapter. The business logic to add the vacation bonus to the selected employee is now pulled out of the page. Like the service call, you can do better, and you will later in this chapter. The code-behind is no longer manipulating the Employee type directly. This makes it easier to replace the Employee type later, should you want to do so.
At this point, you have a ViewModel that s essentially the code-behind for the View. You ve taken your first steps into the MVVM pattern and away from packing all your code in the code-behind. To build on this, it d be nice if you could refactor to take advantage of some best practices associated with the ViewModel pattern and with coding in general. In the next section, we ll dive deeper into the pattern and show how to factor out common code such as service access and business rules.
16.3 Factoring out reusable code
The Single Responsibility Principle (SRP) states (surprisingly enough) that every object should have a single responsibility,2 and that every object should have one and only one reason to change. I don t try to adhere to this as though it were dogma but rather make informed decisions based on this principle representing the perfect state. SRP can sometimes be difficult to apply to something as fa ade-like as a ViewModel class, but it s obvious we didn t even try here. The ViewModel class for the list page is responsible for tracking page state, calling the web service to load data, and applying a vacation bonus to selected employees. If the vacation bonus changes, this class must also change. If the service access changes, this class must change. You need to do something about that.
Robert C. Martin, Principles of OOD, http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod (May 11, 2005).
Copyright © OnBarcode.com . All rights reserved.