.net 在mvvmLight viewmodel中使用WebContext时出现问题
和往常一样,我尝试使用一种新技术,但一开始就遇到了问题 我有一个Silverlight业务应用程序+MvvmLight 在我的viewmodel中,我尝试获取登录用户的角色:.net 在mvvmLight viewmodel中使用WebContext时出现问题,.net,silverlight,mvvm,mvvm-light,.net,Silverlight,Mvvm,Mvvm Light,和往常一样,我尝试使用一种新技术,但一开始就遇到了问题 我有一个Silverlight业务应用程序+MvvmLight 在我的viewmodel中,我尝试获取登录用户的角色: public HomeViewModel() { if (IsInDesignMode) { // Code runs in Blend --> create design time data. } else
public HomeViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
DetermineStartableProcesses();
}
}
private void DetermineStartableProcesses()
{
_startableProcesses = new ObservableCollection<WorkflowProcess>(
WebContext.Current.User.Roles.SelectMany(r =>
WorkflowProcess.GetStartableByRole(r))
.Distinct());
}
看起来ViewModelLocator在创建webcontext get之前正在应用程序启动时实例化ViewModels,这意味着在viewmodel构造函数中执行大量工作对我来说是个坏主意
那么,我应该在viewmodel中的什么位置检索将被数据绑定的数据呢?这就是我在使用mvvm light时避免这种情况的方法。因此,WebContext首先被创建 在App.xaml中:
<Application.ApplicationLifetimeObjects>
<ct:WebContext>
<ct:WebContext.Authentication>
<as:FormsAuthentication DomainContextType="MyProj.Data.AuthenticationContext, MyProj.Client.Common, Version=1.0.0." />
</ct:WebContext.Authentication>
</ct:WebContext>
</Application.ApplicationLifetimeObjects>
<Application.Resources>
<ResourceDictionary>
<ct:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在应用程序构造函数中实例化WebContext。然后在调用之前,将其添加到App_启动中的资源中
public App()
{
Startup += Application_Startup;
Exit += Application_Exit;
UnhandledException += Application_UnhandledException;
if (IsInDesignModeStatic)
{
Services.ServiceLoader.LoadDesignTimeServices();
DispatcherHelper.Initialize();
}
else
{
try
{
ServiceLoader.LoadRunTimeServices();
DispatcherHelper.Initialize();
WebContext webContext = new WebContext();
ApplicationLifetimeObjects.Add(WebContext.Current);
FormsAuthentication fa = new FormsAuthentication();
fa.DomainContext = new Web.Services.AuthenticationDomainContext();
WebContext.Current.Authentication = fa;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.Resources.Add("WebContext", WebContext.Current);
RootVisual = new MainPage();
}
由于我的自定义AuthenticationDomainContext和Membershipprovider,我发现在代码隐藏中执行这一部分更容易…但是Dereks也很好,我只是在使用代码隐藏,而我正在让一切正常工作
public App()
{
Startup += Application_Startup;
Exit += Application_Exit;
UnhandledException += Application_UnhandledException;
if (IsInDesignModeStatic)
{
Services.ServiceLoader.LoadDesignTimeServices();
DispatcherHelper.Initialize();
}
else
{
try
{
ServiceLoader.LoadRunTimeServices();
DispatcherHelper.Initialize();
WebContext webContext = new WebContext();
ApplicationLifetimeObjects.Add(WebContext.Current);
FormsAuthentication fa = new FormsAuthentication();
fa.DomainContext = new Web.Services.AuthenticationDomainContext();
WebContext.Current.Authentication = fa;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.Resources.Add("WebContext", WebContext.Current);
RootVisual = new MainPage();
}