我是Caliburn Micro的新手,并从这个helloworld示例中学习它.在该示例中,只有2个类型为Application和UserControl的视图(.xaml)和1个视图模型.
我避免使用代码.因此我只有查看和查看模型.我想知道如何捕获我的helloworld应用程序的窗口关闭事件,以便我可以在视图模型中处理它.我的目标:当用户通过按右上角的[x]按钮关闭应用程序时,应用程序会向用户提供反馈.
我已阅读有关IViewAware和IScreen的内容,但我没有找到与我的问题相关的具体示例.
我们非常感谢视图和视图模型的简单示例代码.提前致谢.
PS.我用的是VS2013,C#.
我有一个嵌套在窗口内的用户控件,该窗口充当对话框显示的shell.我在shell窗口中忽略焦点,在托管用户控件中,我使用FocusManager将初始焦点设置为命名元素(文本框),如下所示.
这样做,将光标设置在命名文本框的开头; 但是我希望选择所有文本.
TextBoxSelectionBehavior类(下面)通常就是这样,但在这种情况下不是这样.是否有一个简单的xaml修复程序可以在初始焦点上选择指定文本框中的文本?
干杯,
Berryl
// in app startup
TextBoxSelectionBehavior.RegisterTextboxSelectionBehavior();
/// <summary>
/// Helper to select all text in the text box on entry
/// </summary>
public static class TextBoxSelectionBehavior
{
public static void RegisterTextboxSelectionBehavior()
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(OnTextBox_GotFocus));
}
private static void OnTextBox_GotFocus(object sender, RoutedEventArgs e)
{
var tb = (sender as TextBox);
if (tb != null)
tb.SelectAll();
}
}
Run Code Online (Sandbox Code Playgroud)
<UserControl
<DockPanel KeyboardNavigation.TabNavigation="Local"
FocusManager.FocusedElement="{Binding ElementName=tbLastName}" >
<TextBox x:Name="tbLastName" ... />
Run Code Online (Sandbox Code Playgroud)
根据下面的Rachel的评论,我放弃了FocusManger,支持一些代码:
tbLastName.Loaded += (sender, …Run Code Online (Sandbox Code Playgroud) 我有一个自定义控件,它有一个按钮:
<UserControl x:Class="Gambit.Views.FileSelectionControl"
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"
SnapsToDevicePixels="True"
mc:Ignorable="d">
...
<Button Content="Load"
Margin="5,5,5,5"
Height="22"
Width="70"
IsDefault="True"
IsEnabled="{Binding SelectedFileExists}"
AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding CloseDialogCommand}"/>
...
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我希望在另一个控件中包含此控件,但我想Load在主机控件中设置按钮的可见性; 就像是
<UserControl x:Class="Gambit.Views.SomeOtherControl"
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"
SnapsToDevicePixels="True"
mc:Ignorable="d">
...
<GroupBox Header="Select Test Data">
<Views:FileSelectionControl <Here Set the Load Button Visibility>/>
</GroupBox>
...
</UserControl>
Run Code Online (Sandbox Code Playgroud)
在哪里<Here Set the Load Button Visibility>显示我想设置控件的可见性.如何完成[不破坏MVVM模式]?
谢谢你的时间.