我在公共的App.xaml.cs文件中有Window對象。現在,我想在類文件中訪問這個窗口對象。可能嗎?如何訪問窗口WPF中的類文件中的App.xaml中聲明的對象?
在App.xaml.cs文件:
public partial class App : Application
{
public MyWindow mainWindowObj;
void App_Startup(object sender, StartupEventArgs e)
{
mainWindowObj = new MyWindow();
mainWindowObj.Show();
}
}
在一些Classfile.cs我想訪問 'mainWindowobj'。我曾嘗試按照Classfile.cs但錯誤:
((App)Application.Current).mainWindowObj.Title="Hello";
這上面的線是樣品,我想訪問此表格中的變量,對象。
幫助讚賞!
什麼錯誤?這個應用程序真的是WPF還是它是一個雙贏的應用程序? – Viv
hello @Viv錯誤是在行。當前它說:System.Windows.Forms.Application doesnt包含'當前'的定義。任何解決方案 –
well'System.Windows.Forms.Application'不是你想要的,切換'((App)Application.Current).mainWindowObj.Title =「Hello」;'to'(System.Windows.Application.Current as App) .mainWindowObj.Title =「你好」;' – Viv