C# 主窗口获取访问方法的权限
我有一个没有StartupUri的wpf项目。我在app.cs中将主窗口设置为TestWindow。但是我不能用什么都做的方法。如何访问TestWindow的方法C# 主窗口获取访问方法的权限,c#,wpf,C#,Wpf,我有一个没有StartupUri的wpf项目。我在app.cs中将主窗口设置为TestWindow。但是我不能用什么都做的方法。如何访问TestWindow的方法 MainWindow.DoNothing(); //No such method. Get error when try to build 项目: TestWindow.cs public partial class TestWindow : BaseWindow { ... public void DoNothing()
MainWindow.DoNothing(); //No such method. Get error when try to build
项目:
TestWindow.cs
public partial class TestWindow : BaseWindow
{
...
public void DoNothing()
{
return;
}
...
}
...
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = new TestWindow();
}
...
app.xaml
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"/>
将
main窗口
转换为您的类型:
(MainWindow as TestWindow)?.DoNothing();
或者将对TestWindow
的引用存储在您自己的变量中,并使用此变量直接访问该窗口:
public partial class App : Application
{
private readonly TestWindow testWindow = new TestWindow();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow = testWindow;
...
testWindow.BeginInit();
}
}
将其强制转换为类
((类)proporvarwhichtypes是类的BaseClasses)。ClassMethod()
但如果需要此功能。。。那么你的架构有问题了我有一个托盘应用程序。它一直不需要窗户。我设置主窗口并将其隐藏。当新数据到达时,我只显示主窗口并更新信息。当我的try应用程序加载如何将StartupUri更改为代码隐藏函数时,我不需要一个空窗口。