像往常一样,我正在尝试使用新技术并立即解决问题。
我有一个Silverlight业务应用程序+ MvvmLight。
在我的viewmodel中,我尝试获取登录用户的角色:
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());
}
在运行时,我遇到了这个例外:
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:
看起来ViewModelLocator在创建webcontext get之前在应用启动时实例化ViewModels,这意味着我在我的viewmodel构造函数中做了很多工作是个坏主意。
那么,我应该在viewmodel中检索将获得数据绑定的数据吗?
答案 0 :(得分:0)
这是我在使用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>
答案 1 :(得分:0)
在App构造函数中实例化您的WebContext。然后在调用
之前将其添加到App_startup中的资源中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也运行良好,我只是使用了代码,而我正在使一切正常工作。