在自定义控件上使用命令
我正在尝试使用搜索文本框(我按照本教程制作的: http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/)。
我使用 MVVM 和WPF。当您在视图的代码隐藏文件中编写“搜索”事件时,上述用户控件可以工作,但我无法让它与命令一起使用(使用 ViewModel)。
(当您大约 2 秒没有输入任何内容时,搜索事件就会触发。)
我尝试使用 Caliburn,因此它可以将视图事件“映射”到 viewmodel 方法。但是,当事件触发时,应用程序崩溃:“找不到方法 SearchText() 的目标。”在自定义用户控件的 RaiseSearchEvent 方法上。
请参阅以下测试应用程序:测试应用程序
有人可以告诉我我做错了什么吗?我告诉 CaliBurn 执行以下操作:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Search">
<cal:ActionMessage MethodName="SearchText()" />
</i:EventTrigger>
</i:Interaction.Triggers>
所以我认为这是正确的。这意味着当“Search”事件触发时,Caliburn 将在 ViewModel 中查找 SearchText 方法。但这并没有发生,它导致我的应用程序崩溃并烧毁。
你知道为什么吗?或者我如何解决这个问题(不一定要使用 Caliburn)。 我已经尝试添加“扩展命令支持”(http://msdn.microsoft.com /en-us/library/dd458928.aspx),但这对我来说有点太复杂了:/
感谢您的帮助!
I'm trying to use the search text box (which I made by following this tutorial: http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/).
I use MVVM & WPF. The above user control works when you write the "Search"-event in the code-behind file of the View, but I can't get it to work with a command (using the ViewModel).
(The search-event fires when you haven't typed something for about 2 seconds.)
I've tried using Caliburn, so it can "map" the view event to the viewmodel method. However when the event fires, the application crashes: "No target found for method SearchText()." on the RaiseSearchEvent method from the custom user control.
See the following test application: Test application
Could somebody tell me what I'm doing wrong? I told CaliBurn to do the following:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Search">
<cal:ActionMessage MethodName="SearchText()" />
</i:EventTrigger>
</i:Interaction.Triggers>
So I figure this is correct. It means that when the "Search" event fires, caliburn will look for the method SearchText in the ViewModel. This doesn't happen though, and it causes my app to crash and burn.
Do you know why? Or how I could solve this problem (doesn't have to be with Caliburn).
I already tried adding "Extending Command Support" (http://msdn.microsoft.com/en-us/library/dd458928.aspx), but this is a little too complex for me :/
Thanks for any help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 Caliburn 的 ActionMessage,但由于您不使用其 Bootstrapper 类来启动应用程序,因此 MainView 的 DataContext 未设置为 MainViewModel 的实例。如果您在运行时检查 SearchTextBox 的 DataContext,您将看到它为空。
以下一系列步骤可能会解决您的问题(使用链接的示例项目)
创建一个名为 MyBootstrapper 的类。它应该看起来像这样
将新的引导程序添加到应用程序的资源集合中,如下所示(App.xaml)
不知道为什么,但如果引导程序没有嵌套在我的构建中,则当 App. InitializeComponent() 已运行...
更改 App.xaml.cs 以仅运行 InitializeComponent。请注意,我必须稍微调整您的构建才能使其正常工作...仅当您拥有步骤 2 中的嵌套资源字典,或者您有 x 时,InitializeComponent() 才会在 App.g.cs 文件中定义: App.xaml 上的名称属性,或者其他东西...
最后,您需要按照 Wallstreet Programmer 的建议删除括号。
这些步骤应该使您的应用程序实例化引导程序,引导程序又将 MainViewModel 实例化为应用程序的根视图模型,然后创建 MainView 并将其 DataContext 连接到 MainViewModel。那时,您的应用程序应该按预期工作。
You are using the Caliburn's ActionMessage but because you do not use its Bootstrapper class to start up your application, the MainView's DataContext is not set to an instance of the MainViewModel. If you check the SearchTextBox's DataContext at runtime, you'll see it's null.
Here's a series of steps that may solve your problem (using your linked example project)
Create a class called MyBootstrapper. It should look like this
Add your new bootstrapper to the Application's Resources collection, like I show below (App.xaml)
Not sure why, but if the bootstrapper isn't nested in my build, it never is instantiated when App.InitializeComponent() is run...
Change App.xaml.cs to simply run InitializeComponent. Note that I had to tweak your build a bit to get this to work... InitializeComponent() is only defined in the App.g.cs file if you have the nested resource dictionary from step 2, or if you have an x:Name attribute on App.xaml, or perhaps other things...
Finally, you need to remove the parens as Wallstreet Programmer suggested.
Those steps should cause your App to instantiate your bootstrapper, which in turn instantates the MainViewModel as the root viewmodel of your application, and then create a MainView and hook up its DataContext to the MainViewModel. At that point, your application should work as expected.
消除 ()
Remove ()
运行您的应用程序后,我发现您需要初始化 MainViewModel 并将 SearchTextBox 的文本与 TekstBoxTekst 绑定。
代码隐藏
XAML
After I run your application, I see that you need to initialize the MainViewModel and also to bind Text of SearchTextBox with TekstBoxTekst.
Codebehind
XAML