gpt4 book ai didi

c# - wp8 应用程序对象

转载 作者:行者123 更新时间:2023-11-30 22:09:33 25 4
gpt4 key购买 nike

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

ModuleA = 'public partial class App : Application' 对象所在的位置

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
}

感谢您的帮助!

最佳答案

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

在你的模块类中声明一个接口(interface):

public interface IMyApplication
{
void DoStuffInMainApp();
}

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

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

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

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

关于c# - wp8 应用程序对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21488051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com