Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF应用程序中实现服务层_Wpf_Mvvm_Data Binding - Fatal编程技术网

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

在WPF应用程序中实现服务层,wpf,mvvm,data-binding,Wpf,Mvvm,Data Binding,我正在研究WPF应用程序,而且是新手,所以这可能是一个愚蠢的问题 我想做的是从我的服务层触发绑定。为了更好地解释,我创建了一个示例。在本例中,我希望在WPF屏幕上用记录的消息(来自服务方法)绑定网格 请参阅我在服务类的ServiceMethod中的注释。这是我想触发绑定的地方 我试图以最好的方式解释,但如果您需要进一步澄清,请不要犹豫 XAML <Window x:Class="WpfApp1.ServiceExample" xmlns="http://schemas.mi

我正在研究WPF应用程序,而且是新手,所以这可能是一个愚蠢的问题

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

请参阅我在服务类的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>
b单击

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();
    }
}
消息

public partial class ServiceExample : Window
{
    public ServiceExample()
    {
        InitializeComponent();
    }
}
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;
    }
}
服务

public partial class ServiceExample : Window
{
    public ServiceExample()
    {
        InitializeComponent();
    }
}
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;
    }
}
类服务
{
公共列表消息日志;
公共服务()
{
this.MessageLog=新列表();
}
公共int服务方法()
{
int结果=0;
用于(整数计数器=0;计数器<10;++计数器)
{
//这就是绑定应该触发的地方
Add(newmessage(){Text=string.Format(“{0}:{1}”,DateTime.Now.Ticks,counter)});
结果+=计数器;
}
返回结果;
}
}

在MVVM中,您没有来自服务的绑定,从来没有。服务的目的是作为数据的管道,其中可能包含一些有限的业务逻辑。服务的生命周期可能很短,并且通常不维护任何状态


您的绑定应该在视图和viewmodel之间,以任何其他方式绑定都会违反模式。

通常您会使用mvvm,并且viewmodel将具有视图绑定的属性。返回数据的服务通常是模型层的一部分。viewmodel通常会实例化或解析此类服务并调用该方法。有时,viewmodel可能会订阅模型“服务”的事件,其中该服务以某种方式间歇性地接收数据,或者以某种方式表示viewmodel无法选择何时获取该数据。