首先把流程源码放出来 这些代码也包含了一个定时器和记录前台显示,每天几点执行一次,需要拿去,从最底层开始
<Button
x:Name="btnStart"
Width="100"
Height="30"
Margin="5"
HorizontalAlignment="Left"
Click="btnStart_Click"
Content="启动" />
<TextBox
Name="txtLog"
Grid.Row="1"
Grid.ColumnSpan="2"
AcceptsReturn="True"
Foreground="Black"
HorizontalScrollBarVisibility="Auto"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto" />
然后到数据层
public async Task Execute(string[] args)
{
try
{
await Task.Run(() =>
{
LoadDisplay("你迟到了")
})
}
catch (Exception e)
{
//LoadDisplay($"请求接口时出现异常{e.Message}")
}
}
/// <summary>
/// 输出日志信息
/// </summary>
/// <param name="news"></param>
public void LoadDisplay(string news)
{
this.txtLog.Dispatcher.BeginInvoke((Action)delegate
{
this.txtLog.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss " + news + "\r\n"))
if (IsVerticalScrollBarAtBottom)
{
this.txtLog.ScrollToEnd()
}
})
}
调用层
复制代码
/// <summary>
/// //要执行的任务
/// </summary>
/// <param name="state"></param>
private async void DoTask(object state)
{
string[] args = new string[0]
//执行功能...
await Execute(lines)
//再次设定
SetTaskAtFixedTime()
}
复制代码
开始new一个计时器
复制代码
/// <summary>
/// //设定定时执行
/// </summary>
public void SetTaskAtFixedTime()
{
DateTime now = DateTime.Now
DateTime oneOClock = DateTime.Today.AddHours(7.0)//晨7:00
if (now >oneOClock)
{
oneOClock = oneOClock.AddDays(1.0)
}
int msUntilFour = (int)(oneOClock - now).TotalMilliseconds
var t = new System.Threading.Timer(DoTask)
t.Change(msUntilFour, Timeout.Infinite)
}
复制代码
最后 按钮点击事件
private void CheckBox_是否保存为文本文件_Checked(object sender, RoutedEventArgs e){
string fileName = string.Empty
if (string.IsNullOrEmpty(this.TextBox_文件位置.Text))
{
var dialog = new SaveFileDialog { DefaultExt = "txt", AddExtension = true, FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss", DateTimeFormatInfo.CurrentInfo) + "发票" }
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
System.Windows.MessageBox.Show("请选择要保存的文件名称")
return
}
fileName = dialog.FileName
}
else
{
fileName = TextBox_文件位置.Text
}
using (var writer = new StreamWriter(fileName, false, Encoding.UTF8))
{
writer.Write(TextBox_发票.Text)
}
}
private void Button_选择_Click(object sender, RoutedEventArgs e)
{
ClearText()
var dialog = new OpenFileDialog { Multiselect = false, Filter = "TxtFiles|*.txt" }
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.TextBox_文件位置.Text = dialog.FileName
using (var stream = new StreamReader(dialog.FileName, Encoding.Default))
{
TextBox_发票.Text = stream.ReadToEnd()
}
}
}
在WPF中写作文,首先,你需要准备一个文本编辑器,比如Microsoft Word,然后打开WPF程序,在程序中添加一个文本框,将文本框的文本属性设置为可编辑,然后就可以在文本框中输入文字,编辑文章了。此外,你还可以在WPF程序中添加按钮,实现文章的保存、打印等功能,以及添加其他控件,实现文章的格式化等功能。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)