使用MVVM模式

时间:2017-11-03 19:44:01

标签: c# wpf mvvm busyindicator

如果有人能告诉我如何实现简单的繁忙指标/进度条,我将不胜感激。我已经尝试了我能找到的每个答案和教程,但我无法让它发挥作用。我刚刚开始,所以我缺乏掌握复杂答案的知识。

我有一些linq查询需要一段时间才能运行,同时从日期选择器中选择一个不同的日期,我想显示一些简单的加载指示器,这样用户就不会认为窗口只是冻结了。

我试图实现Xceed busyindicator,但这并不像我希望的那么容易。我想我可能需要使用BackgroundWorker,但我无法让它工作。我只想在ViewModel中设置我的属性时显示任何类型的进度条。

XAML:

<wpfTool:BusyIndicator 
    IsBusy="{Binding IsBusy}" 
    BusyContent="Calculating... Please wait..." >
</wpfTool:BusyIndicator> 

视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using My.Model;
using LiveCharts;
using LiveCharts.Wpf;
using System.Windows;
using Xceed.Wpf;

namespace My.ViewModel
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public MyViewModel()
        {
            SetBusinessDays();
            SetHolidays();
            SetWorkingDays();     
        }

        private bool _isBusy;
        public bool IsBusy 
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }

        public DateTime mDate = DateTime.Now;
        public DateTime MDate
        {

            get { return mDate; }
            set
            {
                if (value == mDate)
                {
                    return;
                }

                else
                {
                    mDate = value;
                    OnPropertyChanged("MDate");

                    SetBusy(); *** I was hoping this would work ***

                    SetBusinessDays();
                    SetHolidays();
                    SetWorkingDays();
                    SetDaysCompleted();

                    SetNotBusy(); *** I was hoping this would work ***
                }

            }

        }


        private void SetBusinessDays()
        {
            BusinessDays = Enumerable.Range(1, DateTime.DaysInMonth(MDate.Year, MDate.Month))
                                 .Select(day => new DateTime(MDate.Year, MDate.Month, day))
                                 .Count(d => d.DayOfWeek != DayOfWeek.Sunday &&
                                             d.DayOfWeek != DayOfWeek.Saturday);
        }


        private void SetHolidays()
        {
            int.TryParse(MDate.ToString("MM"), out int month);
            HolidaysInMonth = db.Holidays.Where(a => (a.Date <= MDate) && (a.Date.Month) == month).Select(a => a.ID).Count();
        }

        private void SetWorkingDays()
        {
            int.TryParse(MDate.ToString("MM"), out int month);
            WorkingDays = (BusinessDays - HolidaysInMonth);
        }        


        private void SetBusy()
        {
            IsBusy = true;
        }

        private void SetNotBusy()
        {
            IsBusy = false;
        }

我已经阅读了你提供给Peter的例子。他们没有帮助!这不是一个重复的问题。请让用户提供答案,因为他们认为这对社区有帮助。

0 个答案:

没有答案