gpt4 book ai didi

c# - 全局访问单例的Silverlight应用资源

转载 作者:太空狗 更新时间:2023-10-29 23:35:27 25 4
gpt4 key购买 nike

我有一个单例,一旦点击就会加载用户配置文件信息,我想让它成为我的 SL3 应用程序中的应用程序级资源,以便应用程序中的元素可以绑定(bind)到它。

我的实例化代码版本很简单

UserProfile x = UserProfile.GetInstance();

我希望能够在 app.xaml 文件的 xaml 中执行此操作,而在 WPF 中我们有 ObjectDataProvider,因此我可以表达类似

<ObjectDataProvider MethodName="GetInstance" 
ObjectType="{x:Type local:UserProfile}" x:Key="CurrentUserProfile"/>

我正在努力在 SL3 中为此找到正确的实现。

最佳答案

正如您所指出的,Silverlight 没有ObjectDataProvider。如果您需要它提供的特性,例如惰性实例化,您需要构建一个您自己的类来处理它。如果您实际上不需要这些功能,则只需在启动时将 UserProfile 的实例添加到 App.Resources 中:-

 private void Application_Startup(object sender, StartupEventArgs e)
{
Resources.Add("CurrentUserProfile", UserProfile.GetInstance());
RootVisual = new MainPage();
}

关于c# - 全局访问单例的Silverlight应用资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166852/

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