C# 不同.NET框架中的Tabcontrol问题
两年前,我使用Visual Studio 2010(目标框架:.NET framework 4客户端概要文件)开发了一个Winforms C#应用程序 此应用程序有一个Form1:Form、INotifyPropertyChanged和一个具有多个tabpages的tabcontrol。我将表单的自定义属性绑定到选项卡页上的控件,这些控件在构建表单时不可见。绑定如下所示:C# 不同.NET框架中的Tabcontrol问题,c#,visual-studio-2010,.net-framework-version,C#,Visual Studio 2010,.net Framework Version,两年前,我使用Visual Studio 2010(目标框架:.NET framework 4客户端概要文件)开发了一个Winforms C#应用程序 此应用程序有一个Form1:Form、INotifyPropertyChanged和一个具有多个tabpages的tabcontrol。我将表单的自定义属性绑定到选项卡页上的控件,这些控件在构建表单时不可见。绑定如下所示: BindingSource BndFrom1 = new BindingSource(); BndFrom1.DataSou
BindingSource BndFrom1 = new BindingSource();
BndFrom1.DataSource = typeof(Form1);
BndFrom1.Add(this);
TxtTemperature.DataBindings.Add("Text", BndFrom1, "TemperatureString", true, DataSourceUpdateMode.OnPropertyChanged);
这很有效。在使用.NET Framework 4.6切换到Windows 10后,应用程序突然无法正常工作,在切换到另一个选项卡页时,应用程序速度变慢或完全阻塞。如果我改变了标签页的顺序,第一个标签页总是可以正常工作的。
我试图通过调用
private static void CreateControls(Control control)
{
CreateControl(control);
foreach (Control subcontrol in control.Controls)
{
CreateControl(subcontrol);
}
}
private static void CreateControl(Control control)
{
var method = control.GetType().GetMethod("CreateControl", BindingFlags.Instance | BindingFlags.NonPublic);
var parameters = method.GetParameters();
method.Invoke(control, new object[] { true });
}
在所有选项卡页上,但这不起作用。在我删除了tabpages上所有带有控件的数据绑定之后,问题就解决了 我有我的应用程序的第一个版本,带有数据绑定,仍然在Windows 7 PC上运行propper。但是在更新到.NET Framework 4.7后,它也有同样的问题,在安装新版本的应用程序后,它就消失了 这是一个已知的问题吗?如果是,是否有解释和解决方法?有没有更好的解决方案可以让我继续使用绑定 提前感谢,
Elec我们曾试图复制此错误,但没有成功。您能分享一个示例应用程序来演示这个问题吗? 以下是我们所做的: 1.创建winforms应用程序 2.向表单中添加标签和带有2个选项卡的tabcontrol 3.将文本框添加到tabcontrol中的第二个选项卡 4.将表单上的数据绑定标签绑定到表单上的属性,该属性反映第二个选项卡上文本框的内容 与您的代码相比,您是否看到此复制尝试缺少什么?我们还尝试了将数据绑定到SQL数据库。并且在4.0和4.7版本的框架上没有看到任何差异 以下是Form1.cs代码:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form, INotifyPropertyChanged
{
public Form1()
{
InitializeComponent();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = typeof(Form1);
bindingSource.Add(this);
label1.DataBindings.Add("Text", bindingSource, "CustomProperty1", true,
DataSourceUpdateMode.OnPropertyChanged);
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string CustomProperty1 {
get { return textBox1.Text; }
set {
NotifyPropertyChanged();
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "thing1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
NotifyPropertyChanged("CustomProperty1");
}
}
}
谢谢你,Tanya我们曾尝试复制此错误,但没有成功。您能分享一个示例应用程序来演示这个问题吗? 以下是我们所做的: 1.创建winforms应用程序 2.向表单中添加标签和带有2个选项卡的tabcontrol 3.将文本框添加到tabcontrol中的第二个选项卡 4.将表单上的数据绑定标签绑定到表单上的属性,该属性反映第二个选项卡上文本框的内容 与您的代码相比,您是否看到此复制尝试缺少什么?我们还尝试了将数据绑定到SQL数据库。并且在4.0和4.7版本的框架上没有看到任何差异 以下是Form1.cs代码:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form, INotifyPropertyChanged
{
public Form1()
{
InitializeComponent();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = typeof(Form1);
bindingSource.Add(this);
label1.DataBindings.Add("Text", bindingSource, "CustomProperty1", true,
DataSourceUpdateMode.OnPropertyChanged);
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public string CustomProperty1 {
get { return textBox1.Text; }
set {
NotifyPropertyChanged();
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "thing1";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
NotifyPropertyChanged("CustomProperty1");
}
}
}
谢谢你,Tanya