wp8应用程序对象

时间:2014-01-31 19:03:56

标签: c# windows-phone-8

我是Windows手机编程的新手,我正在构建一个WP8应用程序,并希望从另一个模块访问“App”对象 例如:

ModuleA ='公共部分类App:应用程序'对象存在的位置

ModuleB ='DoThis.xaml'页面所在的位置

我在ModuleA中有这个:

public partial class App : Application
{
 // .. most application stuff stripped out for brevity

  private void Application_Launching(object sender, LaunchingEventArgs e)
  {
    // refresh the value of the IsTrial property when the application is launched
    DetermineIsTrial();

    string uriString = "/ModuleB;component/DoThis.xaml";
    NavigationService.Navigate(new Uri(uriString, UriKind.Relative));
  }

#region Trial
public static bool IsTrial
{
  get;
  // setting the IsTrial property from outside is not allowed
  private set;
}

private void DetermineIsTrial()
{
#if TRIAL
  // set true if trial enabled (Debug_Trial configuration is active)
  IsTrial = true;
#else
  var license = new Microsoft.Phone.Marketplace.LicenseInformation();
  IsTrial = license.IsTrial();
#endif

#if DEBUG
  // set to false if we are debugging....
  //IsTrial = false;
#endif

}

#endregion
}

我不知道如何将ModuleA中的“App”对象传递给ModuleB,以便我可以访问它

我想在ModuleB

中这样做
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
  Debug.WriteLine("DoThis- OnNavigatedTo");

  if( App.IsTrial )  // I would like this to be ModuleA's "App" object
  {
     // disable some functionality because trial mode...
  }

 // the rest cut for brevity 
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您始终可以通过Application.Current访问Application对象。

在模块类中声明一个接口:

public interface IMyApplication
{
    void DoStuffInMainApp();           
}

并在您的应用程序类中实现它:

public partial class App : Application, ModuleB.IMyApplication
{
    ...
}

现在,您可以从模块中调用应用程序类中的方法:

((IMyApplication)Application.Current).DoStuffInMainApp();

答案 1 :(得分:1)

由于模块B无法了解模块A的任何信息,因此您需要创建共享模块C,或者在B中包含所有共享组件。

我喜欢dependency-injection类型的方法,其中给定的类(例如,Page)调用任何外部依赖项(例如,IsTrial),以便类的所有者必须注入所有依赖项。我在我的应用程序中使用了类似的东西:

// settings class that the Pages will get access to
public interface ISettings
{
    public bool IsTrial { get; }
}

// implementation of ISettings -- owned by the App class
public class Settings : ISettings
{
    public bool IsTrial { get; set; }
}

// interface that a Page should inherit if it needs access to IsTrial
public interface IRequiresSettings 
{
    public ISettings { set; }
}

public class SomePage : PhoneApplicationPage, IRequiresSettings 
{
    public ISettings Settings { get; set; }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
      if( Settings != null && Settings.IsTrial )
      {
         // disable some functionality because trial mode...
      }
    }
}

请注意,ISettings封装了IsTrial的只读行为,因此页面将该属性视为只读。

还有一个步骤,那就是实际设置ISettings。 App类应该通过处理RootFrame.Navigated事件来对此负责。它应检查导航到的页面是否继承IRequiresSettings,并相应地设置该属性。

private Settings _settings = new Settings();

private void InitializePhoneApplication()
{
    RootFrame.Navigated += RootFrame_Navigated;
}

void RootFrame_Navigated(object sender, NavigationEventArgs e)
{
    if (e.Content is IRequiresSettings)
        ((IRequiresSettings)e.Content).Settings = _settings;
}

编辑:我删除了“快速而肮脏”的方法,因为@GerritFölster的答案很快而且不脏。