Skip to content

Commit 2625468

Browse files
committed
wip commit(dlg message)
1 parent c36437b commit 2625468

File tree

7 files changed

+87
-50
lines changed

7 files changed

+87
-50
lines changed

src/WEventViewer/MainWindow.axaml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
xmlns:vm="using:WEventViewer.ViewModel"
66
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
77
x:Class="WEventViewer.MainWindow"
8+
x:DataType="vm:MainWindowViewModel"
89
Title="WEventViewer">
9-
<Design.DataContext>
10-
<vm:MainWindowViewModel></vm:MainWindowViewModel>
11-
</Design.DataContext>
12-
<StackPanel Orientation="Vertical">
13-
<Menu DockPanel.Dock="Top">
14-
<MenuItem Header="_File">
15-
<MenuItem Header="_Open" Command="{Binding $parent.$parent.$parent.DataContext.OpenCommand}"/>
16-
</MenuItem>
17-
<MenuItem Header="_About"/>
18-
</Menu>
19-
<DataGrid
20-
BorderBrush="Black"
21-
Margin="20"
22-
BorderThickness="1"
23-
IsReadOnly="True">
24-
</DataGrid>
25-
</StackPanel>
10+
<Design.DataContext>
11+
<vm:MainWindowViewModel></vm:MainWindowViewModel>
12+
</Design.DataContext>
13+
14+
<StackPanel Orientation="Vertical">
15+
<Menu DockPanel.Dock="Top">
16+
<MenuItem Header="_File">
17+
<MenuItem Header="_Open" Command="{Binding OpenCommand}"/>
18+
</MenuItem>
19+
<MenuItem Header="_About"/>
20+
</Menu>
21+
<DataGrid
22+
BorderBrush="Black"
23+
Margin="20"
24+
BorderThickness="1"
25+
IsReadOnly="True">
26+
</DataGrid>
27+
</StackPanel>
2628
</Window>

src/WEventViewer/MainWindow.axaml.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ public partial class MainWindow : Window
99
{
1010
public MainWindow()
1111
{
12-
InitializeComponent();
1312
DataContext = new MainWindowViewModel();
13+
InitializeComponent();
1414
WeakReferenceMessenger.Default.Register<MainWindow, OpenLogRequest>(this, async (recpient, req) =>
1515
{
1616
var vm = new OpenLogWindowViewModel();
1717
var dlg = new OpenLogWindow()
1818
{
1919
DataContext = vm
2020
};
21-
await dlg.ShowDialog(this);
22-
req.PathType = vm.PathType;
23-
req.LogName = vm.LogName;
21+
var ret = await dlg.ShowDialog<OpenLogWindowViewModel>(this);
22+
if(DataContext is MainWindowViewModel mwvm)
23+
{
24+
WeakReferenceMessenger.Default.Send<LoadLogMessage>(new(vm.LogName, vm.PathType));
25+
}
2426
});
2527
}
2628
}

src/WEventViewer/Model/EventLogRepository.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
1010
using System.Collections.ObjectModel;
11+
using System.Diagnostics;
1112

