在WPF应用程序中实现服务层

时间:2018-04-19 19:19:55

标签: wpf mvvm data-binding

我正在研究WPF应用程序并且刚接触它,所以这可能是一个愚蠢的问题,

我想要做的是从我的服务层触发绑定。为了更好地解释,我创建了一个例子。在这个例子中,我想在我的WPF屏幕上用已记录的messag(来自服务方法)绑定网格。

在Service类的ServiceMethod中查看我的评论。这是我想触发绑定的地方。

我试图以最好的方式解释,但如果您需要进一步澄清,请不要犹豫。

XAML

<Window x:Class="WpfApp1.ServiceExample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="ServiceExample" Height="300" Width="300">
    <Window.Resources>
        <local:ServiceExampleViewModel x:Key="serviceExampleViewModel"></local:ServiceExampleViewModel>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <DataGrid ItemsSource="{Binding MessageLog,
                    Source={StaticResource serviceExampleViewModel},
                    Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}">
            </DataGrid>
            <Button Content="Call Service" Command="{Binding CallService,
                Mode=TwoWay, Source={StaticResource serviceExampleViewModel}}"></Button>
            <Label Content="{Binding ServiceResult, 
                    Source={StaticResource serviceExampleViewModel},
                    Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}"></Label>
        </StackPanel>

    </Grid>
</Window>

ServiceExample

public partial class ServiceExample : Window
{
    public ServiceExample()
    {
        InitializeComponent();
    }
}

btnClick

public class btnClick : System.Windows.Input.ICommand
{
    private Action WhatToExecute;
    private Func<bool> WhenToExecute;
    public btnClick(Action what, Func<bool> when)
    {
        WhatToExecute = what;
        WhenToExecute = when;
    }
    public void Refresh()
    {
        if (this.CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
    public event EventHandler CanExecuteChanged;
    public bool CanExecute(object parameter)
    {
        return WhenToExecute();
    }
    public void Execute(object parameter)
    {
        WhatToExecute();
    }
}

ServiceExampleViewModel

class ServiceExampleViewModel : System.ComponentModel.INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private int serviceResult;
    public int ServiceResult
    {
        get { return serviceResult; }
        set
        {
            serviceResult = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ServiceResult"));
        }
    }
    public btnClick CallService { get; set; }
    public Service ServiceObj { get; set; }
    public ServiceExampleViewModel()
    {
        ServiceObj = new Service();
        this.CallService = new btnClick(CallServiceMethod, () => { return true; });
    }

    private void CallServiceMethod()
    {
        this.ServiceResult = this.ServiceObj.ServiceMethod();
    }
}

消息

class Message
{
    public string Text { get; set; }
}

服务

class Service
{
    public List<Message> MessageLog;

    public Service()
    {
        this.MessageLog = new List<Message>();
    }

    public int ServiceMethod()
    {
        int result = 0;
        for (int counter = 0; counter < 10; ++counter)
        {
            //This is where binding should trigger
            this.MessageLog.Add(new Message() { Text = string.Format("{0}:{1}", DateTime.Now.Ticks, counter) });

            result += counter;
        }
        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

在MVVM中,你没有来自服务的绑定,而不是永远。服务的目的是成为数据的管道,可能包含一些有限的业务逻辑。服务的生命周期很短,而且他们通常不会维持任何状态。

您的视图和视图模型之间应该绑定,绑定任何其他方式都违反了模式。