wpf 滚屏数据显示
2021-03-04 21:26
标签:batch sans translate escape anim alc padding wpf def
近期库房想在出库存放区划分货位存放不同客户拣货后的商品数据。
同时需要在货位摆放屏幕以便显示当前货位被那个客户拣货占用,及商品信息、拣货状态等
由于独立项目,数据来源于api接口,所以只是一个客户端轮播即可。故拿wpf来试试demo
设计为:
1、一个主界面为控制台控制第2,3,....屏显示不同客户(货位)的信息去请求不同的数据
2、第2、3...屏的轮播数据展示
实现:
1、创建项目net下都一致,略过
2、控制不同屏幕轮播不同信息,只需要相关配置即可,这里略过
3、显示轮播信息这里使用一个按钮跳转来完成
跳转到次屏
private void btn_Scroll_Click(object sender, RoutedEventArgs e) { ScrollWindow w = new ScrollWindow(); MultipScreenManager.ShowInScreen(w); }
这里使用到一个主次屏管理,引用了某一位博主(当前忘记了是哪一位,有知道的通知我补登)的MultipScreenManager
public static class MultipScreenManager { #region Property internal static Screen[] AllScreens { get { return Screen.AllScreens; } } internal static Screen PrimaryScreen { get { return Screen.PrimaryScreen; } } internal static IEnumerableMinorScreens { get { return Screen.AllScreens.Where(o => o.Primary == false); } } internal static Screen FirstMinorScreen { get { return MinorScreens.FirstOrDefault(); } } #endregion Property #region Method public static void ShowInScreen(this System.Windows.Window win) { SetScreen(win); win.Show(); } public static void ShowDialogInScreen(this System.Windows.Window win) { SetScreen(win); win.ShowDialog(); } private static void SetScreen(System.Windows.Window win) { var attr = win.GetType().GetCustomAttributes(typeof(MultipScreenAttribute), false).FirstOrDefault(o => o is MultipScreenAttribute); int index = 0; bool ingoreOperation = false; WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen.CenterScreen; if (attr != null) { var temp = (attr as MultipScreenAttribute); index = temp.Index; inScreen = temp.InScreen; ingoreOperation = temp.IngoreMinorScreenError; } Screen screen = PrimaryScreen; if (index == 1 && FirstMinorScreen != null) { screen = FirstMinorScreen; } else if (index > 1 && index MinorScreens.Count()) { screen = MinorScreens.ElementAt(index); } else if (index > 0 && index >= MinorScreens.Count() && ingoreOperation) { return; } switch (inScreen) { case WindowStartupLocationInScreen.CenterScreen: SetWindowInScreenCenter(win, screen); break; case WindowStartupLocationInScreen.Manual: SetWindowInScreenManual(win, screen); break; } } private static void SetWindowInScreenCenter(System.Windows.Window win, Screen screen) { win.Top = screen.WorkingArea.Y + (screen.WorkingArea.Height - win.Height) / 2; win.Left = screen.WorkingArea.X + (screen.WorkingArea.Width - win.Width) / 2; } private static void SetWindowInScreenManual(System.Windows.Window win, Screen screen) { win.Top = screen.WorkingArea.Y; win.Left = screen.WorkingArea.X; } #endregion Method } [AttributeUsage(AttributeTargets.Class)] public class MultipScreenAttribute:Attribute { public MultipScreenAttribute(ScreenType type = ScreenType.Primary, WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen.CenterScreen) : this((int)type, inScreen) { } public MultipScreenAttribute(int index = 0, WindowStartupLocationInScreen inScreen = WindowStartupLocationInScreen.CenterScreen) { Index = index; InScreen = inScreen; } /// /// 在窗体初始化显示的位置 /// public WindowStartupLocationInScreen InScreen { get; private set; } /// /// 屏幕索引, 0为主屏,1+为次屏 /// public int Index { get; private set; } /// /// 当任何指定次屏没有找到时,如果该值为TRUE,则忽略这个页面的显示,否则将显示在主屏 /// public bool IngoreMinorScreenError { get; private set; } } public enum ScreenType { /// /// 主屏 /// Primary = 0, /// /// 次屏 /// Minor = 1, } public enum WindowStartupLocationInScreen { Manual = 0, CenterScreen = 1, }
次屏ui:
"WpfApp1.ScrollWindow" 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="ScrollWindow" Loaded="ScrollWindow_Loaded" > "storyboard"> "0:0:1" From="300" To="0" Storyboard.TargetName="stackPanel" Storyboard.TargetProperty="RenderTransform.Y"/> "100"> "*"> "0"> "50"> "50"> "170"> "*"> "1" Name="scrollViewer" HorizontalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch" VerticalScrollBarVisibility="Hidden" VerticalContentAlignment="Stretch"> "stackPanel" Margin="5 5 5 5" > "orderItem_list" ItemsSource="{Binding Results,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" AutoGenerateColumns="False" GridLinesVisibility="None" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" BorderThickness="0" FontSize="25" EnableRowVirtualization="false" EnableColumnVirtualization="False" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" AlternationCount="2" HeadersVisibility="None"> "Sku" Binding="{Binding SkuNo}" Width="160"> "名称" Binding="{Binding SkuName}" Width="300"> "数量" Binding="{Binding Amount}" Width="100"> "状态" Binding="{Binding StatusName}" Width="120">
次屏逻辑交互:
////// ScrollWindow.xaml 的交互逻辑 /// [MultipScreen(1, WindowStartupLocationInScreen.CenterScreen)] public partial class ScrollWindow : Window { private System.Timers.Timer _timer; private ScrollWindowDataContextModel _data; private int _index; private int PageSize = 21; private double Interval = 5000; private int pageCount; private Storyboard _storyboard; private int typeBatch = 1; public ScrollWindow() { AutoScreen();//需要设置高度、宽度才会遵循MultipScreen的指定显示屏显示 //this.Width = 800; //this.Height = 500; InitializeComponent(); this.KeyDown += ScrollWindow_KeyDown; _data = GetAllData(); txtHHName.Content = $"{_data.HHName}"; txtContract.Content = $"合同号:{_data.ContractCode}"; txtAddress.Content = $"地址:{_data.Address}"; } private void ScrollWindow_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape)//Esc键 { this.Close(); } } private void ScrollWindow_Loaded(object sender, RoutedEventArgs e) { //AutoScreen(); if (_timer == null) { _storyboard = (Storyboard)this.FindResource("storyboard"); System.Threading.Tasks.Task.Factory.StartNew(() => { pageCount = (_data.Results.Count() + PageSize - 1) / PageSize; _index= 1; Dispatcher.BeginInvoke(new Action(() => { stackPanel.RenderTransform = new TranslateTransform(0, 0); })); ShowData(); }); _timer = new System.Timers.Timer(); _timer.Interval = Interval; _timer.Elapsed += Action; _timer.Start(); } } private void AutoScreen() { this.WindowState = System.Windows.WindowState.Normal; this.WindowStyle = System.Windows.WindowStyle.None; this.ResizeMode = System.Windows.ResizeMode.NoResize; //this.Topmost = true; if (System.Windows.Forms.Screen.AllScreens.Where(o => o.Primary == false).Count() == 0) { this.Left = 0.0; this.Top = 0.0; this.Width = System.Windows.SystemParameters.WorkArea.Width;//.PrimaryScreenWidth; this.Height = System.Windows.SystemParameters.WorkArea.Height;//.PrimaryScreenHeight; } else { var minorScreen = System.Windows.Forms.Screen.AllScreens.Where(o => o.Primary == false).First(); this.Left = 0.0; this.Top = 0.0; var name = minorScreen.DeviceName; this.Width = minorScreen.WorkingArea.Width; this.Height = minorScreen.WorkingArea.Height; } } private ScrollWindowDataContextModel GetAllData(int type=0) { ScrollWindowDataContextModel allData = new ScrollWindowDataContextModel(); allData.HHName = "赵四"; allData.ContractCode = "1003175664863"; allData.Address = "北京市大兴区旧宫镇 住总万科广场C座11层"; List rList = new List (); for (int i = 0; i 100; i++) { WindowScrollResultModel r = new WindowScrollResultModel() { SkuNo = $"sku{i.ToString()}-{type}", SkuName = $"sku{i.ToString()}-{type}", Amount = i, StatusName = i.ToString().Contains("3") ? "未拣货" : i % 2 == 0 ? "已拣货" : "已复核", }; rList.Add(r); } allData.Results = new ObservableCollection (rList); return allData; } private void Action(object sender, ElapsedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { stackPanel.RenderTransform = new TranslateTransform(0, 0); _storyboard.Begin(); })); _index++; if (_index > pageCount) { // 可以在这循环完一轮后重新加载数据 _data = GetAllData(typeBatch); typeBatch++; _index = 1; } ShowData(); } private void ShowData() { Dispatcher.BeginInvoke(new Action(() => { List dataList = GetPageData(_index); StringBuilder sbMsg = new StringBuilder(); dataList.ForEach(x=> { sbMsg.Append($"{x.SkuNo} {x.SkuName} {x.Amount} {x.StatusName} \r\n"); }); //lab_text.Content = sbMsg.ToString() ; orderItem_list.ItemsSource = dataList; })); } private List GetPageData(int pageIndex) { if (_data != null) { return _data.Results.Skip((pageIndex - 1) * PageSize).Take(PageSize).ToList(); } return null; } private void ScrollWindow_MouseEnter(object sender, MouseEventArgs e) { _timer.Stop(); } private void ScrollWindow_MouseLeave(object sender, MouseEventArgs e) { _timer.Start(); } private void ScrollWindow_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); //AutoScreen(); } } } public class ScrollWindowDataContextModel : NotificationObject { public string _HHName; public string HHName { get => _HHName; set { _HHName = value; RaisePropertyChanged("HHName"); } } public string _ContractCode; public string ContractCode { get => _ContractCode; set { _ContractCode = value; RaisePropertyChanged("ContractCode"); } } public string _Address; public string Address { get => _Address; set { _Address = value; RaisePropertyChanged("Address"); } } public ObservableCollection results; public ObservableCollection Results { get => results; set { results = value; RaisePropertyChanged("Results"); } } } public class WindowScrollResultModel { public string SkuNo { get; set; } public string SkuName { get; set; } public int Amount { get; set; } public string StatusName { get; set; } }
注意点:
让次屏全屏显示时让width、height=获取到的屏幕大小
开启timer设置间隔时间轮播,默认从第一页开始,设定每屏幕可轮播的行数,到最后一页后重新从第一页轮播
也可以开启鼠标经过轮播体时暂停,离开时继续:MouseEnter="ScrollWindow_MouseEnter" MouseLeave="ScrollWindow_MouseLeave"
Storyboard的DoubleAnimation设置属性控制进入轮播的进入效果及位置等
需要ui好看点,可以引入皮肤库,这这里使用了:MaterialDesign
只需要在app.xaml中增加配置:
"pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml"> "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" > "pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml"> "pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml">
然后在使用页面引入:xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
MaterialDesign
wpf 滚屏数据显示
标签:batch sans translate escape anim alc padding wpf def
原文地址:https://www.cnblogs.com/zengwei/p/12922569.html