我目前正在编写一个启动程序,我正处于它的原型化阶段。
我对WPF非常陌生,但不是MVVM架构,但是,我不知道如何将我的ViewModel绑定到CommandBox视图,因为我在使用ViewModel中的方法,也许我是从错误的角度看它,所以如果我做错了什么,请告诉我!)
在输入方面,我有以下ViewModel,它实际上是启动程序的本质:
/// <summary>
/// Provides a buffer of characters in order to search for candidates that matches the buffer.
/// </summary>
public class CommandBufferViewModel : INotifyPropertyChanged
{
private readonly StringBuilder _input;
public CommandBufferViewModel()
{
_input = new StringBuilder();
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Deletes a character from the buffer.
/// </summary>
public void DeleteKey(int index, int length = 1)
{
_input.Remove(index, length);
OnPropertyChanged(nameof(DeleteKey));
}
/// <summary>
/// Adds a character to the buffer.
/// </summary>
public void ProcessKey(int index, char key)
{
_input.Insert(index, key);
OnPropertyChanged(nameof(ProcessKey));
}
/// <summary>
/// Returns results that matches the current buffer.
/// </summary>
/// <returns>Results that matches the current buffer.</returns>
public async Task<CommandResults> Search()
{
// NYI
return default(CommandResults);
}
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}这里的想法是,命令可以存储在远程数据库中,也可以存储在您喜欢的任何本地存储中。
(CommandBox视图) TextBox如下所示:
<TextBox
x:Class="Yalla.Launcher.Views.CommandBox"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bs="clr-namespace:Yalla.Launcher.Behaviors"
mc:Ignorable="d"
Width="Auto" Height="Auto" VerticalContentAlignment="Center" BorderThickness="1">
<i:Interaction.Behaviors>
<bs:WatermarkBehavior Text="Enter Command... (type ? for help)" />
</i:Interaction.Behaviors>
</TextBox>发布于 2016-05-20 12:28:08
我想我知道了,我只需要一个ViewModel,它保存用户正在搜索的命令文本,我将使用它作为启动程序的视图和搜索服务之间的粘合剂。
就像这样:
namespace Yalla.Launcher.ViewModels
{
using System.ComponentModel;
using Commands;
using Features.Commands.Search;
public class SearchViewModel : INotifyPropertyChanged, ISearchableText
{
private SearchCommandHandler _enterCommandHandler;
private string _searchText;
public event PropertyChangedEventHandler PropertyChanged;
public SearchCommandHandler Search => _enterCommandHandler ?? (_enterCommandHandler = new SearchCommandHandler(new SearchService(this)));
public string SearchText
{
get
{
return _searchText;
}
set
{
_searchText = value;
OnPropertyChanged(nameof(SearchText));
}
}
protected virtual void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}最后,我像这样将它绑定到TextBox:
<ctrls:LauncherTextBox
x:Class="Yalla.Launcher.Views.LauncherSearchBox"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bs="clr-namespace:Yalla.Launcher.Behaviors"
xmlns:ctrls="clr-namespace:Yalla.Launcher.Controls"
xmlns:vm="clr-namespace:Yalla.Launcher.ViewModels"
mc:Ignorable="d"
Width="Auto" Height="Auto"
VerticalContentAlignment="Center" BorderThickness="1"
Text="{Binding SearchText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ctrls:LauncherTextBox.DataContext>
<vm:SearchViewModel />
</ctrls:LauncherTextBox.DataContext>
<i:Interaction.Behaviors>
<bs:WatermarkBehavior Text="Enter Command... (type ? for help)" />
</i:Interaction.Behaviors>
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding Search}"></KeyBinding>
</TextBox.InputBindings>
</ctrls:LauncherTextBox>很简单,但这可能是我首先要讲的。
https://stackoverflow.com/questions/37343788
复制相似问题