1. 重庆云诚科技 > 互联网资讯 >

(php开发项目教程)wpf应用开发项目教程

导读学习WPF需要哪些基础知识,要学习C#吗本文最佳回答用户:【梦未逝】 ,现在由重庆云诚科技小编为你讲解与【php开发项目教程】的相关内容!优质回答WPF是微软提供的一种用来开发“...

今天我们来聊聊[php开发项目教程],以下4关于wpf应用开发项目教程的观点希望能帮助到您找到想要的结果。

学习WPF需要哪些基础知识,要学习C#吗

本文最佳回答用户:【梦未逝】 ,现在由重庆云诚科技小编为你讲解与【php开发项目教程】的相关内容!

优质回答WPF是微软提供的一种用来开发“桌面应用”的技术(框架),这项技术本身和C#没有关系,必须会的是xaml语法,而不是编程语言。

关于xaml语法,是一种微软提供的新型的前端语言,可以理解为类似js+css;

参考微软介绍:

单纯的使用Blend也可以开发WPF项目,完全不用写一行代码,就可以做出一个漂亮的界面。调用其他人写好的服务接口,就可以轻松的开发出wpf应用了。

比如常见的“天气预报”应用,就可以不用写后台代码(c#),直接调用一些开放的天气API获取数据,就可以让程序运行起来。

如果要做一些较为复杂的业务系统, 则必须有编程语言的参与,WPF可以与VB或C#两种编程语言结合,进行开发。 目前肯定是C#更优于VB.NET。

个人建议WPF和C#要分开,不要为了开发一个WPF程序而去学习C#,也不要因为希望把C#代码的工作可视化而去开发桌面应用。

C#是编程语言,开发侧重于逻辑、语法、执行效率和安全性,WPF是前端技术,侧重于美观、酷炫、用户体验。你应该选好自己侧重的方向去进行系统学习。

在VS里一边拖控件,一边写代码,这种学出来的都是四不像。

上文就是重庆云诚科技小编分享贡献者:(梦未逝)分析的关于“学习WPF需要哪些基础知识,要学习C#吗”的问题了,不知是否已经解决你的问题?如果没有,下一篇内容可能是你想要的答案,现在接着继续谈谈下文用户【一梦碎今生】分享的“如何在WPF应用程序中通过HttpClient调用Web API”的一些相关疑问做出分析与解答,如果能找到你的答案,可以关注本站。

学习WPF需要哪些基础知识,要学习C#吗

如何在WPF应用程序中通过HttpClient调用Web API

本文最佳回答用户:【一梦碎今生】 ,现在由重庆云诚科技小编为你解答与【php开发项目教程】的相关内容!

优质回答Asynchronous Calls

异步调用

HttpClient is designed to be non-blocking. Potentially long-running operations are implemented as asynchonrous methods, such as GetAsync and PostAsync. These methods return without waiting for the operation to complete. The previous tutorial (Calling a Web API From a Console Application) showed only blocking calls:

HttpClient被设计成是非阻塞的。潜在地,长时间运行的操作是作为异步方法实现的,例如,GetAsync和PostAsync。这些方法不会等待操作完成便会返回。上一教程(通过控制台应用程序调用Web API)只展示了阻塞调用:

HttpResponseMessage response = client.GetAsync("api/products").Result; // Blocking call(阻塞)!

This code blocks the calling thread by taking the Result property. That's OK for a console application, but you should not do it from a UI thread, because it blocks the UI from responding to user input.

这段代码通过采用Result属性,会阻塞调用线程。对于一个控制台应用程序,这没问题,但你不应该在一个UI线程中采用这一做法,因为这会阻止UI去响应用户输入。

The asynchronous methods of HttpClient return Task objects that represent the asynchronous operation.

HttpClient的异步方法会返回表示异步操作的Task对象。

Create the WPF Project

创建WPF项目

Start Visual Studio. From the Start menu, select New Project. In the Templates pane, select Installed Templates and expand the Visual C# node. In the list of project templates, select WPF Application. Name the project and click OK.

启动Visual Studio。从“开始”菜单选择“新项目”。在“模板”面板中,选择“已安装模板”,并展开“Viusal C#”节点。在项目模板列表中,选择“WPF应用程序”。命名此项目并点击“OK”。

Open MainWindow.xaml and add the following XAML markup inside the Grid control:

打开MainWindow.xaml,并在Grid控件中添加以下XAML标记:

<StackPanel Width="250" >

<Button Name="btnGetProducts" Click="GetProducts">Get Products</Button>

<ListBox Name="ProductsList">

<ListBox.ItemTemplate>

<DataTemplate>

<StackPanel Margin="2">

<TextBlock Text="{Binding Path=Name}" />

<TextBlock >Price: $<Run Text="{Binding Path=Price}" />

(<Run Text="{Binding Path=Category}" />)</TextBlock>

</StackPanel>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>

</StackPanel>

This markup defines a ListBox that will be data-bound to the list of products. The DataTemplate defines how each product will be displayed.

这段标记定义了一个将被数据绑定到产品列表的ListBox(列表框)。DataTemplate(数据模板)定义了如何显示每个产品。(其效果如图3-4所示)。

图3-4. WPF界面效果

Add the Model Class

添加模型类

Add the following class to the application:

将以下类添加到应用程序:

class Product

{

public string Name { get; set; }

public double Price { get; set; }

public string Category { get; set; }

}

This class defines a data object that HttpClient will write into the HTTP request body and read from the HTTP response body.

这个类定义了一个数据对象,HttpClient将把它写入HTTP请求体,也从HTTP响应体中读取它。

We'll also add an observable class for data binding:

我们也要添加一个用于数据绑定的可见对象类(observable class):

class ProductsCollection : ObservableCollection<Product>

{

public void CopyFrom(IEnumerable<Product> products)

{

this.Items.Clear();

foreach (var p in products)

{

this.Items.Add(p);

}

this.OnCollectionChanged(

new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

}

}

Install NuGet Package Manager

安装NuGet包管理器

NuGet Package Manager is the easiest way to add the Web API Client library to a project. If you do not have NuGet Package Manager already installed, install it as follows.

将Web API客户端库添加到项目最容易的办法是安装“NuGet包管理器(NuGet Package Manager)”。如果尚未安装NuGet包管理器,按如下步骤安装。

1.Start Visual Studio.

启动Visual Studio.

2.From the Tools menu, select Extensions and Updates.

从“工具”菜单选择“扩展与更新”

3.In the Extensions and Updates dialog, select Online.

在“扩展与更新”对话框中,选择“在线”

4.If you don't see "NuGet Package Manager", type "nuget package manager" in the search box.

如果未看到“NuGet包管理器”,在搜索框中输入“nuget package manager”。

5.Select the NuGet Package Manager and click Download.

选择“NuGet包管理器”,并点击“下载”。

6.After the download completes, you will be prompted to install.

下载完成后,会提示你安装。

7.After the installation completes, you might be prompted to restart Visual Studio.

安装完成后,可能会提示重启Visual Studio。

上述安装过程如图3-5所示。

图3-5. 安装NuGet包管理器

Install the Web API Client Libraries

安装Web API客户端库

After NuGet Package Manager is installed, add the Web API Client Libraries package to your project.

安装NuGet包管理器后,把Web API客户端库包添加到你的项目。步骤如下:

1.From the Tools menu, select Library Package Manager. Note: If do you not see this menu item, make sure that NuGet Package Manager installed correctly.

从“工具”菜单选择“库包管理器”。注:如果看不到这个菜单项,请确保已正确安装了NuGet包管理器。

2.Select Manage NuGet Packages for Solution.

选择“管理解决方案的NuGet包…”

3.In the Manage NuGet Packages dialog, select Online.

在“管理NuGet包”对话框中,选择“在线”。

4.In the search box, type "Microsoft.AspNet.WebApi.Client".

在搜索框中输入“Microsoft.AspNet.WebApi.Client”。

5.Select the ASP.NET Web API Self Host package and click Install.

选择“ASP.NET Web API自托管包”,并点击“安装”。

6.After the package installs, click Close to close the dialog.

这个包安装后,点击“关闭”,关闭此对话框。

上述安装步骤如图3-6所示。

图3-6. 安装Web API客户端库

Initialize HttpClient

初始化HttpClient

From Solution Explorer, open the file MainWindow.xaml.cs. Add the following code.

在“解决方案资源管理器”中,打开MainWindow.xaml.cs文件。添加以下代码:

namespace WpfProductClient

{

using System;

using System.Collections.Generic;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Windows;

public partial class MainWindow : Window

{

HttpClient client = new HttpClient();

ProductsCollection _products = new ProductsCollection();

public MainWindow()

{

InitializeComponent();

client.BaseAddress = new Uri(";);

client.DefaultRequestHeaders.Accept.Add(

new MediaTypeWithQualityHeaderValue("application/json"));

this.ProductsList.ItemsSource = _products;

}

}

}

This code creates a new instance of HttpClient. It also sets the base URI to ";, and sets the Accept header to "application/json", which tells the server to send data in JSON format.

这段代码创建了一个新的HttpClient实例。也把基URI设置为“”,并且把Accept报头设置为“application/json”,这是告诉服务器,以JSON格式发送数据。

Notice that we also created a new ProductsCollection class and set it as the binding for the ListBox.

注意,我们也创建了一个新的ProductsCollection类,并把它设置为对ListBox的绑定。

Getting a Resource (HTTP GET)

获取资源(HTTP GET)

If you are targeting .NET Framework 4.5, the async and await keywords make it much easier to write asynchronous code.

如果你的目标是.NET Framework 4.5(意即,你所开发的应用程序将在.NET 4.5平台上运行 — 译者注),async和await关键字会让你很容易编写异步代码。

If you are targeting .NET Framework 4.0 with Visual Studio 2012, you can install the Async Targeting Pack to get async/await support.

如果你的目标是使用Visual Studio 2012的.NET Framework 4.0,可以安装Async Targeting Pack来获得async/await支持。

The following code queries the API for a list of products. Add this code to the MainWindow class:

以下代码查询产品列表API。将此代码添加到MainWindow类:

private async void GetProducts(object sender, RoutedEventArgs e)

{

try

{

btnGetProducts.IsEnabled = false;

var response = await client.GetAsync("api/products");

response.EnsureSuccessStatusCode(); // Throw on error code(有错误码时报出异常).

var products = await response.Content.ReadAsAsync<IEnumerable<Product>>();

_products.CopyFrom(products);

}

catch (Newtonsoft.Json.JsonException jEx)

{

// This exception indicates a problem deserializing the request body.

// 这个异常指明了一个解序列化请求体的问题。

MessageBox.Show(jEx.Message);

}

catch (HttpRequestException ex)

{

MessageBox.Show(ex.Message);

}

finally

{

btnGetProducts.IsEnabled = true;

}

}

The GetAsync method sends an HTTP GET request. If the HTTP response indicates success, the response body contains a list of products in JSON format. To parse the list, call ReadAsAsync. This method reads the response body and tries to deserialize it to a specified CLR type.

GetAsync方法发送一个HTTP GET请求。如果HTTP响应指示成功,响应体会含有一个JSON格式的产品列表。要解析这个列表,调用ReadAsAsync。这个方法会读取响应体,并试图把它解序列化成一个具体的CLR类型。

As their names imply, GetAsync and ReadAsAsync are asynchronous methods, meaning they return immediately, without waiting for the operation to complete. The await keyword suspends execution until the operation completes. For example:

正如其名称所暗示的,GetAsync和ReadAsAsync是异步方法,意即,它们立即返回,不会等待操作完成。await关键字会挂起执行,直到操作完成。例如:

var response = await client.GetAsync("api/products");

The code that appears after this statement does not execute until the HTTP request is completed. But that does not mean the event handler blocks, waiting for GetAsync to complete. Just the opposite — control returns to the caller. When the HTTP request is completed, execution continues from the point where it was suspended.

出现在这条语句之后的代码直到HTTP请求完成时才会执行。但这并不意味着事件处理器(event handler,也可以叫做事件处理程序 — 译者注)会阻塞,以等待GetAsync完成。恰恰相反 — 控制会返回给调用者。当HTTP请求完成时,执行会从挂起点继续。

If a method uses await, it must have the async modifier:

如果一个方法使用await,它必须有async修饰符:

private async void GetProducts(object sender, RoutedEventArgs e)

Without the await keyword, you would need to call ContinueWith on the Task object:

没有这个await关键字,你就需要调用Task对象上的ContinueWith:

private void GetProducts(object sender, RoutedEventArgs e)

{

btnGetProducts.IsEnabled = false;

client.GetAsync("api/products/2").ContinueWith((t) =>

{

if (t.IsFaulted)

{

MessageBox.Show(t.Exception.Message);

btnGetProducts.IsEnabled = true;

}

else

{

var response = t.Result;

if (response.IsSuccessStatusCode)

{

response.Content.ReadAsAsync<IEnumerable<Product>>().

ContinueWith(t2 =>

{

if (t2.IsFaulted)

{

MessageBox.Show(t2.Exception.Message);

btnGetProducts.IsEnabled = true;

}

else

{

var products = t2.Result;

_products.CopyFrom(products);

btnGetProducts.IsEnabled = true;

}

}, TaskScheduler.FromCurrentSynchronizationContext());

}

}

}, TaskScheduler.FromCurrentSynchronizationContext());

}

This type of code is difficult to get right, so it's recommended to target .NET 4.5, or if that's not possible, install the Async Targeting Pack.

这种型式的代码难以正确,因此建议把目标定为.NET 4.5,或者,如果这不可能,需安装Async Targeting Pack(Async目标包)。

以上就是重庆云诚科技小编解答(一梦碎今生)贡献关于“如何在WPF应用程序中通过HttpClient调用Web API”的答案,接下来继续为你详解体育用户(再无相见)分析“C#中wpf应用程序”的一些相关解答,希望能解决你的问题!

C#中wpf应用程序

本文最佳回答用户:【再无相见】 ,现在由重庆云诚科技小编为你解答与【php开发项目教程】的相关内容!

优质回答按我说的做:

1. 打开Visual Studio 2008(05也成),新建一个WPF项目(别忘了选.NET Framework 3.0)

2. 打开Window1.xaml,用下面的代码替换原有的:

<Window x:Class="WpfApplication1.Window1"

xmlns=";

xmlns:x=";

Title="Window1" Height="300" Width="300">

<StackPanel>

<StackPanel Orientation="Horizontal">

<TextBlock Text="输入: "/>

<TextBox Name="TbInput" Width="100px"/>

<Button Name="BtnCalc" Click="BtnCalc_Click">求平均值</Button>

</StackPanel>

<StackPanel Orientation="Horizontal">

<TextBlock Text="平均值: "/>

<Label Name="LblResult" Foreground="Red" Width="100px">abc</Label>

</StackPanel>

</StackPanel>

</Window>

3. 为BtnCalc_Click事件添加逻辑,打开Window1.xaml.cs,添加:

private void BtnCalc_Click(object sender, RoutedEventArgs e)

{

string input = TbInput.Text.Trim();

if (!String.IsNullOrEmpty(input))

{

string[] rawDataSet = input.Split(", ".ToCharArray());

if (rawDataSet.Length != 5)

return;

try

{

int i = 0;

double sum = 0;

for (; i < rawDataSet.Length; i++)

sum += Double.Parse(rawDataSet[i]);

LblResult.Content = sum / i;

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

}

4. 运行程序,在文本框中输入五个数,中间用空格或逗号分开,然后点计算平均值按钮,即可显示结果。

注意:如果编译没过,请到Window1.xaml中找到:

<Button Name="BtnCalc" Click="BtnCalc_Click">

然后,删掉Click="BtnCalc_Click",再键入Click=,按界面提示添加一个新的事件处理方法,最后粘贴进第3步中的代码(不包括方法签名)即可。

以上就是重庆云诚科技小编解疑贡献者:(再无相见)贡献的关于“C#中wpf应用程序”的问题了,不知是否已经解决你的问题?如果没有,下一篇内容可能是你想要的答案,下面继续解读下文用户【轻寒轻似梦】解答的“WPF高级编程的内容简介”的一些相关疑问做出分析与解答,如果能找到你的答案,可以关注本站。

WPF高级编程的内容简介

本文最佳回答用户:【轻寒轻似梦】 ,现在由重庆云诚科技小编为你讲解与【php开发项目教程】的相关内容!

优质回答《WPF高级编程》主要介绍WPF开发技术、模式和案例。

《WPF高级编程》以“WPF概述”作为开始。在第1章中,首先回答了“WPF是什么?”、“如何开始使用WPF?”以及“WPF能够带来什么?”等几个问题,然后详细分析了WPF开发平台提供的各个子系统及其图形特征。

概述WPF之后,将进入WPF开发技术部分。《WPF高级编程》通过使用Visual Studio创建几个示例程序,带领您快速进入应用程序开发。此外,还将介绍XAML—— 创建用户界面的新标记语言。XAML实际上是独立于WPF的一项技术,但在WPF中广泛使用了XAML。

掌握了WPF的基本概念和开发技术之后,《WPF高级编程》将带领读者进入设计工具的世界。Microsoft提供了许多引人注目的新的设计工具系列。在《WPF高级编程》中将学习使用新的Microsoft Expression Blend工具。通过使用Expression Blend,可以创建高级的用户界面,学习实现样式、布局与动画。读者还将发现,将界面设计保存到XAML文件中,然后在Visual Studio中使用完全相同的标记语言编写应用程序逻辑是多么的容易。

接下来,将介绍如何使用WPF创建特殊效果,包括位图效果、变换,以及使用画刷对象创建玻璃或反射效果等。《WPF高级编程》演示了这些技术,并为在应用程序中创建绚丽的可视化元素提供了基础。

在学习完特殊效果之后,将介绍如何使用WPF创建自定义控件。WPF提供了一个极好的自定义对象模型,允许运用各种风格和已存在的各种元素模板。该模型是一个令人满意的新模型,允许将几乎任何元素放置在另一个元素中。通过这个新功能,在整个创建自定义控件的过程中,都不会遇到问题。当然,任何事物都不是绝对的,使用WPF创建自定义控件,也有可能会遇到一些问题。本部分内容包括在什么情况下需要考虑创建自定义控件,以及如何创建自定义控件。

接下来,将进入WPF应用程序的企业应用开发部分。使用WPF可以创建两种风格的应用程序:基于Windows的单机运行的应用程序与基于Web的应用程序。这两种类型的程序基于相同的代码,即XAML与.NET。这意味着为应用程序指定目标主机环境,只需要简单改变工程文件的设置并管理这些配置即可。这项功能是非常强大的,在《WPF高级编程》的企业开发主题中,对这一功能进行了全面介绍。

在理解了应用程序模型与配置之后,读者可能想了解安全问题。WPF的安全基于.NET 2.0的CAS安全模型,与所选择的应用程序模型以及运行环境也有关。如果WPF程序运行于浏览器中,将涉及到Internet区域安全设置。《WPF高级编程》将深入介绍这些内容。

在探讨了WPF应用程序开发、配置基础,并全面分析了WPF应用程序的安全之后,《WPF高级编程》将进入一些高级主题。这些高级主题之一就是如何混合使用Win32与WPF代码。《WPF高级编程》中将涉及互操作的相关问题。在Win32程序中使用WPF以及在WPF程序中使用Win32都是可行的,《WPF高级编程》将介绍如何实现这一特征,从而可以使读者快速掌握如何将WPF用于Win32程序中。

《WPF高级编程》最后深入研究了体系结构、WPF架构、XAML以及多线程问题。此外,还介绍了Windows Workflow Foundation(WF)与Windows Communication Foundation(WCF),从而使读者可以熟悉.NET Framework 3.0的其他组件。在理解了这些重要组件之后,读者还将学习如何构建一个简单的WCF服务与一个简单的WF工作流应用程序。

关于[php开发项目教程]的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php开发项目教程、wpf应用开发项目教程的信息别忘了在本站进行查找喔。

推荐文章:

  • 膜的组词和部首,膜的组词和拼音是什么
  • 亲字组词100个 亲字的组词有
  • 酬谢是什么意思,定当酬谢是什么意思
  • 异的组词和部首,株的组词和部首
  • 箭组词和拼音 耸组词和拼音部首
  • 有志不在年高的意思-有志不在年高的意思雨来表达了
  • 繁衍的意思 繁衍的意思简单解释
  • 彤组词,胀组词和拼音
  • 血泊的拼音 泊的拼音
  • 螺组词拼音 螺的组词和拼音
  • 本文由网上采集发布,不代表我们立场,转载联系作者并注明出处:https://www.cqycseo.com/zixun/5740.html

    联系我们