WPF WCF produce data as service and WPF consume data as client periodically

1.In WCF,Add WCF Service,BookService

image

 

2.

//D:\C\WcfService1\WcfService1\Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WcfService1
{
    public class Book
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Abstract { get; set;  }
        public string Author { get; set; }
        public string ISBN { get; set; }
        public string Comment { get; set;  }
        public string Content { get; set; }
        public string Summary {  get; set; }
        public string Title {  get; set; }
        public string Topic {  get; set; }
    }
}

//D:\C\WcfService1\WcfService1\IBookService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IBookService" in both code and config file together.
    [ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        long Get_Increment_Id();

        [OperationContract]
        List<Book> Get_Books_List(int cnt = 1000000);
    }
}

//D:\C\WcfService1\WcfService1\BookService.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BookService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select BookService.svc or BookService.svc.cs at the Solution Explorer and start debugging.
    public class BookService : IBookService
    {
        long id = 0;

        public List<Book> Get_Books_List(int cnt = 100)
        {
            List<Book> booksList = new List<Book>();
            for(int i=0;i<cnt;i++)
            {
                id = Get_Increment_Id();
                booksList.Add(new Book()
                {
                    Id =id,
                    Name=$"Name_{id}",
                    ISBN=$"ISBN_{id}_{Guid.NewGuid():N}",
                    Author=$"Author_{id}",
                    Abstract=$"Abstract_{id}",
                    Comment=$"Comment_{id}",
                    Content=$"Content_{id}",
                    Summary=$"Summary_{id}",
                    Title=$"Title_{id}",
                    Topic=$"Topic_{id}"
                });
            }
            return booksList;
        }

        public long Get_Increment_Id()
        {
            return Interlocked.Increment(ref id);
        }
    }
}

3.

image

 

 

4.In WPF,Add project reference of WCF, dll file;

5.

//D:\C\WpfApp19\WpfApp19\MainWindow.xaml
<Window x:Class="WpfApp19.MainWindow"
        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:WpfApp19"
        mc:Ignorable="d"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Grid>
        <DataGrid ItemsSource="{Binding BooksCollection}"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  VirtualizingPanel.CacheLength="5,5"
                  VirtualizingPanel.CacheLengthUnit="Item"
                  ScrollViewer.IsDeferredScrollingEnabled="True"
                  ScrollViewer.CanContentScroll="True"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True"
                  UseLayoutRounding="True"
                  SnapsToDevicePixels="True"
                  AutoGenerateColumns="True"
                  CanUserAddRows="False"
                  IsReadOnly="True">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="FontSize" Value="30"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="50"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>


//D:\C\WpfApp19\WpfApp19\MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WcfService1;

namespace WpfApp19
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        IBookService bkService;
        System.Timers.Timer tmr;
        public MainVM()
        {
            bkService = new BookService();
            tmr = new System.Timers.Timer();
            tmr.Interval = 1;
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Start();
        }

        private void Tmr_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (tmr.Interval < 2)
            {
                tmr.Interval = 10000;
            }

            BooksCollection = new ObservableCollection<Book>(bkService.Get_Books_List(1000000));
            MainTitle = $"{DateTime.Now},first id:{BooksCollection.FirstOrDefault().Id},last id:{BooksCollection.LastOrDefault().Id}";
        }


        private string mainTitle = $"{DateTime.Now},loading...";
        public string MainTitle
        {
            get
            {
                return mainTitle;
            }
            set
            {
                if(value!=mainTitle)
                {
                    mainTitle = value;
                    OnPropertyChanged();
                }
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if (value != booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propName = "")
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }
}

 

 

 

image

 

 

 

image

 

 

image

 

posted @ 2026-04-22 21:29  FredGrit  阅读(8)  评论(0)    收藏  举报