mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add starter and complete projects for ch 7
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Page
|
||||
x:Class="MyMediaCollection.Views.ItemDetailsPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:MyMediaCollection.Views"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Page.Resources>
|
||||
<Style x:Key="AttributeTitleStyle" TargetType="TextBlock">
|
||||
<Setter Property="HorizontalAlignment" Value="Right"/>
|
||||
<Setter Property="VerticalAlignment" Value="Center"/>
|
||||
</Style>
|
||||
<Style x:Key="AttributeValueStyle" TargetType="TextBox">
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch"/>
|
||||
<Setter Property="Margin" Value="8"/>
|
||||
</Style>
|
||||
<Style x:Key="AttributeComboxValueStyle" TargetType="ComboBox">
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch"/>
|
||||
<Setter Property="Margin" Value="8"/>
|
||||
</Style>
|
||||
</Page.Resources>
|
||||
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<SymbolIcon Symbol="Edit" Margin="8"/>
|
||||
<TextBlock Text="Item Details"
|
||||
Style="{StaticResource SubheaderTextBlockStyle}"
|
||||
Margin="8"/>
|
||||
</StackPanel>
|
||||
|
||||
<Grid Grid.Row="1"
|
||||
BorderBrush="{ThemeResource SystemAccentColor}"
|
||||
BorderThickness="0,1,0,1"
|
||||
Margin="4,0,4,8">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="200"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Text="Name:" Style="{StaticResource AttributeTitleStyle}"/>
|
||||
<TextBox Grid.Column="1" Style="{StaticResource AttributeValueStyle}" Text="{x:Bind ViewModel.ItemName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
|
||||
<TextBlock Text="Media Type:" Grid.Row="1" Style="{StaticResource AttributeTitleStyle}"/>
|
||||
<ComboBox Grid.Row="1" Grid.Column="1" Style="{StaticResource AttributeComboxValueStyle}" ItemsSource="{x:Bind ViewModel.ItemTypes}" SelectedValue="{x:Bind ViewModel.SelectedItemType, Mode=TwoWay}"/>
|
||||
|
||||
<TextBlock Text="Medium:" Grid.Row="2" Style="{StaticResource AttributeTitleStyle}"/>
|
||||
<ComboBox Grid.Row="2" Grid.Column="1" Style="{StaticResource AttributeComboxValueStyle}" ItemsSource="{x:Bind ViewModel.Mediums}" SelectedValue="{x:Bind ViewModel.SelectedMedium, Mode=TwoWay}"/>
|
||||
|
||||
<TextBlock Text="Location:" Grid.Row="3" Style="{StaticResource AttributeTitleStyle}"/>
|
||||
<ComboBox Grid.Row="3" Grid.Column="1" Style="{StaticResource AttributeComboxValueStyle}" ItemsSource="{x:Bind ViewModel.LocationTypes}" SelectedValue="{x:Bind ViewModel.SelectedLocation, Mode=TwoWay}"/>
|
||||
</Grid>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Right">
|
||||
<SplitButton x:Name="SaveButton"
|
||||
Content="Save and Return"
|
||||
Margin="8,8,0,8"
|
||||
Click="{x:Bind ViewModel.SaveItemAndReturnAsync}"
|
||||
IsEnabled="{x:Bind ViewModel.IsDirty, Mode=OneWay}">
|
||||
<SplitButton.Flyout>
|
||||
<Flyout>
|
||||
<StackPanel>
|
||||
<Button Content="Save and Create New"
|
||||
Click="{x:Bind ViewModel.SaveItemAndContinueAsync}"
|
||||
IsEnabled="{x:Bind ViewModel.IsDirty, Mode=OneWay}"
|
||||
Background="Transparent"/>
|
||||
<Button Content="Save and Return"
|
||||
Click="{x:Bind ViewModel.SaveItemAndReturnAsync}"
|
||||
IsEnabled="{x:Bind ViewModel.IsDirty, Mode=OneWay}"
|
||||
Background="Transparent"/>
|
||||
</StackPanel>
|
||||
</Flyout>
|
||||
</SplitButton.Flyout>
|
||||
<SplitButton.Resources>
|
||||
<TeachingTip x:Name="SavingTip"
|
||||
Target="{x:Bind SaveButton}"
|
||||
Title="Save and create new"
|
||||
Subtitle="Use the dropdown button option to save your item and create another.">
|
||||
</TeachingTip>
|
||||
</SplitButton.Resources>
|
||||
</SplitButton>
|
||||
<Button Content="Cancel" Margin="8" Command="{x:Bind ViewModel.CancelCommand}"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Page>
|
||||
@@ -0,0 +1,60 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
using MyMediaCollection.ViewModels;
|
||||
|
||||
namespace MyMediaCollection.Views
|
||||
{
|
||||
/// <summary>
|
||||
/// An empty page that can be used on its own or navigated to within a Frame.
|
||||
/// </summary>
|
||||
public sealed partial class ItemDetailsPage : Page
|
||||
{
|
||||
public ItemDetailsPage()
|
||||
{
|
||||
ViewModel = App.HostContainer.Services.GetService<ItemDetailsViewModel>();
|
||||
|
||||
this.InitializeComponent();
|
||||
Loaded += ItemDetailsPage_Loaded;
|
||||
|
||||
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
|
||||
|
||||
// Load the user setting
|
||||
string haveExplainedSaveSetting = localSettings.Values[nameof(SavingTip)] as string;
|
||||
|
||||
// If the user has not seen the save tip, display it
|
||||
|
||||
if (!bool.TryParse(haveExplainedSaveSetting, out bool result) || !result)
|
||||
{
|
||||
SavingTip.IsOpen = true;
|
||||
|
||||
// Save the teaching tip setting
|
||||
localSettings.Values[nameof(SavingTip)] = "true";
|
||||
}
|
||||
}
|
||||
|
||||
private void ItemDetailsPage_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var mainWindow = (Application.Current as App)?.Window as MainWindow;
|
||||
if (mainWindow != null)
|
||||
{
|
||||
mainWindow.SetPageTitle("Item Details");
|
||||
}
|
||||
}
|
||||
|
||||
public ItemDetailsViewModel ViewModel;
|
||||
|
||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
||||
{
|
||||
base.OnNavigatedTo(e);
|
||||
|
||||
var itemId = (int)e.Parameter;
|
||||
|
||||
if (itemId > 0)
|
||||
{
|
||||
ViewModel.InitializeItemDetailData(itemId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Page
|
||||
x:Class="MyMediaCollection.Views.MainPage"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:MyMediaCollection.Views"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:model="using:MyMediaCollection.Model"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<SymbolIcon Symbol="Home" Margin="8"/>
|
||||
<TextBlock Text="Home" Style="{StaticResource SubheaderTextBlockStyle}" Margin="8"/>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
|
||||
<TextBlock Text="Media Type:" Margin="4" Style="{StaticResource SubtitleTextBlockStyle}" VerticalAlignment="Bottom"/>
|
||||
<ComboBox ItemsSource="{x:Bind ViewModel.Mediums, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.SelectedMedium, Mode=TwoWay}" MinWidth="120" Margin="0,2,6,4" VerticalAlignment="Bottom"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
<ListView Grid.Row="1" ItemsSource="{x:Bind ViewModel.Items}"
|
||||
SelectedItem="{x:Bind ViewModel.SelectedMediaItem, Mode=TwoWay}"
|
||||
DoubleTapped="{x:Bind ViewModel.ListViewDoubleTapped}">
|
||||
<ListView.HeaderTemplate>
|
||||
<DataTemplate>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Border BorderBrush="{ThemeResource SystemAccentColor}" BorderThickness="0,0,0,1">
|
||||
<TextBlock Text="Medium" Margin="4,0,0,0" Style="{StaticResource TitleTextBlockStyle}"/>
|
||||
</Border>
|
||||
<Border Grid.Column="1" BorderBrush="{ThemeResource SystemAccentColor}" BorderThickness="0,0,0,1">
|
||||
<TextBlock Text="Title" Margin="4,0,0,0" Style="{StaticResource TitleTextBlockStyle}"/>
|
||||
</Border>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListView.HeaderTemplate>
|
||||
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate x:DataType="model:MediaItem">
|
||||
<Grid IsHitTestVisible="False">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="120"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock Text="{x:Bind Path=MediumInfo.Name}"/>
|
||||
<TextBlock Grid.Column="1" Text="{x:Bind Path=Name}"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListView.ItemTemplate>
|
||||
</ListView>
|
||||
|
||||
<Border Grid.Row="2"
|
||||
BorderBrush="{ThemeResource SystemAccentColor}"
|
||||
BorderThickness="0,1,0,1"
|
||||
Margin="4,0">
|
||||
<StackPanel HorizontalAlignment="Right"
|
||||
Orientation="Horizontal">
|
||||
<Button Command="{x:Bind ViewModel.AddEditCommand}"
|
||||
Content="Add/Edit Item"
|
||||
Margin="8,8,0,8"/>
|
||||
<Button Command="{x:Bind ViewModel.DeleteCommand}"
|
||||
Content="Delete Item"
|
||||
Margin="8"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</Page>
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using MyMediaCollection.ViewModels;
|
||||
|
||||
namespace MyMediaCollection.Views
|
||||
{
|
||||
/// <summary>
|
||||
/// An empty page that can be used on its own or navigated to within a Frame.
|
||||
/// </summary>
|
||||
public sealed partial class MainPage : Page
|
||||
{
|
||||
public MainPage()
|
||||
{
|
||||
ViewModel = App.HostContainer.Services.GetService<MainViewModel>();
|
||||
this.InitializeComponent();
|
||||
Loaded += MainPage_Loaded;
|
||||
}
|
||||
|
||||
private void MainPage_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var mainWindow = (Application.Current as App)?.Window as MainWindow;
|
||||
if (mainWindow != null)
|
||||
{
|
||||
mainWindow.SetPageTitle("Home");
|
||||
}
|
||||
}
|
||||
|
||||
public MainViewModel ViewModel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user