0

有一个小问题ka。有一堂课

    public class PLayer
{
public String Name{get;set;}
public TimeSpan Tax { get; set; }

}

主要形式

 public partial class MainWindow : Window
{
public ObservableCollection<PLayer> PlayersInGame { get; set; }
public ObservableCollection<PLayer> PlayersInGame2 { get; set; }
public ObservableCollection<PLayer> PlayersOnBench { get; set; }

public MainWindow()
{
PlayersInGame = new ObservableCollection<PLayer>();
PlayersInGame2 = new ObservableCollection<PLayer>();

PlayersOnBench = new ObservableCollection<PLayer>();

InitializeComponent();

}


private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 10; i++)
{
String vName = "Игрок" + i.ToString();
PlayersInGame.Add(new PLayer { Name = vName, Tax = new TimeSpan(0) });
}
for (int i = 10; i < 20; i++)
{
String vName = "Игрок" + i.ToString();
PlayersInGame2.Add(new PLayer { Name = vName, Tax = new TimeSpan(0) });
}
Game.Items.Refresh();
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
if (Game.SelectedIndex > -1)
{
var temp = PlayersInGame[Game.SelectedIndex];
//PlayersInGame.RemoveAt(Game.SelectedIndex);
temp.Tax = new TimeSpan(0, 0, 5);
PlayersOnBench.Add(temp);
Game.Items.Refresh();
Bench.Items.Refresh();
}
if (Game2.SelectedIndex > -1)
{
var temp = PlayersInGame2[Game2.SelectedIndex];
//PlayersInGame2.RemoveAt(Game2.SelectedIndex);
temp.Tax = new TimeSpan(0, 0, 5);
PlayersOnBench.Add(temp);
Game2.Items.Refresh();
Bench.Items.Refresh();
}
}

private void timer_Tick(object sender, EventArgs e)
{
foreach (var x in PlayersOnBench)
{
x.Tax -= new TimeSpan(0, 0, 1);
}

List<int> Temp = new List<int>();
for (var i = 0; i < PlayersOnBench.Count; i++)
{
if (PlayersOnBench[i].Tax == TimeSpan.Zero)
{
Temp.Add(i);

}
}
for (int i = Temp.Count - 1; i >= 0; i--)
{
var s = PlayersOnBench[i];



PlayersOnBench.RemoveAt(Temp[i]);
//PlayersInGame.Add(s);
//Game.Items.Refresh();


}
Bench.Items.Refresh();
}
}

在主窗体上,当您单击按钮“Button2_Click”时,会将行添加到 ListView“Bench”,并添加一个计时器。在处理“timer_Tick”时,计时器正在计算添加到“Bench”的所有行。联系 ossushestvlyaetsya 绑定。我的问题是了解二进制序列化,如何将 ListView“Bench”的内容传输到服务器以在 ListView 或 ListBox 中显示。该项目的二进制序列化已用于发送文本字段。

4

1 回答 1

0

您的问题有点不清楚您要达到的目标。一般来说,如果您使用二进制序列化,它会将您的对象转换为字节数组,您需要反序列化才能恢复您的对象。下面是一个示例

    BinaryFormatter m_formatter;
    Byte[] m_stateData;
    List<T> cloned_objList;

    public binaryserializer(List<T> PlayersOnBench)
    {
        if ((!Object.ReferenceEquals(listToClone, null)) && (typeof(T).IsSerializable))
        {
            m_formatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                try
                {
                    m_formatter.Serialize(stream, PlayersOnBench);
                }
                catch { }

                stream.Seek(0, SeekOrigin.Begin);
                m_stateData = stream.ToArray();
            }
        }
    }

    public List<T> BenchStates
    {
        get
        {
            using (MemoryStream stream = new MemoryStream(m_stateData))
            {
                try
                {
                    cloned_objList = (List<T>)m_formatter.Deserialize(stream);
                }
                catch (Exception) { }
            }
            return cloned_objList;
        }
    }
于 2013-07-01T21:30:20.157 回答