• 一段完整的Silverlight演示代码[Page.xaml.cs]


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using SilverlightInterview.DataService;
    using Visifire.Charts;
    using Visifire.Commons;
    using SilverlightInterview.StockService;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;


    namespace SilverlightInterview
    {
        public partial class Page : UserControl
        {
            private TextBox tb;
            private Button btn;
            int clickCount = 0;
            string TopicByDel;
            int i = 0;
            Random rand = new Random(DateTime.Now.Millisecond);
            List<double> ChartValues;
            List<string> ChartKeys;


            public Page()
            {
                InitializeComponent();
                s2.Height = 1;
                ChartValues = new List<double>();
                ChartKeys = new List<string>();
                for (int i = 0; i < 6; i++)
                {
                    ChartValues.Add(i*10);
                    ChartKeys.Add("网站"+i.ToString());

                }
                    CreatChart();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                DataServiceSoapClient ds = new DataServiceSoapClient();
                ds.AddNoteCompleted += new EventHandler<AddNoteCompletedEventArgs>(ds_AddNoteCompleted);
                ds.AddNoteAsync(topic.Text,remark.Text);
                GetList();
              
            }

            public void StartTimer(object o, RoutedEventArgs sender)
            {
                System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 5000); // 100 Milliseconds
                myDispatcherTimer.Tick += new EventHandler(GetMyChart);
                myDispatcherTimer.Start();
            }


            public void GetMyChart(object o, EventArgs sender)
            {
               
                //UpdateChart();

                GetService();
                UpdateChartByASMX();
            }


            private void CreatChart()
            {
                Chart chart = new Chart();

                chart.Width = 500;
                chart.Height = 300;

                Title title = new Title();

                title.Text = "股市行情表";

                chart.Titles.Add(title);

                DataSeries dataSeries = new DataSeries();

                dataSeries.RenderAs = RenderAs.Column;

                DataPoint dataPoint;

                for (int i = 0; i < 4; i++)
                {
                    dataPoint = new DataPoint();
                    dataPoint.YValue = rand.Next(10, 100);
                    dataSeries.DataPoints.Add(dataPoint);
                }

                chart.Series.Add(dataSeries);
                chart.Name = "mychart";
                chart.Width = 800;
                SP3.Children.Add(chart);

            }

            private void UpdateChart()
            {
                Chart chart = (Chart)SP3.FindName("mychart");
               
                chart.Series.Clear();
                chart.Titles.Clear();

                Title title = new Title();
                title.Text = "股市行情表:"+System.DateTime.Now.ToString();
                chart.Titles.Add(title);

                DataPoint dataPoint;
                DataSeries dataSeries = new DataSeries();

                dataSeries.RenderAs = RenderAs.Column;

                for (int i = 0; i < 5; i++)
                {
                    dataPoint = new DataPoint();
                    dataPoint.YValue = rand.Next(10, 100);
                    dataSeries.DataPoints.Add(dataPoint);
                }
                chart.Series.Add(dataSeries);
            }

            private void GetService()
            {

                StockServiceSoapClient ws = new StockServiceSoapClient();
                ws.GetStockCompleted += new EventHandler<GetStockCompletedEventArgs>(ws_GetStockCompleted);
                ws.GetStockAsync();
            }

            void ws_GetStockCompleted(object sender, GetStockCompletedEventArgs e)
            {

                if (e.Error == null)
                {
                    XElement doc = XElement.Load(new StringReader(e.Result));
                    GetAllValues(doc);
                   

                    //using (XmlReader Reader = XmlReader.Create(new StringReader(e.Result)))
                    //{
                    //    ChartValues = new List<double>();
                    //    ChartKeys = new List<string>();
                    //    for (int i = 0; i < 4; i++)
                    //    {
                    //        //Reader.ReadToFollowing("value");
                    //        //ChartValues.Add(Convert.ToInt32(Reader.ReadInnerXml()));
                    //        //Reader.ReadToFollowing("name");
                    //        //ChartKeys.Add(Reader.ReadElementContentAsString());

                    //        Reader.ReadToFollowing("value");
                    //        ChartValues.Add(Reader.ReadElementContentAsDouble());

                    //        //Stock1.Text += ChartValues[i] + "-";
                    //    }
                    //}
                }
            }

            private void GetAllValues(XElement doc)
            {
                ChartValues = new List<double>();
                ChartKeys = new List<string>();

                var myValues = from pv in doc.Descendants("value")
                               select pv.Value;
                foreach (var v1 in myValues)
                    ChartValues.Add(Convert.ToDouble(v1));

                var myKeys = from pn in doc.Descendants("name")
                             //where pn.Value == "方欣科技"
                               select pn.Value;
                foreach (var v1 in myKeys)
                    ChartKeys.Add(Convert.ToString(v1));
            }

            private void UpdateChartByASMX()
            {

                Chart chart = (Chart)SP3.FindName("mychart");

                chart.Series.Clear();
                chart.Titles.Clear();

                Title title = new Title();
                title.Text = "股市走势:" + System.DateTime.Now.ToString();
                chart.Titles.Add(title);

                DataPoint dataPoint;
                DataSeries dataSeries = new DataSeries();
                dataSeries.RenderAs = RenderAs.StackedColumn;


                for (int i = 0; i < 6; i++)
                {
                    dataPoint = new DataPoint();
                    dataPoint.AxisXLabel = ChartKeys[i];
                    dataPoint.YValue = ChartValues[i];
                    dataSeries.DataPoints.Add(dataPoint);
                }
                chart.Series.Add(dataSeries);

            }


            void ds_AddNoteCompleted(object sender, AddNoteCompletedEventArgs e)
            {
                msg.Text = "添加成功!";
            }

            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                GetList();
            }

            private void GetList()
            {
                DataServiceSoapClient ds = new DataServiceSoapClient();
                ds.GetNotesByTopicCompleted += new EventHandler<GetNotesByTopicCompletedEventArgs>(ds_GetNotesByTopicCompleted);
                ds.GetNotesByTopicAsync("*");
               

            }

            void ds_GetNotesByTopicCompleted(object sender, GetNotesByTopicCompletedEventArgs e)
            {
                msg.Text = "读取成功!";
                //myGrid.DataContext=null;
                myGrid.ItemsSource = e.Result;
                myGrid1.ItemsSource = e.Result;
               
            }

            private void Button1_Click(object sender, RoutedEventArgs e)
            {

            }

            private void myGrid_MouseEnter(object sender, MouseEventArgs e)
            {
                myGrid.Opacity = 1;
            }

            private void myGrid_MouseLeave(object sender, MouseEventArgs e)
            {
                myGrid.Opacity = 0.5;
            }

            private void s1_MouseEnter(object sender, MouseEventArgs e)
            {
                colorStoryboard2.Begin();
            }

            private void Slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                if (e.NewValue != e.OldValue)
                {
                    //e.NewValue;?
                    //myGrid.Width = Slider1.Value;
                }
            }

            private void Button_Click_2(object sender, RoutedEventArgs e)
            {

                (sender as Button).IsEnabled = false;
                s2.Height = 30;

                clickCount ++;
                tb = null;
                btn = null;
                opacityStoryboard1.Begin();
                tb = new TextBox();
                tb.Name = "tbSearch";
                tb.Width = 260;
                s2.Children.Add(tb);

                btn = new Button();
                btn.Name = "btnSearch";
                btn.Content = "...开始搜索" + clickCount.ToString();
                btn.Click += new RoutedEventHandler(btn_Click);
                s2.Children.Add(btn);

            }

            void btn_Click(object sender, RoutedEventArgs e)
            {
                GetSearchResult(((TextBox)s2.FindName("tbSearch")).Text);
            }

            private void GetSearchResult(string searchContent)
            {
                DataServiceSoapClient ds = new DataServiceSoapClient();
                ds.GetNotesByTopicCompleted += new EventHandler<GetNotesByTopicCompletedEventArgs>(ds_SearchByTopicCompleted);
                ds.GetNotesByTopicAsync(searchContent);


            }

            void ds_SearchByTopicCompleted(object sender, GetNotesByTopicCompletedEventArgs e)
            {
                msg.Text = "搜索完成!";
                myGrid.ItemsSource = e.Result;
                myGrid1.ItemsSource = e.Result;
            }


            private void s2_MouseLeave(object sender, MouseEventArgs e)
            {
                s2.Opacity = 0.1;
            }

            private void s2_MouseEnter(object sender, MouseEventArgs e)
            {
                s2.Opacity = 1;
            }


            private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
            {
                Note bindData = (Note)e.Row.DataContext;
                Button btn1 = myGrid.Columns[3].GetCellContent(e.Row).FindName("ButtonDetail") as Button;
                Button btn2 = myGrid.Columns[3].GetCellContent(e.Row).FindName("ButtonDel") as Button;
               
                btn1.Tag = bindData.ID + "," + bindData.Topic + "," + e.Row.GetIndex();
                //btn1.Content = btn1.Tag;
                btn2.Tag = bindData.ID + "," + bindData.Topic + "," + e.Row.GetIndex();

            }

            private void ButtonDetail_Click(object sender, RoutedEventArgs e)
            {
                Button b = sender as Button;
               
               
                string[] t = b.Tag.ToString().Split(',');
                System.Windows.MessageBox.Show(t[0] + "," + t[1] + "," + t[2], "方欣科技", MessageBoxButton.OKCancel);
            }

            private void ButtonDel_Click_1(object sender, RoutedEventArgs e)
            {
                MessageBoxResult result =
        MessageBox.Show("您确定要删除吗?",
        "方欣科技", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    Button b = sender as Button;
                    string[] t = b.Tag.ToString().Split(',');
                    TopicByDel = t[1];

                    DataServiceSoapClient ds = new DataServiceSoapClient();
                    ds.DelByIDCompleted += new EventHandler<DelByIDCompletedEventArgs>(ds_DelByIDCompleted);
                    ds.DelByIDAsync(t[0]);
                    GetList();
                }

            }

            void ds_DelByIDCompleted(object sender, DelByIDCompletedEventArgs e)
            {
                msg.Text = "已删除:" + TopicByDel;
            }

            private void remark_MouseEnter(object sender, MouseEventArgs e)
            {
                //media1.Position = new TimeSpan(0);   
                //media1.Play();
            }

            private void remark_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                //media1.Position = new TimeSpan(0);
                //media1.Play();
            }


        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    强化学习
    nginx环境准备
    全面解读PHP-数据结构
    全面解读PHP-数据库缓存
    跨域问题的解决方法
    使用 apt-get 清理
    怎样用 Bash 编程:逻辑操作符和 shell 扩展
    怎样用 Bash 编程:语法和工具
    使用 split 命令分割 Linux 文件,使用 cat 合并文件
    通过tar包解压安装docker
  • 原文地址:https://www.cnblogs.com/starcrm/p/1356494.html
Copyright © 2020-2023  润新知