0
解决运行C#代码时,按钮在Blend的Visual Studio 2015年单击
我一直在寻找了一段时间,所以我想它会帮助几个人也找不到答案。
我发现blend
是一个非常棒的地方,可以制作出漂亮的UI。
我已经在Blend从Visual Studio做了一个简单button
2015年
这个按钮的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文件结合起来。
如果有人可以展示如何实现它将是非常棒的。
谢谢。
的代码,您应该简单地可以只需更换'Console.WriteLine(“哟男子”);'你要运行的代码,当按钮按下。你有没有尝试过,或者有什么理由为什么这不适合你? –
嘿,队友,对不起,这是我的代码的一部分,只是为了检查我是否知道打印信息。我试图插入readurl的东西,但即使在使用System.Net后,即使在'WebClient'上也有问题。 – Ben
不需要道歉。你能编辑你的问题来包含插入你的WebClient代码时可能遇到的任何编译错误吗? –