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 System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Windows.Controls.Theming;
using System.Threading;
using System.ComponentModel;
namespace ComputerDropDragControl
{
/// <summary>
/// “机房”界面
/// 创建人:吴兆娟
/// 创建时间:2011-10-24
/// </summary>
public partial class ComputerRoom : UserControl
{
#region <<页面加载>>
private BackgroundWorker bgWorker = new BackgroundWorker();
public ComputerRoom(string roomID, UIElement ui)
{
InitializeComponent();
BackElement = ui;
RoomID = roomID;
//加载数据
LoadData();
LoadDataDetail();
//初始化界面
InitForm();
//加载界面
LoadRow();
}
/// <summary>
/// 初始化界面
/// </summary>
private void InitForm()
{
//tbOne.Text = RoomID;
}
#endregion
#region <<控件事件>>
/// <summary>
/// “排头”点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnHead_Click(object sender, RoutedEventArgs e)
{
//((Waiting)((Grid)((StackPanel)BackElement).Parent).Children[1]).Visibility = Visibility.Visible;
#region <<使用BackgroundWorker类>>
获取“目标传递参数”
//Button btn = (Button)sender;
//TextBlock tb = (TextBlock)btn.Content;
//Room entity = (Room)tb.DataContext;
//string rackID = entity.RowID;
((StackPanel)BackElement).Children.Clear();
ComputerRack a = new ComputerRack(rackID.ToString());
((StackPanel)BackElement).Children.Add(a);
BackgroundWorker是否支持取消运行异步操作
//bgWorker.WorkerSupportsCancellation = true;
加载DoWork,RunWorkerCompleted事件
//bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
//bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
//bgWorker.RunWorkerAsync(rackID);
#endregion
#region
//Thread test = new Thread(new ThreadStart(beginTest));
这是前台线程还是后台线程呢?
//WaitingClass.ShowWaiting();
获取“目标传递参数”
//Button btn = (Button)sender;
//TextBlock tb = (TextBlock)btn.Content;
//Room entity = (Room)tb.DataContext;
//string rackID = entity.RowID;
带参数的线程
//ParameterizedThreadStart ParStart = new ParameterizedThreadStart(test);
//Thread myThread = new Thread(ParStart);
//myThread.IsBackground = true;
//object o = rackID;
//myThread.Start(o);
//Thread temp = new Thread(new ThreadStart(ShowWaiting));
//temp.IsBackground = false;
StackPanel stackMark = new StackPanel();
stackMark.Background = new SolidColorBrush(Colors.Black);
stackMark.Opacity = 0.5;
((Grid)StaticClass.UIRoot).Children.Insert(1, stackMark);
((Border)((StackPanel)StaticClass.ProgressBarStatic).Parent).Visibility = Visibility.Visible;
StaticClass.UISB.Begin();
//((Border)((ShinyRedTheme)((StackPanel)StaticClass.ProgressBarStatic).Parent).Parent).Visibility = Visibility.Visible;
//((ShinyRedTheme)((StackPanel)StaticClass.ProgressBarStatic).Parent).Visibility = Visibility.Visible;
Button btn = (Button)sender;
string currentRowID = ((Equipment)btn.DataContext).ID;
TextBlock tb = (TextBlock)btn.Content;
Room entity = (Room)tb.DataContext;
string rackID = entity.RowID;
((StackPanel)BackElement).Children.Clear();
ComputerRack a = new ComputerRack(rackID.ToString());
((StackPanel)BackElement).Children.Add(a);
//“设备树”相关操作
Grid gridTreeView = ((Grid)((ScrollViewer)StaticClass.EquipmentTreeViewScroll).Content);
TreeView equipmentTreeView = (TreeView)gridTreeView.Children[0];
foreach (TreeViewItem item in equipmentTreeView.Items)//知道的肯定是二级菜单的做法
{
foreach (TreeViewItem secondItem in item.Items)//进入“二级菜单”
{
string tempRowID = ((Equipment)secondItem.DataContext).ID;
if (tempRowID == currentRowID)
{
secondItem.IsSelected = true;
return;
}
}
}
#endregion
}
private void ShowWaiting()
{
WaitingClass.ShowWaiting();
}
#region
//后台异步执行操作的地方
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (bgWorker.CancellationPending == false)
{
//如果没有被取消异步操作,则线程阻塞以下,然后报告给bgWorker_ProgressChanged事件
//MessageBox.Show("在做事呢啊!");
//获取到传递进来的参数
string rackID = e.Argument.ToString();
e.Result = e.Argument.ToString();
//开辟的后台线程,执行后台运算
((StackPanel)BackElement).Dispatcher.BeginInvoke(() =>
{
((StackPanel)BackElement).Children.Clear();
WaitingClass.ShowWaiting();
ComputerRack a = new ComputerRack(rackID.ToString());
((StackPanel)BackElement).Children.Add(a);
});
//ComputerRack a = new ComputerRack(rackID.ToString());
//e.Result = a;
//Thread.Sleep(50);
}
else
{
//设置当前已取消异步线程操作
e.Cancel = true;
}
执行100次循环
//for (int i = 0; i < 100; i++)
//{
// //判断当前BackgroundWorker是否已经被取消了异步操作
// if (bgWorker.CancellationPending == false)
// {
// //如果没有被取消异步操作,则线程阻塞以下,然后报告给bgWorker_ProgressChanged事件
// Thread.Sleep(50);
// bgWorker.ReportProgress(i + 1);
// e.Result = "3322";
// }
// else
// {
// //设置当前已取消异步线程操作
// e.Cancel = true;
// break;
// }
//}
}
//异步操作执行完毕之后的处理
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show("做完了啊!");
WaitingClass.HideWaiting();
//ComputerRack cRack = (ComputerRack)e.Result;
//string rackID = e.Result.ToString();
//ComputerRack a = new ComputerRack(rackID.ToString());
//((StackPanel)BackElement).Dispatcher.BeginInvoke(new labelDelegete(beginTest), rackID);
//完成异步操作之后检查是否取消了异步操作,弹出相关提示
//if (e.Cancelled == true)
//{
// MessageBox.Show("取消异步线程操作");
//}
//else
//{
// MessageBox.Show("异步线程执行完毕" + e.Result);
//}
//this.btncancel.Content = "已完成";
}
else
{
MessageBox.Show(e.Error.Message);
}
}
public delegate void RackDelegete(ComputerRack entity);
public void AddToUI(ComputerRack a)
{
((StackPanel)BackElement).Children.Clear();
((StackPanel)BackElement).Children.Add(a);
}
#endregion
public void test(object rackID)
{
//开辟的后台线程,执行后台运算
((StackPanel)BackElement).Children.Clear();
ComputerRack a = new ComputerRack(rackID.ToString());
((StackPanel)BackElement).Children.Add(a);
//((StackPanel)BackElement).Dispatcher.BeginInvoke(new labelDelegete(beginTest), rackID);
//testLabel.Dispatcher.BeginInvoke(new labelDelegete(beginTest), rackID);
}
public delegate void labelDelegete(string rackID);
public void beginTest(object rackID)
{
//开辟的后台线程,执行后台运算
((StackPanel)BackElement).Children.Clear();
ComputerRack a = new ComputerRack(rackID.ToString());
((StackPanel)BackElement).Children.Add(a);
//Thread.Sleep(5000);
//testLabel.Content = rackID.ToString();
}
/// <summary>
/// “机排”点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
Frame entity = (Frame)btn.DataContext;
((StackPanel)BackElement).Children.Clear();
ComputerRackSingle cr = new ComputerRackSingle("f1_r1");//先写死
((StackPanel)BackElement).Children.Add(cr);
}
#endregion
#region <<辅助方法>>
/// <summary>
/// 加载界面
/// </summary>
private void LoadRow()
{
List<Room> result = (from room in rList
where room.RoomID == RoomID
select room).ToList<Room>();
foreach (Room room in result)
{
LoadRowDetail(room);
}
}
/// <summary>
/// 加载行的明细
/// </summary>
/// <param name="rowNo"></param>
private void LoadRowDetail(Room entity)
{
List<Frame> result = (from frame in fList
where frame.RoomID == entity.RowID
select frame).ToList<Frame>();
Grid gridFather = new Grid();
gridFather.Margin = new Thickness(3, 0, 0, 10);
Rectangle recOne = new Rectangle();
recOne.Width = 100 * result.Count;
recOne.Height = 125;
recOne.Margin = new Thickness(6, 7, 0, 0);
recOne.Stroke = new SolidColorBrush(Colors.Black);
recOne.Stroke.Opacity = 0.1;
recOne.Fill = new SolidColorBrush(Colors.Black);
recOne.Fill.Opacity = 0.3;
recOne.VerticalAlignment = VerticalAlignment.Bottom;
recOne.HorizontalAlignment = HorizontalAlignment.Left;
gridFather.Children.Add(recOne);
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(1);
border.Height = 125;
border.HorizontalAlignment = HorizontalAlignment.Left;
border.VerticalAlignment = VerticalAlignment.Top;
//border.Margin = new Thickness(7, 0, 0, 10);
Grid RackGrid = new Grid();
RackGrid.Background = new SolidColorBrush(Colors.White);
RackGrid.Height = 125;
RackGrid.HorizontalAlignment = HorizontalAlignment.Left;
RackGrid.ShowGridLines = false;
RackGrid.Width = 100 * result.Count;
border.Width = 100 * result.Count;
RowDefinition rd1 = new RowDefinition();
rd1.Height = new GridLength(25);
RowDefinition rd2 = new RowDefinition();
rd2.Height = new GridLength(100);
RackGrid.RowDefinitions.Add(rd1);
RackGrid.RowDefinitions.Add(rd2);
StackPanel stackHead = new StackPanel();
stackHead.SetValue(Grid.ColumnSpanProperty, entity.FrameCount);
stackHead.SetValue(Grid.RowProperty, 0);
stackHead.Height = 25;
stackHead.Background = new SolidColorBrush(Colors.LightGray);
TextBlock tbHeader = new TextBlock();
tbHeader.Text = entity.RowNo;
tbHeader.DataContext = entity;
tbHeader.Margin = new Thickness(-100 * result.Count / 2 + 13, 0, 0, 0);
Button btnHead = new Button();
btnHead.Content = tbHeader;
btnHead.Height = 25;
btnHead.FontSize = 15;
btnHead.VerticalAlignment = VerticalAlignment.Center;
btnHead.DataContext = new Equipment(entity.RowID, entity.RoomID, entity.RowNo, "排头", false);
btnHead.Click += new RoutedEventHandler(btnHead_Click);
stackHead.Children.Add(btnHead);
RackGrid.Children.Add(stackHead);
for (int i = 0; i < result.Count; i++)
{
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(100);
RackGrid.ColumnDefinitions.Add(c1);
Button btn = new Button();
//btn.SetValue(Grid.ColumnProperty, i);
//btn.SetValue(Grid.RowProperty, 1);
btn.Width = 80;
btn.Height = 80;
btn.Content = result[i].FrameName;
btn.Click += new RoutedEventHandler(btn_Click);
btn.DataContext = result[i];
//RackGrid.Children.Add(btn);
TwilightBlueTheme blueTheme = new TwilightBlueTheme();
blueTheme.Content = btn;
blueTheme.Background = new SolidColorBrush(Colors.White);
blueTheme.SetValue(Grid.ColumnProperty, i);
blueTheme.SetValue(Grid.RowProperty, 1);
RackGrid.Children.Add(blueTheme);
}
border.Child = RackGrid;
gridFather.Children.Add(border);
stackOne.Children.Add(gridFather);
}
#endregion
/// <summary>
/// 机房ID
/// </summary>
public string RoomID { get; set; }
public UIElement BackElement { get; set; }
public ObservableCollection<Room> rList;
public ObservableCollection<Frame> fList;
#region <<定义实体类>>
public class Room
{
public Room(string roomID, string rowID, string rowNo, int frameCount)
{
RoomID = roomID;
RowID = rowID;
RowNo = rowNo;
FrameCount = frameCount;
}
public string RoomID { get; set; }
public string RowID { get; set; }
public string RowNo { get; set; }
public int FrameCount { get; set; }
}
private ObservableCollection<Room> LoadData()
{
return rList = new ObservableCollection<Room>
{
new Room("1","2","r1_1",4),
new Room("1","3","r2_1",4),
new Room("1","4","r3_1",3),
new Room("5","6","r1_5",7),
new Room("5","7","r2_5",8),
new Room("8","9","r1_8",9)
};
}
public class Frame
{
public Frame(string frameID, string frameName, string rowNo, string roomID)
{
FrameID = frameID;
FrameName = frameName;
RowNo = rowNo;
RoomID = roomID;
}
public string FrameID { get; set; }
public string FrameName { get; set; }
public string RowNo { get; set; }
public string RoomID { get; set; }
}
private ObservableCollection<Frame> LoadDataDetail()
{
return fList = new ObservableCollection<Frame>
{
new Frame("f1_r1","A-1","r1_1","2"),
new Frame("f2_r1","A-2","r1_1","2"),
new Frame("f3_r1","A-3","r1_1","2"),
new Frame("f4_r1","A-4","r1_1","2"),
new Frame("f1_r2","A-1","r2_1","3"),
new Frame("f2_r2","A-2","r2_1","3"),
new Frame("f3_r2","A-3","r2_1","3"),
new Frame("f4_r2","A-4","r2_1","3"),
new Frame("f1_r3","A-1","r3_1","4"),
new Frame("f2_r3","A-2","r3_1","4"),
new Frame("f3_r3","A-3","r3_1","4"),
new Frame("f1_r5","A-1","r1_5","6"),
new Frame("f2_r5","A-2","r1_5","6"),
new Frame("f3_r5","A-3","r1_5","6"),
new Frame("f1_r5","A-4","r1_5","6"),
new Frame("f2_r5","A-5","r1_5","6"),
new Frame("f3_r5","A-6","r1_5","6"),
new Frame("f3_r5","A-7","r1_5","6"),
new Frame("f1_r5","A-1","r2_5","7"),
new Frame("f2_r5","A-2","r2_5","7"),
new Frame("f3_r5","A-3","r2_5","7"),
new Frame("f1_r5","A-4","r2_5","7"),
new Frame("f2_r5","A-5","r2_5","7"),
new Frame("f3_r5","A-6","r2_5","7"),
new Frame("f2_r5","A-7","r2_5","7"),
new Frame("f3_r5","A-8","r2_5","7"),
new Frame("f1_r5","A-1","r1_8","9"),
new Frame("f2_r3","A-2","r1_8","9"),
new Frame("f3_r5","A-3","r1_8","9"),
new Frame("f1_r5","A-4","r1_8","9"),
new Frame("f2_r5","A-5","r1_8","9"),
new Frame("f3_r5","A-6","r1_8","9"),
new Frame("f1_r5","A-7","r1_8","9"),
new Frame("f2_r5","A-8","r1_8","9"),
new Frame("f2_r5","A-9","r1_8","9")
};
}
#endregion
}
}