这是我的XAML视图(为了便于阅读,省略了一些代码):
<Window ... xmlns:c="http://www.caliburnproject.org">
<Button Content="Close without saving" c:Message.Attach="Close(false)" />
<Button Content="Save and Close" c:Message.Attach="Close(true)" />
</Window>
Run Code Online (Sandbox Code Playgroud)这是ViewModel中的代码:
public void Close(bool save)
{
if (save)
{
// save the data
}
TryClose();
}
Run Code Online (Sandbox Code Playgroud)这当然不起作用 - 因为动作参数"true"和"false"不是XAML中的对象或对象属性.如何使这项工作,并在Caliburn Micro中将一个布尔值作为Action参数发送? 我正在尝试使用caliburn micro消息来触发我创建的附加事件:
public static class DataChanging
{
public delegate void DataChangingEventHandler(object sender, DataChangingEventArgs e);
public static readonly RoutedEvent ChangingEvent =
EventManager.RegisterRoutedEvent("Changing",
RoutingStrategy.Bubble,
typeof(DataChangingEventHandler),
typeof(DataChanging));
public static void AddChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).AddHandler(DataChanging.ChangingEvent, handler);
}
public static void RemoveChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).RemoveHandler(DataChanging.ChangingEvent, handler);
}
public static bool GetActivationMode(DependencyObject obj)
{
return (bool)obj.GetValue(ActivationModeProperty);
}
public static void SetActivationMode(DependencyObject obj, bool value)
{
obj.SetValue(ActivationModeProperty, value);
}
public static readonly DependencyProperty ActivationModeProperty =
DependencyProperty.RegisterAttached("ActivationMode",
typeof(bool),
typeof(DataChanging),
new FrameworkPropertyMetadata(false,
HandleActivationModeChanged));
private static …
Run Code Online (Sandbox Code Playgroud) 当用户单击窗口的关闭按钮时,是否可以从ViewModel取消关闭或者是否必须使用后面的代码?
据我所知,CanClose或TryClose不能解决问题.
我有一个MainView.xaml,绑定到MainViewModel就好了.
我想尝试的是将我在主窗体上的许多控件分成UserControls.
现在我将UserControls与MainView一起放在Views文件夹中,并命名为LeftSideControlView.xaml和RightSideControlView.xaml.相应的ViewModel位于ViewModels文件夹中,名为LeftSideControlViewModel等.
我成功将usercontrols添加到主视图:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<UserControls:LeftSideControlView cal:Bind.Model="{Binding}" />
<UserControls:RightSideControlView cal:Bind.Model="{Binding}"
Grid.Column="1" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
它们在设计师中正确显示.这是xaml中的其中一个:
<UserControl x:Class="TwitterCaliburnWPF.Library.Views.LeftSideControlView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<StackPanel>
<Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/>
<TextBox x:Name="TextBox1"
Width="200"
HorizontalAlignment="Center" FontSize="25" Margin="5" />
<Button Width="200" Height="50" Content="Button!" Margin="20" />
</StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我使用Castle.Windsor在AppBootstrapper for Caliburn中添加了viewmodels及其接口.
public class ApplicationContainer : WindsorContainer
{
public ApplicationContainer()
{
// Register all dependencies here
Register(
Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifeStyle.Is(LifestyleType.Singleton),
Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Is(LifestyleType.Singleton),
Component.For<ILeftSideControlViewModel>().ImplementedBy<LeftSideControlViewModel>(),
Component.For<IRightSideControlViewModel>().ImplementedBy<RightSideControlViewModel>()
);
RegisterViewModels();
}
private void RegisterViewModels()
{ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在ListBox ItemTemplate中获取上下文菜单以调用父视图模型上的方法,并将作为参数单击的项目传入.我有这个工作项目模板中的其他按钮,但对于上下文菜单,它似乎失败了.
我有以下xaml(缩写为清晰):
<ListBox>
<ListBox.GroupStyle>
<GroupStyle>
...
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ContextMenu>
<ContextMenu Name="cm">
<MenuItem Header="Open"
cal:Message.Attach="Open($dataContext)">
</MenuItem>
</Grid.ContextMenu>
<TextBlock VerticalAlignment="Center" >
.. text..
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,这与视觉树不同的事实有关,因此Caliburn无法可靠地解决方法.我确定这是一个常见问题,我尝试了一些我在网上发现的东西,但似乎没有任何效果.
有任何想法吗??
我刚刚学习WPF和Caliburn.Micro.我正在按照此处提供的代码:http: //caliburnmicro.codeplex.com/wikipage?title = Customizing20The%20Bootstrapper&referringTitle=Documentation
显然这段代码适用于Silverlight,但我的项目是WPF,因此我收到的错误是CompositionHost未定义.
该文件表明我需要直接在wpf中初始化容器,但是没有文档可以显示如何.如何直接初始化容器?
编辑1引导过滤器在文档中是这样的:
container = CompositionHost.Initialize(
new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
)
);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(container);
container.Compose(batch);
Run Code Online (Sandbox Code Playgroud)
我把它转换成:
var catalog =
new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
this.container = new CompositionContainer(catalog);
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(this.container);
this.container.Compose(batch);
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,我收到的错误是MEF无法找到IShell的实现
Could not locate any instances of contract IShell.
Run Code Online (Sandbox Code Playgroud)
我相信我的MEF初始化是不正确的.你能帮我解决一下吗?
我正在尝试创建类似于MDI选项卡式界面的东西,因此左侧有一个导航窗格(一个列表框),右侧有一个ContentPresenter.
我有一个ShellViewModel,它上面有一个名为AvailAbleScreens的BindableCollection,我成功地使用ListViews DataTemplate绑定到该列表:
<ListView x:Name="AvailableScreens">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<BulletDecorator />
<Button x:Name="DisplayView">
<TextBlock Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
</Button>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,虽然Button的名称设置正确,但我不能让Command为我火.在MdiViewModel类上,我有以下代码用于该按钮:
public bool CanDisplayView()
{
return true;
}
public void DisplayView()
{
MessageBox.Show("Hello");
}
Run Code Online (Sandbox Code Playgroud)
所有的Caliburn.Micro示例都通过约定绑定到x:Name属性,但如果我删除Text ="{Binding}"它会停止工作,所以我怀疑这种数据绑定方式对子模型不起作用?
无论如何,Shell的ViewModel目前非常简单:
ShellViewModel
* AvailableScreens
-MdiViewModel1
-MdiViewModel2
* CurrentActiveScreen
Run Code Online (Sandbox Code Playgroud)
任何想法我如何用Caliburn.Micro做到这一点?Rob Eisenberg在Twitter上向我建议我可能想要在进入完全成熟的Caliburn框架之前开始使用Caliburn.Micro.
我在我的WPF应用程序中使用Caliburn.Micro的.NET(Rx)的Reactive扩展.我正在尝试移植我的WPF应用程序以使用MVVM架构,我需要监视TextBox控件的Text属性中的更改.
如果Text属性的最后一次更改超过3秒前,我需要调用服务的LoadUser方法.
将逻辑从我的旧解决方案移植到具有MVVM架构的新解决方案.
XAML:
<TextBox Name="Nick"
Grid.Row="0"
FontSize="14"
Margin="2,2,2,2"
HorizontalAlignment="Stretch"
TextChanged="Nick_TextChanged" />
Run Code Online (Sandbox Code Playgroud)
在代码背后我有这个:
...
Observable.FromEvent<TextChangedEventArgs>(Nick, "TextChanged")
.Select(e => ((TextBox)e.Sender).Text)
.Where(text => text.Length > 3)
.Do(LoadUser)
.Throttle(TimeSpan.FromSeconds(3000))
.Subscribe(LoadUser);
...
private void LoadUser(string text){...}
Run Code Online (Sandbox Code Playgroud)
我想在我的视图模型类中使用Observable.FromEvent.像这样的东西
视图:
<TextBox Name="Nick"
Grid.Row="0"
FontSize="14"
Margin="2,2,2,2"
HorizontalAlignment="Stretch"
Micro:Message.Attach="[TextChanged]=[Action TextChanged()]"/>
Run Code Online (Sandbox Code Playgroud)
查看型号:
[Export(typeof(IAddFriendViewModel))]
public class AddFriendViewModel : Screen, IAddFriendViewModel
{
private string _nick;
public string Nick
{
get { return _nick; }
set
{
_nick = value;
NotifyOfPropertyChange(()=>Nick);
}
}
...
//how can I access …
Run Code Online (Sandbox Code Playgroud) 我很确定这已经在某个地方得到了解答,但我似乎无法在我的生活中找到它.
我正在尝试使用TabControl在UserControls之间切换(每个选项卡都不同,因此不使用Items)
这是细分:我有我的主视图和3个用户控件.Mainview有一个选项卡控件 - 每个选项卡应显示不同的用户控件.
我可以很容易地将tabcontrol对象设置为usercontrol但是它没有绑定到viewmodel,只绑定了视图.
所以我在我的VM中使用Conductor,以及ActivateItem.这是它开始变得奇怪/令人沮丧的地方.应用程序从选中Tab0开始,但Tab2(最后一个选项卡)内容.单击任何其他选项卡,为该选项卡加载正确的ViewModel.单击返回Tab0,同时加载正确的内容.
我如何让它停下来?另外,如果切换标签没有再次重新初始化视图模型,清除已经输入的字段,我真的很喜欢它.
无论如何,这是我的一些消息来源,我打算将它放在这里并在我打破鼠标之前处理其他事情.
视图:
<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row ="1">
<TabItem Header="PC Information">
<Grid>
<ContentControl x:Name="LoadRemoteInfo" cal:View.Model="{Binding ActiveItem}"/>
</Grid>
</TabItem>
<TabItem Header="Remote Tools">
<Grid>
<ContentControl x:Name="LoadRemoteTools" cal:View.Model="{Binding ActiveItem}"/>
</Grid>
</TabItem>
<TabItem Header="CHRemote">
<Grid>
<ContentControl x:Name="LoadCHRemote" cal:View.Model="{Binding ActiveItem}"/>
</Grid>
</TabItem>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
和ViewModel:
class MainViewModel : Conductor<object>
{
RemoteInfoViewModel remoteInfo = new RemoteInfoViewModel();
RemoteToolsViewModel remoteTools = new RemoteToolsViewModel();
CHRemoteViewModel chRemote = new CHRemoteViewModel();
public MainViewModel()
{
ActivateItem(remoteInfo);
}
public void LoadRemoteInfo()
{
ActivateItem(remoteInfo);
}
public void LoadRemoteTools() …
Run Code Online (Sandbox Code Playgroud) http://caliburnmicro.com上的Caliburn.Micro主页提出了以下声明,但是我无法使用我从这个示例中可以想到的任何变体使用CMBox控件.不管怎么说这都不会起作用,因为这些名字并不相同.有没有人有CM示例允许我获得PasswordBox的值?是否需要特定版本的CM?我正在运行CM的1.5.2版本.理想情况下不使用附加属性,但如果可以使用CM,那么唯一的方法就好了.请不要在安全问题上讲课,因为这不是我的问题.
使用参数和保护方法自动在视图和视图模型之间应用方法
<StackPanel>
<TextBox x:Name="Username" />
<PasswordBox x:Name="Password" />
<Button x:Name="Login" Content="Log in" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
public bool CanLogin(string username, string password)
{
return !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password);
}
public string Login(string username, string password)
{
...
}
Run Code Online (Sandbox Code Playgroud) caliburn.micro ×10
wpf ×7
c# ×6
mvvm ×2
xaml ×2
.net-4.0 ×1
c#-4.0 ×1
caliburn ×1
contextmenu ×1
data-binding ×1
mef ×1
passwordbox ×1
tabcontrol ×1