我在 WPF 应用程序中使用 PRISM 5。我的应用程序中的 Shell View 有两个区域,将其视为 A 和 B。区域 A 包含一个 POPUP(PRISM 5 交互功能用于显示弹出窗口)。
当我在 View 的构造函数中创建弹出 View 模型的实例时,应用程序正在运行。
工作代码
public PopupView()
{
InitializeComponent();
this.DataContext = new PopupViewModel(); // Working code
}
但是当我尝试使用依赖注入(inject)创建 View 模型实例时。应用程序在父 View ( View A)的 InitializeComponent(); 上失败。
DI 无效代码
public PopupView(PopupViewModel viewModel)
{
InitializeComponent(); // Failing in AView initialze
// before reaching here
this.DataContext = viewModel;
}
在module/bootstrapper中查看模型注册
container.RegisterType<AViewModel>();
发生错误
NULLReference异常发生
Stacktrace(针对问题进行了编辑)
at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state)
at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(Type type, Object[] args)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CreateInstance(XamlType xamlType, Object[] args)
at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndObject()
at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at MyNamespace.AView.InitializeComponent() in e:\xxx\xxxxx\xxx\AView.xaml:line 1
at MyNamespace.AView..ctor(AViewModel viewModel) in e:\xxx\xxxxx\xxx\AView.xaml.cs:line 18
AViewModel(编辑了一个以避免项目特定信息)
public class ItemSelectionNotification : Confirmation
{
//This class includes properties related to my project
}
public class AViewModel
{
public InteractionRequest<ItemSelectionNotification> ItemSelectionRequest { get; private set; }
public AViewModel(EventAggregator eventAggregator,IUnityContainer container)
{
this.eventAggregator = eventAggregator;
this.container = container;
ItemSelectionRequest = new InteractionRequest<ItemSelectionNotification>();
SettingsCommand = new DelegateCommand(OnClickSetting); //Command for settings button click
}
//Button click handling
public void OnClickSetting()
{
var notification = new ItemSelectionNotification()
{
Title = "Items"
};
this.ItemSelectionRequest.Raise(notification,OnSaveCallback);
}
private void OnSaveCallback(PropertySelectionNotification returned)
{
}
}
最佳答案
我假设您在 XAML 中使用 InteractionRequestTrigger 和 PopupWindowAction 将 PopupView 绑定(bind)到相应的 InteractionRequest。
您不能将 PopupViewModel 传递给 PopupView 的构造函数,因为该 View 是由 PopupWindowAction 直接创建的,而不是由 DI-容器。当 PopupWindowAction 创建 PopupView 时,它会将 View 的 DataContext 设置为您传递给 的 。 INotification 对象InteractionRequest.Raise(…)INotification 有一个 Content 属性,可用于将您想要的任何数据传递给 PopupView。例如,您可以在此处传递 PopupViewModel。
编辑:我查找了PopupWindowAction sources看来我错了。他们在尝试实例化 PopupWindowAction.WindowContentType 时使用 ServiceLocator,因此从技术上讲,将 PopupViewModel 传递给 PopupView 的构造函数应该不会导致异常,但它仍然无用,因为 View 的 DataContext 被传递给 InteractionRequest 的 INotification 对象进一步替换。
示例:
// PopupViewModel.cs
internal sealed class PopupViewModel
{
public PopupViewModel(string message)
{
Message = message;
}
public string Message { get; }
}
// PopupView.xaml
<UserControl …>
<Grid DataContext="{Binding Content, Mode=OneTime}">
<Label Text="{Binding Message, Mode=OneTime}" />
</Grid>
</UserControl>
// SomeViewModel.cs
internal sealed class SomeViewModel
{
// Don't use DI-container references to construct objects, inject factories instead.
// Also to keep things simple you can just create your PopupViewModel directly if it has no external dependencies.
private readonly Func<string, PopupViewModel> _popupViewModelFactory;
public SomeViewModel(Func<string, PopupViewModel> popupViewModelFactory)
{
_popupViewModelFactory = popupViewModelFactory;
}
public ICommand ShowPopupCommand { get; } = new DelegateCommand(DoShowPopup);
public InteractionRequest<INotification> PopupRequest { get; } = new InteractionRequest<INotification>();
private void DoShowPopup()
{
PopupRequest.Raise(new Notification
{
Content = _popupViewModelFactory("This is a Popup Message!")
}, _ =>
{
// Callback code.
});
}
}
// SomeView.xaml
<UserControl …>
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding PopupRequest, Mode=OneTime}">
<prism:PopupWindowAction WindowContentType="views:PopupView" />
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
<Button Command="{Binding ShowPopupCommand, Mode=OneTime}" />
<UserControl>
// SomeModule.cs (or maybe Bootstrapper.cs if you setup your container in Bootstrapper)
public sealed class SomeModule : IModule
{
private readonly IUnityContainer _container;
public SomeModule(IUnityContainer container)
{
_container = container;
}
public override void Initialize()
{
_container.RegisterType<Func<string, PopupViewModel>>(
new InjectionFactory(c =>
new Func<string, PopupViewModel>(message =>
c.Resolve<PopupViewModel>(
new ParameterOverride("message", message))));
}
}
关于c# - 在弹出窗口中获取 IOC 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27485492/
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象