【发布时间】:2017-07-03 05:08:37
【问题描述】:
已解决
我一直在寻找,但找不到答案,所以我想它会帮助更多的人。
我发现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 文件结合起来。
如果有人可以展示如何实现它会很棒。
谢谢。
【问题讨论】:
-
当按下按钮时,您应该能够将
Console.WriteLine("yo man");替换为您要运行的代码。您是否尝试过,或者有什么理由不适合您? -
嘿伙计,对不起,我的代码部分只是为了检查我是否知道打印消息,这是我的错。我试图插入 readurl 的东西,但即使在
using System.Net之后我也遇到了WebClient的问题 -
无需道歉。您能否编辑您的问题以包含插入
WebClient代码时可能遇到的任何编译错误? -
我已经设法解决了这些问题!谢谢你,小伙伴。编辑代码以防人们需要。 @NikolajDamLarsen
标签: c# visual-studio xaml user-interface blend