[英]Calling Window_Loaded event from an another class
My MainWindow.xaml.cs class includes following Window_Loaded
Event: 我的MainWindow.xaml.cs类包含以下Window_Loaded
事件:
public void Window_Loaded(object sender, RoutedEventArgs e)
{
SQLClass sqlclass = new SQLClass();
sqlclass.initSQL(dataGrid1);
}
So I'm calling this initSql(Datagrid dataGrid)
method from the SQLClass
, which actually binds my dataGrid to the Sql table. 所以我从SQLClass
调用此initSql(Datagrid dataGrid)
方法,该方法实际上将我的dataGrid绑定到Sql表。
public void initSQL(DataGrid dataGrid)
{
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\\Users\\Osman\\");
try
{
using (SqlConnection cn = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=FinalApplication1;Integrated Security=True;"))
{
cn.Open();
SqlDataAdapter adap = new SqlDataAdapter("SELECT Time, Condition FROM Status", cn);
System.Data.DataSet ds = new System.Data.DataSet();
adap.Fill(ds, "Status");
dataGrid.DataContext = ds.Tables["Status"].DefaultView;
}
}
catch (Exception ex)
{
MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
This works perfectly on a startup, but when I want to update the dataGrid after inserting a value to the Sql table, I'm then calling the Window_Loaded
method from from my BluetoothLE
class: 这在启动时效果很好,但是当我想在向Sql表中插入一个值后更新dataGrid时,然后从我的BluetoothLE
类中调用Window_Loaded
方法:
public class BluetoothLE
{
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
dispatcherTimer.Stop();
MainWindow mainwin = new MainWindow();
mainwin.Window_Loaded(this, null);
}
}
The issue is that the dataGrid does not update in this case. 问题是在这种情况下dataGrid不会更新。 The Window_Loaded
method is triggered, but it does not update the dataGrid. 已触发Window_Loaded
方法,但不会更新dataGrid。 What's actually wrong here ?. 这到底是怎么了?
your are creating a new instance of your main window where you don't show rather than updating datagrid
of your current MainWindow
. 您正在创建不显示而不是更新当前MainWindow
datagrid
的主窗口的新实例。 Try: 尝试:
private void Update()
{
//enter datagrid functionality you have in window load event
}
public class BluetoothLE
{
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
dispatcherTimer.Stop();
Update();
}
}
The code: 编码:
MainWindow mainwin = new MainWindow();
mainwin.Window_Loaded(this, null);
will not work, you need to call Window_Loaded
on the current instance of MainWindow
, creating a new instance (with the new MainWindow()
line) won't affect the window that's displayed on the screen. 将无法正常工作,您需要在MainWindow
的当前实例上调用Window_Loaded
,创建一个新实例(使用new MainWindow()
行)不会影响屏幕上显示的窗口。
I don't know where you create your BluetoothLE
class but it needs to have a reference to the MainWindow
instance so it can call methods on it. 我不知道您在哪里创建BluetoothLE
类,但是它需要对MainWindow
实例进行引用,以便可以在其上调用方法。 Something like the following: 类似于以下内容:
public class BluetoothLE
{
private MainWindow windowInstance;
public BluetoothLE(MainWindow windowInstance) {
this.windowInstance = windowInstance;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
dispatcherTimer.Stop();
windowInstance.Window_Loaded(this, null);
}
}
And if you create BluetoothLE
from inside MainWindow
: 而且,如果您从MainWindow
内部创建BluetoothLE
:
var bluetooth = new BluetoothLE(this);
If you wanted to get rid of that dependence (you probably should in the long run) you might want to use C# events, but that's another story. 如果您想摆脱这种依赖性(从长远来看,您可能应该这样做),则可能要使用C#事件,但这是另一回事了。
Also pull out your update code into a separate method called UpdateGrid()
or something and call it from the Window_Load
event and other places you need to update, Window_Load
shouldn't be used to update the UI from elsewhere. 还要将您的更新代码提取到一个名为UpdateGrid()
的单独方法中,或从Window_Load
事件和您需要更新的其他地方调用它,不应使用Window_Load
从其他地方更新UI。
((MainWindow)Application.Current.MainWindow).Window_Loaded(this, null);
这应该做您想要做的事情,尽管这是一种非常丑陋的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.