首页 > 其他分享 >WPF WebClient EAP async await

WPF WebClient EAP async await

时间:2024-04-07 21:00:45浏览次数:18  
标签:Windows Text await System result using EAP WPF WebClient

<Window x:Class="WpfApp40.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:local="clr-namespace:WpfApp40" WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="0">
            <TextBlock x:Name="_result"   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
            FontSize="20"/>
        </ScrollViewer>
       
        <Button Grid.Row="1" x:Name="_loadBtn" Click="_loadBtn_Click" Content="Load Content" FontSize="20"
                HorizontalAlignment="Left" />
    </Grid>
</Window>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp40
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string url = "https://learn.microsoft.com/en-us/";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void _loadBtn_Click(object sender, RoutedEventArgs e)
        {
            var client = new WebClient();
            
            _result.Text = "Please wait...";
            client.DownloadStringCompleted += (s, args) =>
            {
                if(args.Error!=null)
                {
                    _result.Text = "Error:" + args.Error.Message;
                }
                else
                {
                    _result.Text = args.Result;
                }
            };
            client.DownloadStringAsync(new Uri(url));
        }
    }
}

 

 

 

//WebClient DownloadStringTaskAsync, async await 
private async void _loadBtn_Click(object sender, RoutedEventArgs e)
{
    var client = new WebClient();
    _result.Text = "Please wait...";
    try
    {
        _result.Text = await client.DownloadStringTaskAsync(url);
    }
    catch (WebException ex)
    {
        _result.Text = "Error: " + ex.Message;     
    }
}

 

标签:Windows,Text,await,System,result,using,EAP,WPF,WebClient
From: https://www.cnblogs.com/Fred1987/p/18119865

相关文章

  • 往 VisualStudio 工具箱中添加 WPF/WinForms 控件的几种方式
    在使用VisualStudio开发WPF或WinForms应用时,打开UI文件的设计界面,我们可以从工具箱的控件列表中直接拖拽控件到界面上。通过这种方式,可以清晰的展示控件库中所有可用的控件,并且非常方便的将其添加到界面中。那么我们可以通过哪些方式将WPF/WinForms控件库中的控件添加到VisualS......
  • WPF —— 后台实现fromto动画实例
    标签页<ButtonWidth="100"Height="40"Content="点击开始动画"Click="Button_Click"Name="b1"></Button><!--HorizontalAlignment="Left"-->&l......
  • NodeJs通过async/await处理异步
    NodeJs通过async/await处理异步 场景远古时代我们在编写express后台,经常要有许多异步IO的处理。在远古时代,我们都是用chunk函数处理,也就是我们最熟悉的那种默认第一个参数是error的函数。我们来模拟一个Mongo数据库的操作,感受一下。mongoDb.open(function(err,db){......
  • 用async/await改造Node.js(Express)网站
    用async/await改造Node.js(Express)网站Mike的读书季关注IP属地:北京2018.11.0200:13:00字数582阅读3,1151.回调的嵌套陷阱在Node.js中,使用回调的方式进行异步操作,我们以读取文件内容为例:constfs=require('fs');//定义一个以回调的方式获取文件的函数funct......
  • WPF datagrid mvvm multi select via customize datagrid
    usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;namespaceWpfApp39{publicclassMultiSelectDataGrid:D......
  • C#开发之WPF项目中权限控制的实现(attribute)
    1功能描述实现一个权限检查机制,以确保用户根据其权限级别进行相应的操作。定义四级权限:Operator,Maintenance,Supervisor,Administrator,每一级权限都有其特定的操作范围。能够根据用户的权限级别判断用户是否有权执行特定的操作。2设计分析如果实现为接口形式,那么每次在需......
  • WPF布局控件汇总
    1.Grid表格布局Grid为WPF中最常用的布局容器,作为View中的主要组成部分,负责框架中整体的页面布局。注意:Grid的列宽与行高可采用固定、自动、按比例三种方式定义。固定长度:值为一个确定的数字自动长度:值为Auto,实际作用就是取实际控件所需的最小值比例长度:*表示占用剩余的全......
  • WPF开发一个可以自适应排列的Panel控件
    一.控件介绍    初看标题可能无法理解,我们看看什么是自适应排列。乍一看它有点像WrapPanel控件,都是从左至右排列,如果一行排列不下就换行继续排列,但是细看你就会发现不对,WrapPanel控件行尾是不会对齐的,也就是说只要WrapPanel的子控件的宽度不一致,每一行的末尾就会必定留下一......
  • Wpf BackgroundWorker WorkerSupportsCancellation CancellationPending
    //xaml<Windowx:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • Wpf BackgroundWorker DoWork RunWorkerCompleted
    //xaml<Windowx:Class="WpfApp37.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......