我将可观察字典从视图模型绑定到视图.我使用Caliburn Micro Framework.
视图:
<ListBox Name="Friends"
SelectedIndex="{Binding Path=SelectedFriendsIndex,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedFriend, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
Style="{DynamicResource friendsListStyle}"
IsTextSearchEnabled="True" TextSearch.TextPath="Value.Nick"
Grid.Row="2"
Margin="4,4,4,4"
PreviewMouseRightButtonUp="ListBox_PreviewMouseRightButtonUp"
PreviewMouseRightButtonDown="ListBox_PreviewMouseRightButtonDown"
MouseRightButtonDown="ListBox_MouseRightButtonDown"
Micro:Message.Attach="[MouseDoubleClick]=[Action OpenChatScreen()]" >
Run Code Online (Sandbox Code Playgroud)
视图模型类中的代码.
属性看起来像这样:
public MyObservableDictionary<string, UserInfo> Friends
{
get { return _friends; }
set
{
_friends = value;
NotifyOfPropertyChange(() => Friends);
}
}
Run Code Online (Sandbox Code Playgroud)
在Dispatcher计时器中,我每隔3秒调用一个单独的线程新服务方法.
所以我的视图模型的构造函数我有这个:
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DispatcherTimer_Tick;
_dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
_dispatcherTimer.Start();
_threadDispatcher = Dispatcher.CurrentDispatcher;
Run Code Online (Sandbox Code Playgroud)
而计时器滴答方法在这里:
private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
{
new System.Threading.Tasks.Task(() =>
{
//get new data from server …
Run Code Online (Sandbox Code Playgroud) 我是新手WPF程序员.我正在尝试为我的代码添加一些结构:用户控件和MVVM.
在这里研究,我发现人们推荐Caliburn.Micro.另一方面,我在这里和其他地方发现了一些关于Caliburn.Micro在UserControls上玩得不好的投诉.
所以我的问题是:Caliburn.Micro能否很好地使用用户控件?
我试图绑定一个事件与Caliburn Micro,我有一些问题得到正确的消息到该方法.我想添加在更改文本框中的值后按"Enter"键的功能,并执行与旁边的按钮绑定的方法相同的方法.但是,无论按哪个键,我都会遇到以下例外情况:
MyApp.exe中出现"System.InvalidCastException"类型的第一次机会异常
mscorlib.dll中发生了'System.Reflection.TargetInvocationException'类型的第一次机会异常
WindowsBase.dll中出现类型为"System.Reflection.TargetInvocationException"的第一次机会异常
在另一个类似问题绑定KeyDown事件Silverlight的建议下,我尝试过使用ActionExecutionContext,但无济于事.
这是xaml:
<TextBox Name="Threshold"
Margin="5"
Grid.Column="1"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cal:ActionMessage MethodName="ExecuteFilterView">
<cal:Parameter Value="$executionContext"/>
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
方法:
public void ExecuteFilterView(ActionExecutionContext context)
{
//Do stuff...
}
Run Code Online (Sandbox Code Playgroud)
我明白我可能会为自己省去一些麻烦,只是在后面的代码中做一个标准的事件处理程序,但这个应用程序是MVVM中的一个练习并学习使用Caliburn.Micro,所以我想坚持使这个特定的方法工作.
我只是想从活动中发送错误的信息吗?我的xaml编码不正确得到我想要的吗?或者我完全错过了其他的东西?
在我的Windows Phone 8应用程序中,我有项目列表.我为列表项定义了ItemTemplate.
我想在每个项目中显示一个来自视图模型的值,而不是列表项本身.我应该如何设置从列表项到viewmodel的绑定.
我的数据模板是这样的:
<DataTemplate x:Key="template">
<StackPanel>
<TextBlock Text="{Binding Name}"/> <!-- From list item -->
<TextBlock Text="{Binding MyViewModel.Country ?? }"/> <!-- From view model -->
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
那么如何将Country属性绑定到视图模型,而不是列表项(在项目源中).
我有时使用Caliburn.Micro来创建应用程序.
使用最简单的BootStrapper,我可以像这样使用IoC容器(SimpleContainer):
private SimpleContainer _container = new SimpleContainer();
protected override object GetInstance(Type serviceType, string key) {
return _container.GetInstance(serviceType, key);
}
protected override IEnumerable<object> GetAllInstances(Type serviceType) {
return _container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance) {
_container.BuildUp(instance);
}
Run Code Online (Sandbox Code Playgroud)
所以在Configure
方法中我可以像这样添加和注册我的ViewModel:
container.PerRequest<MyMainViewModel>();
Run Code Online (Sandbox Code Playgroud)
我的ViewModel的构造函数可以有一个参数,在请求时由IoC容器注入:
public MyMainViewModel(IWindowManager windowManager)
{
//do the init
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时,它按预期工作 DisplayRootViewFor<MyMainViewModel>()
但是,如果我打算创建更多逻辑并使用Conductor,会发生什么?
在这些例子中,作者使用一个简单的,无IoC的实现来"方便":
为了使这个样本尽可能简单,我甚至没有使用带引导程序的IoC容器.我们先来看看ShellViewModel.它继承自Conductor,实现如下:
Run Code Online (Sandbox Code Playgroud)public class ShellViewModel : Conductor<object> { public ShellViewModel() { ShowPageOne(); } public void ShowPageOne() { ActivateItem(new PageOneViewModel()); } public …
我非常喜欢Caliburn和命名约定绑定,并且惊讶于Visibility的绑定方式与"CanNAME"约定用于保护Action的方式不同.据我所知,只有在Caliburn中明确使用Binding时才使用BooleanToVisibilityConverter,而不是像guard方法那样自动使用.所以我想修改源代码以自动绑定到"bool?ControlNameIsVisible()"(null等于崩溃)或类似.我想知道这是否是正确的方法,如果有人已经完成了实施并且可以在这里分享它.
我正在尝试使用带有WPF的Caliburn.Micro学习.如何在视图中添加多个视图?
<Window x:Class="ProjectName.Views.MainView"
...>
<Grid>
<views:MyControlView />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
另一个视图,使用viewmodel:MyControlViewModel
<UserControl x:Class="ProjectName.Views.MyControlView"
...>
<Grid>
...
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
如果我只是添加视图,它将不会检测到它具有具有适当名称的viewmodel.我怎么能把它绑在上面呢?
我已尝试使用不同的bootstrappers并使用类似cal:Bind.Model ="path/classname/merge of the two".试图将其添加到主视图和usercontrol(MyControlView).我非常感谢有关此事的任何帮助.我几乎卡住了,我真的想用Caliburn.Micro :)
最好的问候,钻石鱼
编辑:我仍然无法让它工作,问题似乎是在引导程序或其他东西.但只是为了澄清,这是我的代码,我正在运行testproject.
MainView xaml:
<Window x:Class="Test.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:views="clr-namespace:Test.Views"
Title="MainWindow" Height="360" Width="640">
<Grid>
<views:MyControlView />
</Grid>
Run Code Online (Sandbox Code Playgroud)
MainViewModel代码:
public partial class MainViewModel : PropertyChangedBase
{
}
Run Code Online (Sandbox Code Playgroud)
MyControlView xaml:
<UserControl x:Class="Test.Views.MyControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
cal:Bind.Model="Test.MyControlViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="{Binding MyProp}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
MyControlView代码:
public class MyControlViewModel : PropertyChangedBase
{
public string MyProp
{
get …
Run Code Online (Sandbox Code Playgroud) WPF和Caliburn.Micro的曲线相对较低.
我的目标是将组合框选定项目的绑定从后面的ShellView代码移动到视图模型,就像组合框的项目集合一样.
XAML:
<Window x:Class="EomDatabaseUtility.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Eom Tool Database Utility" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="258" HorizontalAlignment="Left" Margin="12,41,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" />
<Button Content="Execute" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" VerticalAlignment="Top" Width="75" x:Name="Execute" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="120" x:Name="CatalogName" SelectedValuePath="{Binding Path=SelectedCatalogName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
Code Behind(如果我理解正确的话,目标是不必添加任何代码):
namespace EomDatabaseUtility.Views
{
using System.Windows;
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
}
// --> This should go in the view model, Right?
private string selectedCatalogName; …
Run Code Online (Sandbox Code Playgroud) 好吧我试图使用MahApps.Metro和Caliburn.Micro在一起但我遇到了一些问题
这是我的引导程序
public sealed class TestBootstrapper : Bootstrapper<ShellViewModel>
{
private CompositionContainer container;
protected override void Configure()
{
container = new CompositionContainer(new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
CompositionBatch batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new AppWindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
}
protected override object GetInstance(Type serviceType, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
var exports = container.GetExportedValues<object>(contract);
if (exports.Count() > 0)
{
return exports.First();
}
return base.GetInstance(serviceType, key);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的AppWindowManager
public sealed class AppWindowManager : WindowManager
{
static readonly …
Run Code Online (Sandbox Code Playgroud) 有没有人有关于如何将Caliburn Micro与ModernUi(https://mui.codeplex.com)一起使用的示例或教程?