C# 使用类在MVVM灯光中引用视图模型定位器
我正在尝试下面的代码来查找视图模型定位器的引用,但我得到一个错误,即对象引用不能设置为对象的实例:-C# 使用类在MVVM灯光中引用视图模型定位器,c#,xamarin.forms,mvvm-light,C#,Xamarin.forms,Mvvm Light,我正在尝试下面的代码来查找视图模型定位器的引用,但我得到一个错误,即对象引用不能设置为对象的实例:- internal class Locator : ViewModelLocator { private static readonly Lazy<Locator> _locator = new Lazy<Locator>(() => new Locator(), LazyThreadSafetyMode.PublicationOnly); publi
internal class Locator : ViewModelLocator
{
private static readonly Lazy<Locator> _locator = new Lazy<Locator>(() => new Locator(), LazyThreadSafetyMode.PublicationOnly);
public static Locator Instance => _locator.Value;
private Locator()
{
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<AddStudentViewModel>();
}
}
内部类定位器:ViewModelLocator
{
private static readonly Lazy _locator=new Lazy(()=>new locator(),LazyThreadSafetyMode.PublicationOnly);
公共静态定位器实例=>\u Locator.Value;
专用定位器()
{
SimpleIoc.Default.Register();
SimpleIoc.Default.Register();
}
}
有谁能帮我解决这个问题吗?注册viewmodel后,您需要创建这样的方法
public CreateAssetViewModel CreateAssetVM
{
get
{
if (!SimpleIoc.Default.IsRegistered<CreateAssetViewModel>())
{
SimpleIoc.Default.Register<CreateAssetViewModel>();
}
return ServiceLocator.Current.GetInstance<CreateAssetViewModel>();
}
}
public CreateAssetViewModel CreateAssetVM
{
得到
{
如果(!SimpleIoc.Default.IsRegistered())
{
SimpleIoc.Default.Register();
}
返回ServiceLocator.Current.GetInstance();
}
}
注册viewmodel后,您需要创建这样的方法
public CreateAssetViewModel CreateAssetVM
{
get
{
if (!SimpleIoc.Default.IsRegistered<CreateAssetViewModel>())
{
SimpleIoc.Default.Register<CreateAssetViewModel>();
}
return ServiceLocator.Current.GetInstance<CreateAssetViewModel>();
}
}
public CreateAssetViewModel CreateAssetVM
{
得到
{
如果(!SimpleIoc.Default.IsRegistered())
{
SimpleIoc.Default.Register();
}
返回ServiceLocator.Current.GetInstance();
}
}
我在我的项目中使用以下代码,您需要添加public get set属性,以便在locator类中定位视图模型:-
internal class Locator : ViewModelLocator
{
private static readonly Lazy<Locator> _locator = new Lazy<Locator>(() => new Locator(), LazyThreadSafetyMode.PublicationOnly);
public static Locator Instance => _locator.Value;
private Locator()
{
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<AddStudentViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public AddStudentViewModel AddStudentViewModel
{
get
{
return ServiceLocator.Current.GetInstance<QuestionsViewModel>();
}
}
}
我在我的项目中使用以下代码,您需要添加public get set属性,以便在locator类中定位视图模型:-
internal class Locator : ViewModelLocator
{
private static readonly Lazy<Locator> _locator = new Lazy<Locator>(() => new Locator(), LazyThreadSafetyMode.PublicationOnly);
public static Locator Instance => _locator.Value;
private Locator()
{
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<AddStudentViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public AddStudentViewModel AddStudentViewModel
{
get
{
return ServiceLocator.Current.GetInstance<QuestionsViewModel>();
}
}
}
我希望您使用第一种方法,通过延迟加载,您可以找到定位器的引用:)
private static readonly Lazy _locator=new Lazy(()=>new locator(),LazyThreadSafetyMode.PublicationOnly);
公共静态定位器实例=>\u Locator.Value;
我希望您使用第一种方法,通过延迟加载,您可以找到定位器的参考:)
private static readonly Lazy _locator=new Lazy(()=>new locator(),LazyThreadSafetyMode.PublicationOnly);
公共静态定位器实例=>\u Locator.Value;
谢谢,我也找到了。谢谢,我也找到了。