我的Silverlight项目中有ViewModel
public class MainViewModel : BaseNotify, IMainViewModel
{
[Dependency("AccountID")]
public Guid AccountID { get; set; }
...
public MainViewModel()
{
if (AccountID != Guid.Empty)
{
PreSelectedAccount = new Account() { ID = AccountID };
SingleAccountMode = true;
}
}
....
}
我以这种方式使用Unity:
public static class IoC
{
static IoC()
{
Current = new UnityContainer();
Current.RegisterType<IMainViewModel, MainViewModel>();
}
public static UnityContainer Current { get; set; }
}
public partial class App : Application
{
[Dependency]
public IMainViewModel ViewModel { get; set; }
private void Application_Startup(object sender, StartupEventArgs e)
{
Guid accountId = "1234-5678-1234-5678-1234";
IoC.Current.BuildUp(this);
}
}
调用BuildUp方法后,我在App.ViewModel中有MainViewModel实例,但是如何设置Unity以在BuildUp期间为MainViewModel.AccountId属性值注入(inject)一些值?
最佳答案
您需要使用override进行解析/构建:
IoC.Current.BuildUp(this, new PropertyOverride("AccountID", accountId));
关于c# - 注入(inject)ViewModel及其属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8989316/