【问题标题】:(Tricky) Call method from app.xaml.cs(棘手)从 app.xaml.cs 调用方法
【发布时间】:2012-09-02 19:55:48
【问题描述】:

我有一个 Pivot-Page 应用程序,每个不同的页面都有不同的应用程序栏。 app-bar 中的方法都在 app.xaml.cs 中声明,但所有输入元素都在 MainPage.xaml.cs 中。

我的一个应用栏上有一个登录按钮,但在 app.xaml.cs 中我无法访问文本框等。

创建 MainPage 的新实例没有帮助,因为所有文本框的值都是空的。 是的,我在 MainPage 中的方法是公开的,但是 MainPage.method(); 不起作用。

如何从 app.xaml.cs 调用 MainPage.xaml.cs 中的方法,该方法使用 MainPage 的文本框?

【问题讨论】:

  • 我不认为将 GUI 元素放在 app.xaml.cs 中是个好主意
  • 我必须在 app.xaml.cs 中声明我的不同应用栏。因此,这些 app-bar 的每个方法都在 app.xaml.cs 中。
  • maethod() 不起作用是什么意思?您是否尝试在 App 类中调用 this.MainWindow.method() ?是否设置了正确的页面?你是在构造之后调用它吗?

标签: c# windows-phone-7 methods call


【解决方案1】:
((MainPage)App.Current.RootVisual).method()

【讨论】:

    【解决方案2】:

    我是蛮力的:我在我想制作的课程中手工制作了我的应用栏。 而且有效

    I got the tip from MS

    【讨论】:

      【解决方案3】:

      以下是基于我正在编写的应用程序所需的代码的通用方法:

      在 MainPage.xaml 中创建如下方法:

      public static void InMainPage()
      {
          System.Diagnostics.Debug.WriteLine("Hi I am a method in MainPage.xaml.cs");
      }
      

      现在在 App.xaml.cs 中,您现在可以在任何方法中引用它:

      MainPage.InMainPage();
      

      希望这个答案能帮助你或其他处于同样困境的人

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        相关资源
        最近更新 更多