2016-05-20 5 views
0

Я сейчас в процессе написания пусковой установки, и я нахожусь на этапе прототипирования.Создание пусковой установки в WPF

Я действительно новичок в 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)); 
    } 
} 

Идея заключается в том, что команды могут быть сохранены в удаленной базе данных или что-то для хранения вы может понравиться, а также локально.

The (вид 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> 

ответ

0

Я думаю, что я понял это, я просто иметь ViewModel, который содержит текст, который команда пользователь ищет и я 'll использовать это как клей между представлением и службой поиска пусковой установки.

Что-то вроде этого:

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> 

Довольно простой, но это, наверное, то, что мне нужно, чтобы начать с.