1213
namespace WEventViewer.Model
1314
{
@@ -52,23 +53,32 @@ public void Clear()
5253
{
5354
records.Clear();
5455
}
56+
static readonly DiagnosticListener _DS = new DiagnosticListener(nameof(EventLogRepository));
5557
public async Task Load(string logName, PathType pathType, string? query, CancellationToken token, IProgress<long> progress)
5658
{
5759
using var evreader = new EventLogReader(new EventLogQuery(logName, pathType, query));
5860
long count = 0;
59-
while(!token.IsCancellationRequested)
61+
while (!token.IsCancellationRequested)
6062
{
6163
await Task.Yield();
6264
using var record = evreader.ReadEvent();
63-
if(record == null)
65+
try
6466
{
65-
break;
67+
if (record == null)
68+
{
69+
break;
70+
}
71+
records.Add(record.ToLogRecord());
72+
count++;
73+
if ((count & 0xff) == 0)
74+
{
75+
progress?.Report(count);
76+
}
6677
}
67-
records.Add(record.ToLogRecord());
68-
count++;
69-
if((count & 0xff) == 0)
78+
catch (Exception ex)
7079
{
71-
progress?.Report(count);
80+
_DS.Write("Error", new { Exception = ex, LogRecordDescription = record.FormatDescription() });
81+
throw;
7282
}
7383
}
7484
progress?.Report(count);

src/WEventViewer/OpenLogWindow.axaml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:vm="using:WEventViewer.ViewModel"
6+
xmlns:local="using:WEventViewer"
67
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
78
x:Class="WEventViewer.OpenLogWindow"
9+
x:DataType="vm:OpenLogWindowViewModel"
810
Title="OpenLogWindow">
9-
<Design.DataContext>
10-
<vm:OpenLogWindowViewModel/>
11-
</Design.DataContext>
12-
<StackPanel HorizontalAlignment="Stretch">
13-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
14-
<Label Content="abc" Margin="10,10,10,10" HorizontalAlignment="Left"/>
15-
<TextBox Margin="10,10,10,10" Padding="50,0,0,0" HorizontalAlignment="Stretch" Text="{Binding $parent.$parent.LogName, Mode=TwoWay}"/>
16-
</StackPanel>
17-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
18-
<Label Content="abc" Margin="10,10,10,10" HorizontalAlignment="Left"/>
19-
<TextBox Margin="10,10,10,10" Padding="0,0,0,0" HorizontalAlignment="Stretch"/>
20-
</StackPanel>
21-
</StackPanel>
11+
<Design.DataContext>
12+
<vm:OpenLogWindowViewModel/>
13+
</Design.DataContext>
14+
<Window.Resources>
15+
<vm:PathTypeValueConverter x:Key="PathTypeConverter"/>
16+
</Window.Resources>
17+
<StackPanel HorizontalAlignment="Stretch">
18+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
19+
<Label Content="LogName" Margin="10,10,10,10" HorizontalAlignment="Left"/>
20+
<TextBox Margin="10,10,10,10" Padding="0,0,0,0" HorizontalAlignment="Stretch" Text="{Binding LogName, Mode=TwoWay}"/>
21+
</StackPanel>
22+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
23+
<Label Content="PathType" Margin="10,10,10,10" HorizontalAlignment="Left" VerticalAlignment="Center"/>
24+
<TextBox Margin="10,10,10,10" Padding="10,0,10,10" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=PathType, Mode=TwoWay,Converter={StaticResource PathTypeConverter}}"/>
25+
</StackPanel>
26+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
27+
<Button Content="OK"/>
28+
<Button Content="Cancel"/>
29+
</StackPanel>
30+
</StackPanel>
2231
</Window>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
using Avalonia;
22
using Avalonia.Controls;
33
using Avalonia.Markup.Xaml;
4+
using CommunityToolkit.Mvvm.Messaging;
45

56
namespace WEventViewer;
67

78
public partial class OpenLogWindow : Window
89
{
10+
internal record class OpenDialogResultMessage(bool isOk);
911
public OpenLogWindow()
1012
{
1113
InitializeComponent();
14+
WeakReferenceMessenger.Default.Register<OpenLogWindow, OpenDialogResultMessage>(this, (recipient, msg) =>
15+
{
16+
if(msg.isOk)
17+
{
18+
19+
}
20+
});
1221
}
1322
}

src/WEventViewer/ViewModel/MainWindowViewModel.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
using CommunityToolkit.Mvvm.DependencyInjection;
88
using CommunityToolkit.Mvvm.Input;
99
using CommunityToolkit.Mvvm.Messaging;
10-
using CommunityToolkit.Mvvm.Collections;
10+
using CommunityToolkit.Mvvm.ComponentModel;
1111
using System.Windows.Input;
1212
using WEventViewer.Model;
1313
using System.Diagnostics.Eventing.Reader;
1414
using System.Diagnostics.Eventing;
1515
using System.Collections.ObjectModel;
1616
using System.Collections.Specialized;
17+
using Avalonia;
1718

1819
namespace WEventViewer.ViewModel
1920
{
@@ -22,6 +23,7 @@ class OpenLogRequest
2223
public string? LogName { get; set; }
2324
public PathType PathType { get; set; }
2425
}
26+
record class LoadLogMessage(string logName, PathType pathType);
2527
internal class MainWindowViewModel
2628
{
2729
EventLogRepository? _EventLogRepository;
@@ -30,13 +32,13 @@ public MainWindowViewModel()
3032
_Progress = new Progress<long>(l => LogCount = l);
3133
_EventLogRepository = new EventLogRepository();
3234
_EventLogRepository.Records.CollectionChanged += Records_CollectionChanged;
33-
OpenCommand = new RelayCommand(async () =>
35+
OpenCommand = new RelayCommand(() =>
3436
{
35-
var ret = WeakReferenceMessenger.Default.Send<OpenLogRequest>(new OpenLogRequest());
36-
if(ret.LogName != null)
37-
{
38-
await _EventLogRepository.Load(ret.LogName, ret.PathType, null, default, _Progress);
39-
}
37+
WeakReferenceMessenger.Default.Send<OpenLogRequest>(new OpenLogRequest());
38+
});
39+
WeakReferenceMessenger.Default.Register<MainWindowViewModel, LoadLogMessage>(this, async (vm, msg) =>
40+
{
41+
await _EventLogRepository.Load(msg.logName, msg.pathType, null, default, _Progress);
4042
});
4143
}
4244

@@ -49,7 +51,7 @@ private void Records_CollectionChanged(object? sender, NotifyCollectionChangedEv
4951
}
5052

5153
Progress<long> _Progress;
52-
public ICommand OpenCommand;
54+
public ICommand OpenCommand { get; private set; }
5355
long _LogCount;
5456
public long LogCount
5557
{

src/WEventViewer/ViewModel/OpenLogWindowViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public PathType PathType
7272
public OpenLogWindowViewModel()
7373
{
7474
IsOk = false;
75-
OkCommand = new RelayCommand(() => IsOk = true);
75+
OkCommand = new RelayCommand(() =>
76+
{
77+
IsOk = true;
78+
});
7679
}
7780
public bool IsOk;
7881
public ICommand OkCommand;

0 commit comments

Comments
 (0)