![](/img/trans.png)
[英]Visual Studio 2015 C# color dialog for textbox when clicked in
[英]Run C# code when button clicked in Blend Visual Studio 2015
解決了
我已經尋找了一段時間,但找不到答案,所以我想它將對更多人有用。
我發現blend
是制作美觀UI的絕佳場所。
我在Visual Studio 2015的Blend中做了一個簡單的button
。
此按鈕的XAML如下所示:
<Window x:Class="WpfApplication1.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:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" Margin="56.471,79.283,54.639,68.382"/>
</Grid>
</Window>
CS文件看起來像這樣:
using System;
using System.Collections.Generic;
using System.Linq;
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;
using System.Net;
using System.IO;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
WebRequest request =WebRequest.Create("http://www.example.com");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
}
}
}
現在,當我單擊按鈕時,我希望它運行以下代碼來讀取URL(用C#編寫)
WebRequest request = WebRequest.Create("http://www.example.com");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
我是C#的新手,我不確定如何將代碼與XAML文件結合在一起。
如果有人可以展示如何實現,那將是很棒的。
謝謝。
也許我錯過了一些東西,但似乎並沒有連接起來。
<Grid>
<Button x:Name="button" Content="Button" Margin="56.471,79.283,54.639,68.382" Click="button_Click"/>
</Grid>
然后在后面的代碼中
private void button_Click(object sender, RoutedEventArgs e)
{
using (WebClient client = new WebClient())
{
string s = client.DownloadString(url);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.