学点儿c#语言wpf开发

首发:个人博客,更新&纠错&回复

visual studio 2015,界面越来越漂亮了。

比起swift和python啥的,还是c#外观上更像java,windows的界面编程,wpf的方式跟android等编程也蛮像,声明式的界面定义,界面和controller类当然绑定,总之玩着很爽就是了。

现在在想的问题是想个啥主题做着……

下面的代码是学习关键点时练的,老规矩,界面交互+web请求+数据库访问——

using MySql.Data.MySqlClient;
using qdf_test_wpf_1;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
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;

namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

private void button_Click(object sender, RoutedEventArgs e)
        {

//进行web请求
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://427studio.net/api/blog");
            httpRequest.Timeout = 2000;
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
            string result = sr.ReadToEnd();
            result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            int status = (int)httpResponse.StatusCode;
            sr.Close();

//解析json
            List<Person> ps = JSON.parse<List<Person>>(result);
            foreach (Person p in ps)
            {
                Console.WriteLine(p.title);
            }
            jsonCount.Content = "js数组长度:" + ps.Count();

//数据库连接
            string str = "Server=那啥;User ID=那啥;Password=那啥;Database=那啥;CharSet=那啥;";
            MySqlConnection conn = new MySqlConnection(str);//实例化链接
            conn.Open();//开启连接
            string strcmd = "select id, name from admin_user";
            MySqlCommand cmd = new MySqlCommand(strcmd, conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            int rowCount = 0;
            while (reader.Read())
            {
                String title = reader.GetString("name");
                //Console.WriteLine(title);

rowCount++;
            }
            dbCount.Content = "数据库记录行数:" + rowCount;
            reader.Close();
            conn.Close();

//文件系统
            FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("由wpf程序写入!");
            sw.Close();
        }
    }
}

长期欢迎项目合作机会介绍,项目收入10%用于酬谢介绍人。新浪微博:@冷镜,QQ:908789432
上一篇:ldd查看可执行程序依赖的文件


下一篇:关于singleton的几个实现