简体   繁体   中英

wp8 Application object

I am new to Windows phone programming and I am building a WP8 application and would like to access the "App" object from another module eg:

ModuleA = where the 'public partial class App : Application' object lives

ModuleB = where 'DoThis.xaml' page lives

I have this in 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
}

I don't know how to get the "App" object from ModuleA over to ModuleB so I can access it

I would like to do this in 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 
}

Thanks for your help !

You can always access the Application object via Application.Current .

Declare an interface in your module class:

public interface IMyApplication
{
    void DoStuffInMainApp();           
}

and implement it in your application class:

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

Now you can call a method in your application class from your module:

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

Since Module B can't know anything about Module A, you'll need to either create a shared Module C, or include all shared components in B.

I like a dependency-injection type of approach, where a given class (eg, the Page) calls for any external dependencies (eg, IsTrial), so that the owner of the class must inject all dependencies. I use something like this in my apps:

// 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...
      }
    }
}

Notice that ISettings encapsulates the read-only behavior of IsTrial , so that the page sees the property as read-only.

There is one more step, and that is to actually set ISettings . The App class should be responsible for this, by handling the RootFrame.Navigated event. It should check if the navigated-to page inherits IRequiresSettings , and set that property accordingly.

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;
}

Edited: I deleted my "quick and dirty" approach, because @GerritFölster's answer is as quick and not dirty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM