简体   繁体   English

在Blend Visual Studio 2015中单击按钮时运行C#代码

[英]Run C# code when button clicked in Blend Visual Studio 2015

Solved 解决了

I have been looking for a while and couldn't find an answer so I guess it will help few more people. 我已经寻找了一段时间,但找不到答案,所以我想它将对更多人有用。

I have found blend is an awesome place to make a nice looking UI. 我发现blend是制作美观UI的绝佳场所。

I have made a simple button in Blend from Visual Studio 2015. 我在Visual Studio 2015的Blend中做了一个简单的button

The XAML of this button looks like this: 此按钮的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>

and the cs file looks like this: 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);
        }
    }
}

Now, when I click the button I had like it to run the following code for reading URL (which is written in C#) 现在,当我单击按钮时,我希望它运行以下代码来读取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);

I'm pretty new in C# and I'm not sure how to combine a code with XAML file. 我是C#的新手,我不确定如何将代码与XAML文件结合在一起。

If someone can show how to implement it will be awesome. 如果有人可以展示如何实现,那将是很棒的。

Thank you. 谢谢。

Perhaps im missing something but it doesnt appear to be wired up. 也许我错过了一些东西,但似乎并没有连接起来。

<Grid>
    <Button x:Name="button" Content="Button" Margin="56.471,79.283,54.639,68.382" Click="button_Click"/>
</Grid>

Then in the code behind 然后在后面的代码中

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM