As usual, I'm tryi开发者_开发技巧ng to use a new technology and having problems right off the bat.
I have a Silverlight Business Application + MvvmLight.
In my viewmodel I try to get the logged in users' roles:
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());
}
At runtime, I get this exception:
System.Windows.Markup.XamlParseException occurred
Message=The invocation of the constructor on type 'CompanyHR.ViewModel.ViewModelLocator' that matches the specified binding constraints threw an exception. [Line: 18 Position: 57]
LineNumber=18
LinePosition=57
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at CompanyHR.App.InitializeComponent()
at CompanyHR.App..ctor()
InnerException: System.ArgumentNullException
Message=Value cannot be null.
Parameter name: source
StackTrace:
at System.Linq.Enumerable.SelectMany[TSource,TResult](IEnumerable`1 source, Func`2 selector)
at CompanyHR.ViewModel.HomeViewModel.DetermineStartableProcesses()
at CompanyHR.ViewModel.HomeViewModel..ctor()
at CompanyHR.ViewModel.ViewModelLocator..ctor()
InnerException:
Looks like the ViewModelLocator is instantiating the ViewModels at app startup before the webcontext get's created, which means it's a bad idea for me to do a lot of work in my viewmodel constructors.
So, where at in the viewmodel should I be retrieving data that will get databound?
Instantiate your WebContext in the App constructor. then add it to your resources in the App_startup before you call
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();
}
I find its easier to do this part in the code behind because of my custom AuthenticationDomainContext and Membershipprovider...But Dereks works well too, i just was using the code behind while i was getting everything working.
This is how I avoided that when using mvvm-light. So, WebContext gets created first.
In 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>
精彩评论