首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WPF中创建一个启动程序

在WPF中创建一个启动程序
EN

Stack Overflow用户
提问于 2016-05-20 10:03:34
回答 1查看 1.1K关注 0票数 0

我目前正在编写一个启动程序,我正处于它的原型化阶段。

我对WPF非常陌生,但不是MVVM架构,但是,我不知道如何将我的ViewModel绑定到CommandBox视图,因为我在使用ViewModel中的方法,也许我是从错误的角度看它,所以如果我做错了什么,请告诉我!)

在输入方面,我有以下ViewModel,它实际上是启动程序的本质:

代码语言:javascript
复制
/// <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如下所示:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-20 12:28:08

我想我知道了,我只需要一个ViewModel,它保存用户正在搜索的命令文本,我将使用它作为启动程序的视图和搜索服务之间的粘合剂。

就像这样:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

很简单,但这可能是我首先要讲的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37343788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档