定時任務 Wpf.Quartz.Demo.1已經能運行了,本節開始用wpf搭界面。 準備工作: 1.界面選擇MahApp.Metro 在App.xaml添加資源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.Merg ...
定時任務 Wpf.Quartz.Demo.1已經能運行了,本節開始用wpf搭界面。
準備工作:
1.界面選擇MahApp.Metro
在App.xaml添加資源
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /> <ResourceDictionary> <System:Double x:Key="WindowTitleFontSize">12</System:Double> <System:Double x:Key="NormalFontSize">12</System:Double> <System:Double x:Key="ContentFontSize">12</System:Double> <System:Double x:Key="FlatButtonFontSize">12</System:Double> <System:Double x:Key="MenuFontSize">12</System:Double> <System:Double x:Key="ContextMenuFontSize">12</System:Double> <System:Double x:Key="StatusBarFontSize">12</System:Double> <System:Double x:Key="ToggleSwitchFontSize">12</System:Double> <System:Double x:Key="ToggleSwitchHeaderFontSize">12</System:Double> <System:Double x:Key="UpperCaseContentFontSize">12</System:Double> <System:Double x:Key="TabItemFontSize">12</System:Double> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>App.xaml
其中 <System:Double x:Key="WindowTitleFontSize">12</System:Double> ...為改變整個app的相關字體大小。
2.添加log4net記錄異常
安裝log4net。
在App.config內配置log4net
<log4net> <!--按日期分割日誌文件 一天一個--> <appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender"> <!--是否續寫--> <param name="AppendToFile" value="true" /> <!--最小鎖定模型以允許多個進程可以寫入同一個文件--> <param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" /> <param name="StaticLogFileName" value="true" /> <!--保存路徑--> <param name="File" value="Log\" /> <param name="DatePattern" value="yyyy-MM-dd.LOG" /> <param name="StaticLogFileName" value="false" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n-----------------------------------------%n時間:%d %n級別:%level %n類名:%c%n文件:%F 第%L行%n日誌內容:%m%n-----------------------------------------%n%n" /> </layout> </appender> <root> <!--文件形式記錄日誌--> <appender-ref ref="LogFileAppenderByDate" /> </root> </log4net>App.config
在App.xaml.cs內添加
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Windows; [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace Wpf.Quartz { /// <summary> /// App.xaml 的交互邏輯 /// </summary> public partial class App : Application { public App() { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException; } private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { //記錄嚴重錯誤 log.Fatal(e.Exception); e.Handled = true;//使用這一行代碼告訴運行時,該異常被處理了,不再作為UnhandledException拋出了。 } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { //記錄嚴重錯誤 log.Fatal(e.ExceptionObject); } } }App.xaml.cs
其中 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException += Application_DispatcherUnhandledException; 捕獲沒有處理的異常,避免程式崩潰。
3.使用MVVM模式,這裡不使用mvvmlight,prism等框架,這裡寫個簡單的類ViewModel繼承它即可。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Wpf.Quartz.Helper { public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }BaseViewModel
4.前臺主界面
<Controls:MetroWindow x:Class="MyWpf.Quart.Demo.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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" Title="QuartzDemo" Height="600" Width="800" WindowStartupLocation="CenterScreen" > <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="100"></RowDefinition> </Grid.RowDefinitions> <Menu Background = "{DynamicResource AccentColorBrush}"> <MenuItem Header="系統菜單" Background="Transparent"> <MenuItem Header="全部開始" Command="{Binding MeunCommand}" CommandParameter="StartAll"/> <MenuItem Header="全部停止" Command="{Binding MeunCommand}" CommandParameter="StopAll"/> </MenuItem> </Menu> <DataGrid Grid.Row="1" x:Name="table" AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding TaskRuns}" SelectedItem="{Binding SelectedRun}" ColumnWidth="Auto"> <DataGrid.ContextMenu> <ContextMenu > <MenuItem Header="啟動" Command="{Binding JobStartCommand}" CommandParameter="{Binding SelectedRun}" /> <MenuItem Header="停止" Command="{Binding JobStopCommand}" CommandParameter="{Binding SelectedRun}" /> <MenuItem Header="重新啟動" Command="{Binding JobReStartCommand}" CommandParameter="{Binding SelectedRun}" /> <MenuItem Header="暫停" Command="{Binding JobPauseCommand}" CommandParameter="{Binding SelectedRun}" /> <MenuItem Header="恢復" Command="{Binding JobResumeCommand}" CommandParameter="{Binding SelectedRun}" /> <MenuItem Header="手動執行一次" Command="{Binding JobRunCommand}" CommandParameter="{Binding SelectedRun}" /> </ContextMenu> </DataGrid.ContextMenu> <DataGrid.Columns> <DataGridTextColumn Header="編輯" IsReadOnly="True" Binding="{Binding IsEdit}"></DataGridTextColumn> <DataGridTextColumn Header="名稱" IsReadOnly="True" Binding="{Binding DisplayName}"></DataGridTextColumn> <DataGridTextColumn Header="標識符" IsReadOnly="True" Binding="{Binding Name}"></DataGridTextColumn> <DataGridTemplateColumn Header="狀態"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock VerticalAlignment="Center"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="描述" IsReadOnly="True" Binding="{Binding Remark}"></DataGridTextColumn> <DataGridTemplateColumn Header="執行設置" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="txt" ToolTip="點擊設置" VerticalAlignment="Center"> <Hyperlink Command="{Binding DataContext.JobSetCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" CommandParameter="{Binding }">設置 </Hyperlink> <Run Text="{Binding SettingStr}"></Run> </TextBlock> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="開始時間" IsReadOnly="True" Binding="{Binding StartTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn> <DataGridTextColumn Header="結束時間" IsReadOnly="True" Binding="{Binding EndTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn> <DataGridTextColumn Header="下次執行時間" IsReadOnly="True" Binding="{Binding NextRunTime,StringFormat=yyyy-MM-dd HH:mm:ss}"></DataGridTextColumn> <DataGridTemplateColumn Header="詳情"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="txt" ToolTip="點擊打開" VerticalAlignment="Center"> <Hyperlink Command="{Binding DataContext.JobDetailCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" CommandParameter="{Binding }">詳情</Hyperlink> </TextBlock> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> <GridSplitter x:Name="gsSplitterr" Grid.Row="2" Height="3" Background="{DynamicResource AccentColorBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> <RichTextBox x:Name="rtb" Grid.Row="3" IsReadOnly="True" VerticalScrollBarVisibility="Auto"> <RichTextBox.ContextMenu> <ContextMenu> <MenuItem Header="剪貼" Command="ApplicationCommands.Cut"/> <MenuItem Header="複製" Command="ApplicationCommands.Copy"/> <MenuItem Header="粘貼" Command="ApplicationCommands.Paste"/> </ContextMenu> </RichTextBox.ContextMenu> <RichTextBox.Resources> <Style TargetType="{x:Type Paragraph}"> <Setter Property="Margin" Value="0"/> <Setter Property="LineHeight" Value="20"/> </Style> </RichTextBox.Resources> </RichTextBox> </Grid> </Controls:MetroWindow>MainWindow
好的,至此準備工作已經完成。預計還有兩節全部完成。
代碼下載:https://pan.baidu.com/s/1Ri_yangO0N0sfC-KyZVwbw