Collections are a very important concept used in Universal Windows Platform (UWP) development to efficiently manage and display data. In this article, we will explain the basic concept of Collections in UWP development, the main collection types used, and provide example code utilizing these collections.
1. Basic Concept of Collections
Collections are data structures that allow multiple objects to be grouped and managed as a single unit. In UWP, classes like ObservableCollection<T>
, List<T>
, and Dictionary<TKey, TValue>
are primarily used to implement collections. These collections are very useful for dynamically updating the user interface through data binding with the UI.
2. Main Collection Classes
2.1 ObservableCollection<T>
ObservableCollection<T>
is a collection used to automatically notify the UI of changes in data. This collection ensures that the UI is updated automatically when items are added or removed, making it commonly used in UWP applications that follow the MVVM pattern.
2.2 List<T>
List<T>
is the most basic collection that stores a set of elements. While it is flexible and efficient, it does not directly support data binding as it does not synchronize with the UI. It is mainly used when complex data processing is required.
2.3 Dictionary<TKey, TValue>
Dictionary<TKey, TValue>
represents a set of key-value pairs and is useful when data retrieval is needed. This collection allows quick value retrieval via its keys.
3. Example: A Simple Todo List Application Using ObservableCollection
In this section, we will create a simple Todo List application using ObservableCollection<T>
. This application allows users to add and delete tasks.
3.1 Data Model
public class TodoItem { public string Title { get; set; } public bool IsCompleted { get; set; } }
3.2 ViewModel Setup
using System.Collections.ObjectModel; using System.ComponentModel; public class TodoViewModel : INotifyPropertyChanged { public ObservableCollection<TodoItem> Todos { get; set; } public TodoViewModel() { Todos = new ObservableCollection<TodoItem>(); } public void AddTodo(string title) { Todos.Add(new TodoItem { Title = title, IsCompleted = false }); OnPropertyChanged("Todos"); } public void RemoveTodo(TodoItem todo) { Todos.Remove(todo); OnPropertyChanged("Todos"); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }
3.3 XAML UI Configuration
Below is a XAML example that binds the previously defined TodoViewModel
to configure the UI.
<Page x:Class="TodoList.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TodoList"> <Grid> <StackPanel> <TextBox x:Name="TodoInput" Width="300" PlaceholderText="Enter your task" /> <Button Content="Add" Click="AddButton_Click" /> <ListBox ItemsSource="{x:Bind ViewModel.Todos, Mode=OneWay}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsCompleted}"></CheckBox> <TextBlock Text="{Binding Title}" /> <Button Content="Delete" Click="RemoveButton_Click" Tag="{Binding}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Grid> </Page>
3.4 Code-behind: Button Click Events
Implement button click event handlers to allow users to add or delete tasks.
public sealed partial class MainPage : Page { public TodoViewModel ViewModel { get; set; } public MainPage() { this.InitializeComponent(); ViewModel = new TodoViewModel(); DataContext = ViewModel; } private void AddButton_Click(object sender, RoutedEventArgs e) { var todoTitle = TodoInput.Text; if (!string.IsNullOrEmpty(todoTitle)) { ViewModel.AddTodo(todoTitle); TodoInput.Text = string.Empty; } } private void RemoveButton_Click(object sender, RoutedEventArgs e) { var button = (Button)sender; var todoToRemove = (TodoItem)button.Tag; ViewModel.RemoveTodo(todoToRemove); } }
4. Performance Optimization of Collections
When using collections in UWP, it’s also important to optimize performance. Particularly, in cases with large amounts of data or frequent updates, performance can be improved using the following methods.
4.1 Using Virtualization
Item templates like ListView
or GridView
provide virtualization internally, loading only the items displayed on the screen into memory at once. This feature can be used to efficiently display large amounts of data.
4.2 Utilizing CollectionChanged Event
ObservableCollection<T>
uses the CollectionChanged
event to notify the UI about changes in the collection. Thus, UI update optimization should be considered during large data changes.
5. Conclusion
Collections are essential for data management and UI updates in UWP development. Utilizing ObservableCollection<T>
allows for immediate UI reactions to data changes, enabling effective structuring of applications through the MVVM pattern. It is hoped that utilizing Collections in UWP applications will provide a more engaging user experience.