Windows phone 7 this.DataContext是否为空?用于填充-切换到Autofac
我正在使用MVVM灯、适用于windows phone 7的Autofac和Autofac定位器 使用autofacWindows phone 7 this.DataContext是否为空?用于填充-切换到Autofac,windows-phone-7,mvvm-light,autofac,datacontext,Windows Phone 7,Mvvm Light,Autofac,Datacontext,我正在使用MVVM灯、适用于windows phone 7的Autofac和Autofac定位器 使用autofac 使用GalaSoft.MvvmLight; 使用GalaSoft.MvvmLight.Ioc; 使用Microsoft.Practices.ServiceLocation; 使用MvvmLight1.Model; 使用Autofac; 名称空间MvvmLight1.ViewModel { 公共类ViewModelLocator { 静态ViewModelLocator() {
使用GalaSoft.MvvmLight;
使用GalaSoft.MvvmLight.Ioc;
使用Microsoft.Practices.ServiceLocation;
使用MvvmLight1.Model;
使用Autofac;
名称空间MvvmLight1.ViewModel
{
公共类ViewModelLocator
{
静态ViewModelLocator()
{
var builder=new ContainerBuilder();
RegisterType();
var container=builder.Build();
SetLocatorProvider(()=>新的AutofacServiceLocator(容器));
}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Performance”,
“CA1822:MarkMembersAstatic”,
justion=“此非静态成员用于数据绑定目的。”)]
公共主视图模型主视图
{
得到
{
返回ServiceLocator.Current.GetInstance();
}
}
}
}
使用Microsoft.Phone.Controls;
命名空间MvvmLight1
{
公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
var main=this.DataContext as MainPage;//将为空
}
}
}
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用Autofac;
使用Microsoft.Practices.ServiceLocation;
命名空间MvvmLight1
{
公共密封类AutofacServiceLocator:ServiceLocatorImplBase
{
只读IComponentContext容器;
公共AutofacServiceLocator(IComponentContext容器)
{
if(容器==null)
抛出新的ArgumentNullException(“容器”);
_容器=容器;
}
受保护的重写对象DoGetInstance(类型serviceType,字符串键)
{
返回键!=null?\u container.resolveName(键,服务类型):\u container.Resolve(服务类型);
}
受保护的重写IEnumerable DoGetAlliances(类型serviceType)
{
var enumerableType=typeof(IEnumerable);
对象实例=_container.Resolve(enumerableType);
return((IEnumerable)instance.Cast();
}
}
}
使用GalaSoft.MvvmLight;
使用MvvmLight1.Model;
名称空间MvvmLight1.ViewModel
{
公共类MainViewModel:ViewModelBase
{
公共主视图模型()
{
}
}
}
现在,MainPage.cs的构造函数中的viewModel将始终为null
当我刚刚使用SimpleIoc时,它被主对象填充。这行代码
ServiceLocator.SetLocatorProvider的确切位置(在哪个类的哪个方法中)(…
在您的解决方案中?它在我的Locator类中,我更新了我的代码。只有ServiceLocator.SetLocatorProvider
您更改了什么?因此loginVm=this.DataContext作为loginVm;
始终位于LoginView
构造函数中,DataContext
始终设置为DataContext=”{绑定登录,源={StaticResource Locator}}”
?如何以及在何处包装您的LoginView
?顺便问一下,为什么要在视图的构造函数中保留对LoginVm
的引用?我猜您的帖子中仍然有一个打字错误this.DataContext作为主页;
应该是this.DataContext作为MainViewModel;
。您可以尝试移动从XAML到代码隐藏的DataContext
设置可以更容易地跟踪问题。因此,从XAML中删除DataContext=“{Binding Main,
”,并在MainPage
构造函数中添加这一行:InitializeComponent();DataContext=((ViewModelLocator)应用程序。Current.Resources[”定位器“]).Main;vm=DataContext作为MainViewModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using MvvmLight1.Model;
using Autofac;
namespace MvvmLight1.ViewModel
{
public class ViewModelLocator
{
static ViewModelLocator()
{
var builder = new ContainerBuilder();
builder.RegisterType<MainViewModel>();
var container = builder.Build();
ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(container));
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
}
}
using Microsoft.Phone.Controls;
namespace MvvmLight1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
var main = this.DataContext as MainPage; // will be null
}
}
}
<phone:PhoneApplicationPage x:Class="MvvmLight1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"
Orientation="Portrait"
d:DesignWidth="480"
d:DesignHeight="768"
shell:SystemTray.IsVisible="True"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle"
Text="{Binding ApplicationTitle}"
Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock x:Name="PageTitle"
Text="{Binding PageName}"
Margin="-3,-8,0,0"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentGrid"
Grid.Row="1">
<TextBlock Text="{Binding WelcomeTitle}"
Style="{StaticResource PhoneTextNormalStyle}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="40"
TextWrapping="Wrap"
TextAlignment="Center" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using Microsoft.Practices.ServiceLocation;
namespace MvvmLight1
{
public sealed class AutofacServiceLocator : ServiceLocatorImplBase
{
readonly IComponentContext _container;
public AutofacServiceLocator(IComponentContext container)
{
if (container == null)
throw new ArgumentNullException("container");
_container = container;
}
protected override object DoGetInstance(Type serviceType, string key)
{
return key != null ? _container.ResolveNamed(key, serviceType) : _container.Resolve(serviceType);
}
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
var enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);
object instance = _container.Resolve(enumerableType);
return ((IEnumerable)instance).Cast<object>();
}
}
}
using GalaSoft.MvvmLight;
using MvvmLight1.Model;
namespace MvvmLight1.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
}
}
}