我是Windows编程新手,我正在构建一个WP8应用程序,我想从另一个模块访问"App“对象,例如:
ModuleA =“公共部分类应用程序:应用程序”对象所在的位置
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
}我不知道如何将"App“对象从ModuleA转到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
}谢谢你的帮助!
发布于 2014-01-31 19:27:58
您可以始终通过Application.Current访问应用程序对象。
在模块类中声明一个接口:
public interface IMyApplication
{
void DoStuffInMainApp();
}并在应用程序类中实现它:
public partial class App : Application, ModuleB.IMyApplication
{
...
}现在,您可以从模块中调用应用程序类中的方法:
((IMyApplication)Application.Current).DoStuffInMainApp();https://stackoverflow.com/questions/21488051
复制相似问题