main
syneffort 2 years ago
parent 6079621243
commit c81bfdf64e
  1. 12
      PacticeSolution/PacticeSolution.sln
  2. 9
      PacticeSolution/TimerAndWorker/App.xaml
  3. 17
      PacticeSolution/TimerAndWorker/App.xaml.cs
  4. 10
      PacticeSolution/TimerAndWorker/AssemblyInfo.cs
  5. 28
      PacticeSolution/TimerAndWorker/MainWindow.xaml
  6. 122
      PacticeSolution/TimerAndWorker/MainWindow.xaml.cs
  7. 10
      PacticeSolution/TimerAndWorker/TimerAndWorker.csproj
  8. 9
      PacticeSolution/TreeViewSample/App.xaml
  9. 17
      PacticeSolution/TreeViewSample/App.xaml.cs
  10. 10
      PacticeSolution/TreeViewSample/AssemblyInfo.cs
  11. 66
      PacticeSolution/TreeViewSample/MainWindow.xaml
  12. 72
      PacticeSolution/TreeViewSample/MainWindow.xaml.cs
  13. 10
      PacticeSolution/TreeViewSample/TreeViewSample.csproj

@ -129,6 +129,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaPlayer", "MediaPlayer\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Speech", "Speech\Speech.csproj", "{248DDC0C-F8C0-4687-BBC2-71C7C310537E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimerAndWorker", "TimerAndWorker\TimerAndWorker.csproj", "{6210631F-C8DF-45DC-B53E-3119E8BDFF9E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TreeViewSample", "TreeViewSample\TreeViewSample.csproj", "{CA88EE83-8C7F-4FEF-963A-DEACA5E502FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -387,6 +391,14 @@ Global
{248DDC0C-F8C0-4687-BBC2-71C7C310537E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{248DDC0C-F8C0-4687-BBC2-71C7C310537E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{248DDC0C-F8C0-4687-BBC2-71C7C310537E}.Release|Any CPU.Build.0 = Release|Any CPU
{6210631F-C8DF-45DC-B53E-3119E8BDFF9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6210631F-C8DF-45DC-B53E-3119E8BDFF9E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6210631F-C8DF-45DC-B53E-3119E8BDFF9E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6210631F-C8DF-45DC-B53E-3119E8BDFF9E}.Release|Any CPU.Build.0 = Release|Any CPU
{CA88EE83-8C7F-4FEF-963A-DEACA5E502FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA88EE83-8C7F-4FEF-963A-DEACA5E502FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA88EE83-8C7F-4FEF-963A-DEACA5E502FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA88EE83-8C7F-4FEF-963A-DEACA5E502FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,9 @@
<Application x:Class="TimerAndWorker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TimerAndWorker"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace TimerAndWorker
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,28 @@
<Window x:Class="TimerAndWorker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TimerAndWorker"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label x:Name="lblTime" Grid.Row="0" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"
Content="00:00:00"/>
<StackPanel Margin="10" Grid.Row="1">
<DockPanel DockPanel.Dock="Top">
<Button x:Name="btnSyncCalc" HorizontalAlignment="Left" Width="150" Content="Sync"
Click="btnSyncCalc_Click"/>
<Button x:Name="btnAsyncCalc" HorizontalAlignment="Right" Width="150" Content="Async"
Click="btnAsyncCalc_Click"/>
</DockPanel>
<ListBox x:Name="lbResult" Height="100"/>
<ProgressBar x:Name="pbCalcProcess" DockPanel.Dock="Bottom" Height="18"/>
</StackPanel>
</Grid>
</Window>

@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace TimerAndWorker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly int RECURSIVE_COUNT = 10000;
private DispatcherTimer _timer;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(1);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private void _timer_Tick(object? sender, EventArgs e)
{
lblTime.Content = DateTime.Now.ToString("HH:mm:ss.fff");
}
private void btnSyncCalc_Click(object sender, RoutedEventArgs e)
{
pbCalcProcess.Value = 0;
lbResult.Items.Clear();
int result = 0;
for (int i = 0; i < RECURSIVE_COUNT; i++)
{
if (i % 7 == 0)
{
lbResult.Items.Add(i);
lbResult.SelectedIndex = lbResult.Items.Count - 1;
lbResult.ScrollIntoView(lbResult.SelectedItem);
result++;
}
Thread.Sleep(1);
pbCalcProcess.Value = Convert.ToInt32((double)i / RECURSIVE_COUNT * 100);
}
MessageBox.Show($"Numbers between 0 and {RECURSIVE_COUNT} divisable by 7: {result}");
}
private void btnAsyncCalc_Click(object sender, RoutedEventArgs e)
{
pbCalcProcess.Value = 0;
lbResult.Items.Clear();
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += Worker_DoWork;
worker.ProgressChanged += Worker_ProgressChanged;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
worker.RunWorkerAsync(RECURSIVE_COUNT);
}
private void Worker_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show($"Numbers between 0 and {RECURSIVE_COUNT} divisable by 7: {e.Result}");
}
private void Worker_ProgressChanged(object? sender, ProgressChangedEventArgs e)
{
pbCalcProcess.Value = e.ProgressPercentage;
if (e.UserState != null)
{
lbResult.Items.Add(e.UserState);
lbResult.SelectedIndex = lbResult.Items.Count - 1;
lbResult.ScrollIntoView(lbResult.SelectedItem);
}
}
private void Worker_DoWork(object? sender, DoWorkEventArgs e)
{
int max = (int)e.Argument;
int result = 0;
for (int i = 0; i < max; i++)
{
int progressPercentage = Convert.ToInt32((double)i / RECURSIVE_COUNT * 100);
if (i % 7 == 0)
{
result++;
(sender as BackgroundWorker)?.ReportProgress(progressPercentage, i);
}
else
(sender as BackgroundWorker)?.ReportProgress(progressPercentage);
Thread.Sleep(1);
}
e.Result = result;
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,9 @@
<Application x:Class="TreeViewSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewSample"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace TreeViewSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,66 @@
<Window x:Class="TreeViewSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeViewSample"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TreeView Grid.Row="0" Margin="10">
<TreeViewItem Header="Level 1" IsExpanded="True">
<TreeViewItem Header="Level 2.1"/>
<TreeViewItem Header="Level 2.2" IsExpanded="True">
<TreeViewItem Header="Level 3.1"/>
<TreeViewItem Header="Level 3.2"/>
</TreeViewItem>
<TreeViewItem Header="Level 2.3"/>
</TreeViewItem>
</TreeView>
<TreeView Grid.Row="1" Margin="10">
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Ellipse Width="10" Height="10" Margin="0 0 10 0" Fill="LightBlue"/>
<TextBlock Text="Level 1 (Blue)"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Level 2.1" Foreground="Blue"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem IsExpanded="True">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Ellipse Width="10" Height="10" Margin="0 0 10 0" Fill="LightGreen"/>
<TextBlock Text="Level 2.2 (Green)" Foreground="Green"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="Level 3.1" Foreground="Red"/>
</TreeViewItem.Header>
</TreeViewItem>
<TreeViewItem>
<TreeViewItem.Header>
<TextBlock Text="Level 3.2" Foreground="Red"/>
</TreeViewItem.Header>
</TreeViewItem>
</TreeViewItem>
</TreeViewItem>
</TreeView>
<TreeView x:Name="trvStructure" Grid.Row="2" Margin="10"
TreeViewItem.Expanded="trvStructure_Expanded"/>
</Grid>
</Window>

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TreeViewSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
trvStructure.Items.Add(CreateTreeItem(drive));
}
}
private TreeViewItem CreateTreeItem(object obj)
{
TreeViewItem item = new TreeViewItem();
item.Header = obj.ToString();
item.Tag = obj;
item.Items.Add("Loading...");
return item;
}
private void trvStructure_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.Source as TreeViewItem;
if (item.Items.Count != 1 || !(item.Items[0] is string))
return;
item.Items.Clear();
DirectoryInfo expandedDir = null;
if (item.Tag is DriveInfo)
expandedDir = (item.Tag as DriveInfo).RootDirectory;
if (item.Tag is DirectoryInfo)
expandedDir = (item.Tag as DirectoryInfo);
try
{
foreach (DirectoryInfo subDir in expandedDir.GetDirectories())
item.Items.Add(CreateTreeItem(subDir));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Loading…
Cancel
Save