Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 主窗口获取访问方法的权限_C#_Wpf - Fatal编程技术网

C# 主窗口获取访问方法的权限

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()

我有一个没有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()
    {
        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更改为代码隐藏函数时,我不需要一个空窗口。