欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

定时任务 Wpf.Quartz.Demo.2

程序员文章站 2022-07-25 23:09:18
定时任务 Wpf.Quartz.Demo.1已经能运行了,本节开始用wpf搭界面。 准备工作: 1.界面选择MahApp.Metro 在App.xaml添加资源

定时任务 wpf.quartz.demo.1已经能运行了,本节开始用wpf搭界面。

准备工作:

1.界面选择mahapp.metro

在app.xaml添加资源

定时任务 Wpf.Quartz.Demo.2
 <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

定时任务 Wpf.Quartz.Demo.2
<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内添加

定时任务 Wpf.Quartz.Demo.2
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继承它即可。

定时任务 Wpf.Quartz.Demo.2
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.前台主界面

定时任务 Wpf.Quartz.Demo.2

 

定时任务 Wpf.Quartz.Demo.2
<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