首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在mvvmLight视图模型中使用WebContext时出现问题

在mvvmLight视图模型中使用WebContext时出现问题
EN

Stack Overflow用户
提问于 2011-05-25 05:02:46
回答 2查看 940关注 0票数 1

像往常一样,我正在尝试使用一种新技术,但马上就遇到了问题。

我有一个Silverlight Business Application + MvvmLight。

在我的视图模型中,我尝试获取登录用户的角色:

代码语言:javascript
复制
    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());
    }

在运行时,我得到了这个异常:

代码语言:javascript
复制
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,这意味着在视图模型构造函数中做大量工作对我来说不是一个好主意。

那么,我应该在视图模型的什么地方检索将获得数据绑定的数据呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-27 08:37:13

在应用程序构造函数中实例化您的WebContext。然后在调用之前将其添加到App_startup中的资源中

代码语言:javascript
复制
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...But Dereks也工作得很好,我只是在使用代码幕后,而我正在让一切正常工作。

票数 0
EN

Stack Overflow用户

发布于 2011-05-25 10:48:49

这就是我在使用mvvm-light时避免这种情况的方法。因此,首先创建WebContext。

在App.xaml中:

代码语言:javascript
复制
 <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6116851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档