定時任務 Wpf.Quartz.Demo.2

来源:https://www.cnblogs.com/akwkevin/archive/2019/02/22/10416335.html
-Advertisement-
Play Games

定時任務 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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1. Unobtrusive JavaScript介紹 說到Unobtrusive Ajax,就要談談UnobtrusiveJavaScript了,所謂Unobtrusive JavaScript即為非侵入式JavaScript(即將Js代碼與html代碼分離,方便閱讀與維護),是目前在Web開發領 ...
  • 在本文中,我們將學習如何使用Rotativa.AspNetCore工具從ASP.NET Core中的視圖創建PDF。如果您使用ASP.NET MVC,那麼Rot​​ativa工具已經可用,我們可以使用它來生成pdf。 創建一個MVC項目,無論您是core或不core,都可以nuget下包.命令如下: ...
  • 一、問題描述 進行winform 開發我們在進行數據交換時避免不了使用多線程或非同步方法,這樣操作也將避免不了跨線程對控制項進行操作(賦值、修改屬性)。 下麵通過一個測試說明一下問題 點擊一個按鈕非同步對textbox進行賦值 運行測試結果 1 private void button1_Click(obj ...
  • 我們一般下載中文文件名一般會utf-8編碼再下載,火狐瀏覽器會出現文件名亂碼,解決辦法,火狐瀏覽器文件名不編碼,直接下載: ...
  • 上一篇我們說到大文件的分片下載、斷點續傳、秒傳,有的博友就想看分片下載,我們也來總結一下下載的幾種方式,寫的比較片面,大家見諒^_^。 下載方式: 1、html超鏈接下載; 2、後臺下載(四種方法:返回filestream、返回file、TransmitTile方法、Response分塊下載)。 1 ...
  • //定義變數 bool a = false; //正則表達式 string b = @" ^ (13[0 - 9] | 14[5 | 7] | 15[0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9] | 18[0 | 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9]) ...
  • 最近正在使用SUSE系統,因為項目環境是沒有外網的。 網上查找SUSE的資料比較少,於是整理了一下,希望對有需要的人有一點點幫助。 "SUSE12Sp3安裝配置.net core 生產環境(1) IP,DNS,網關,SSH,GIT" "SUSE12Sp3安裝配置.net core 生產環境(2) 離 ...
  • listbox使用DataSource進行數據綁定和刪除,大家肯定都會,寫這個隨筆只是因為。。。。這是一年半以前剛進公司的我遺留的bug,現在看看當時竟然沒有解決 - -現在寫個測試程式,寫個隨筆記錄一下,當時萌新的我。。。首先聲明瞭一個類,要綁定的類型。 //聲明一個全局集合 public Lis ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...