小编MrD*_*ium的帖子

DataGridView CheckBox 列不会将更改应用于底层绑定对象

我有一个Record对象列表,用作我的 DataGridView 中的行。每个Record都有一个新的 bool 值Helped。果然,这个新值在我的表单中显示为复选标记。

当它目前时,检查此框时,似乎似乎Helped在相应的BOOL中尚未更改BOOL的值Record

我需要更改某种只读属性吗?如何将我的表单上的点击作为其值的变化传回DataSource

编辑:我已经找到了该System.Windows.Forms.DataGridViewEditMode.EditOnEnter属性,但我仍然没有看到我的Record.Helped属性得到更新。

c# checkbox datagridview datagridviewcheckboxcell winforms

4
推荐指数
1
解决办法
2134
查看次数

使用Take()从字节数组中获取字节,在Take()之后转换回byte []时遇到问题

byte[] result = memStream.ToArray();                              
memStream.Close();
byte[] temp = result.Take(255);
var str = System.Text.Encoding.Default.GetString(temp);
Run Code Online (Sandbox Code Playgroud)

上面的失败就result.Take(255);行了.它说我不能转换IEnumerablebyte[],并询问如果我错过了一个演员.

我是C#的新手,不确定该怎么做.

c# take

3
推荐指数
1
解决办法
1841
查看次数

从Winforms中的外部线程访问UI

在WPF中,可以使用类似以下内容的方法:

Application.Current.Dispatcher.BeginInvoke(new Action(() => Form1.grid.Items.Refresh()));
Run Code Online (Sandbox Code Playgroud)

访问主线程之外的UI函数。但是,在Winforms中,没有相同的功能。从我的“工作”线程访问在Form1类内部存在的BindingList的最简单方法是什么?当前,当我尝试访问“ Form1.record_list”时收到以下错误:

System.InvalidOperationException: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
Run Code Online (Sandbox Code Playgroud)

编辑:到目前为止,我感谢您的帮助,但是我对“ this.Invoke”迷失了。我在单独线程中的方法没有“调用”。

到目前为止,这是我的代码示例。

        public static void listen(IPEndPoint server_ip)
    {
        Console.WriteLine("In listen");
        while (true)
        {
            try
            {
                byte[] received_bytes = udp_client.Receive(ref server_ip);
                string received_data = Encoding.ASCII.GetString(received_bytes);
                Record record = JsonConvert.DeserializeObject<Record>(received_data);
                Form1.record_list.Add(record); //This is where I assume the problem spawns
            }

            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }




public partial class Form1 : Form
{ …
Run Code Online (Sandbox Code Playgroud)

c# winforms

1
推荐指数
1
解决办法
3143
查看次数

在多人游戏中,如何通过套接字有效地发送位置数据?

我目前正在为Unity游戏构建我自己的网络代码(用于学习体验),并且我在"解码数据包"方面遇到了一些严重的延迟.

基本上,我playerObject将它们的位置数据(Vector3(x,y,z))作为JSON字符串发送到服务器,然后服务器将其发送回所有其他播放器.

事物的插座方面很顺利.从创建数据包到收到数据包的延迟非常小.

但是,我让我的客户,当他们试图取消JSON的远程客户端的位置,一个巨大的延迟:

Vector3 remotePos = JsonUtility.FromJson<Vector3>(_command.Substring(5 + _usernameLength));

Json字符串在字符串的开头有一个标识符,表示它是一个Position更新,后跟两个表示用户名长度的数字,然后是用户名(这样远程客户端可以更新正确的playerObject.整个字符串看起来这样的事情.

POS09NewPlayer{"x":140.47999572753907,"y":0.25,"z":140.7100067138672}

从服务器收到此类数据包后,我的客户将执行以下任务:

        int _usernameLength = Int32.Parse(_command.Substring(3, 2));
        string _username = _command.Substring(5, _usernameLength);
        Vector3 remotePos = JsonUtility.FromJson<Vector3>
        if (_username != username)
        {
        playerDict[_username].transform.position = remotePos;
        } 
Run Code Online (Sandbox Code Playgroud)

所有这些都"有效",但在3个客户端同时连接后变得非常迟缓.

我究竟做错了什么?必须有一个重大的缺陷,因为我只为每个.015秒发送3个玩家的更新,其中像战地这样的游戏可以为64个玩家每秒发送60个更新!

任何意见,将不胜感激.

c# multiplayer game-development

1
推荐指数
1
解决办法
2964
查看次数