主頁(yè) > 知識(shí)庫(kù) > 仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條

仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條

熱門標(biāo)簽:百度競(jìng)價(jià)排名 集中運(yùn)營(yíng)管理辦法 網(wǎng)站排名優(yōu)化 服務(wù)器配置 硅谷的囚徒呼叫中心 地方門戶網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云

為了界面友好,一般的操作時(shí)間較長(zhǎng)時(shí),都需要增加進(jìn)度條提示。由于WPF自帶的進(jìn)度條其實(shí)不怎么好看,而且沒啥視覺效果。后來(lái),裝VS2012時(shí),發(fā)現(xiàn)安裝過(guò)程中進(jìn)度條效果不錯(cuò),于是上網(wǎng)查了資料。學(xué)習(xí)了ModernUI(開源的),地址:https://github.com/firstfloorsoftware/mui。

  后來(lái),做了嘗試寫了個(gè)Demo,效果不錯(cuò)。另外,專門錄制了tif文件,方便大家看到效果。廢話不多說(shuō),先展示效果:

一、效果展示

  A、VS2012安裝界面圖;

  B、個(gè)人嘗試Demo效果圖: 

二、實(shí)現(xiàn)說(shuō)明

  1、下載MUI相關(guān)代碼或者dll文件;

  2、工程中引入該dll,并引入其資源文件;

復(fù)制代碼 代碼如下:

Application.Resources>
        ResourceDictionary>
            ResourceDictionary.MergedDictionaries>
                ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
                ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
            /ResourceDictionary.MergedDictionaries>
        /ResourceDictionary>
    /Application.Resources>

  3、在需要顯示進(jìn)度條的頁(yè)面,加入控件(其實(shí)還是WPF控件,只是MUI擴(kuò)展了其樣式而已);

復(fù)制代碼 代碼如下:

Label Margin="280,169,0,0" Style="{StaticResource BackGroundContentText}" x:Name="lblMainState" HorizontalAlignment="Left" VerticalAlignment="Top">正在啟動(dòng):/Label>
        ProgressBar Margin="280,200,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500" Minimum="0" x:Name="ProgressControlRealValue" Maximum="1"  Value="0.1" Height="16" IsIndeterminate="False"/>
        Label Margin="280,212,0,0" Style="{StaticResource BackGroundContentText}" x:Name="lblProcess" HorizontalAlignment="Left" VerticalAlignment="Top">正在加載地圖數(shù)據(jù).../Label>
        ProgressBar Margin="280,250,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"  Minimum="0" x:Name="ProgressControl"  Width="500" Maximum="2" Height="16" IsIndeterminate="True" />

  4、后臺(tái)實(shí)現(xiàn),由于要根據(jù)情況更新進(jìn)度文字及進(jìn)度條的值。所以,這里用到了異步BackgroundWorker(具體可以網(wǎng)上查查相關(guān)資料);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Monitor.Class
{
  /// summary>
  /// 異步操作
  /// /summary>
  public class CWorker
  {
    /// summary>
    /// 對(duì)象
    /// /summary>
    private BackgroundWorker backgroundWorker;

    /// summary>
    /// 后臺(tái)執(zhí)行的操作
    /// /summary>
    public Action BackgroundWork { get; set; }

    /// summary>
    /// 后臺(tái)任務(wù)執(zhí)行完畢后事件
    /// /summary>
    public event EventHandlerBackgroundWorkerEventArgs> BackgroundWorkerCompleted;

    private BackgroundWorkerEventArgs _eventArgs;//異常參數(shù)

    /// summary>
    /// 構(gòu)造
    /// /summary>
    public CWorker()
    {
      _eventArgs = new BackgroundWorkerEventArgs();
      backgroundWorker = new BackgroundWorker();
      backgroundWorker.WorkerReportsProgress = true;
      backgroundWorker.WorkerSupportsCancellation = true;
      backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
      backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
    }

    /// summary>
    /// 開始工作
    /// /summary>
    public void BegionWork()
    {
      if (backgroundWorker.IsBusy)
        return;
      backgroundWorker.RunWorkerAsync();
    }

    /// summary>
    /// 工作
    /// /summary>
    /// param name="sender">/param>
    /// param name="e">/param>
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
      if (BackgroundWork != null)
      {
        try
        {
          BackgroundWork();
        }
        catch (Exception ex)
        {
          _eventArgs.BackGroundException = ex;
        }
      }
    }

    /// summary>
    /// 完成
    /// /summary>
    /// param name="sender">/param>
    /// param name="e">/param>
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      if (this.BackgroundWorkerCompleted != null)
      {
        this.BackgroundWorkerCompleted(null, _eventArgs);
      }
    }
  }

  /// summary>
  /// 事件
  /// /summary>
  public class BackgroundWorkerEventArgs : EventArgs
  {
    /// summary>
    /// 后臺(tái)程序運(yùn)行時(shí)拋出的異常
    /// /summary>
    public Exception BackGroundException { get; set; }
  }
}

namespace Monitor
{
  /// summary>
  /// Splash.xaml 的交互邏輯
  /// /summary>
  public partial class Splash : Window
  {
    MainWindow m_MainWindow = null;//主窗口
    CWorker m_Work = null;//任務(wù)

    public Splash()
    {
      InitializeComponent();
      m_MainWindow = new MainWindow();//創(chuàng)建主窗口對(duì)象
      m_Work = new CWorker();
      m_Work.BackgroundWork = this.ProcessDo;
      m_Work.BackgroundWorkerCompleted += new EventHandlerBackgroundWorkerEventArgs>(m_Work_BackgroundWorkerCompleted);
    }

    /// summary>
    /// 進(jìn)度提示
    /// /summary>
    public void ProcessDo()
    {
      m_MainWindow.InitData(this);
    }

    /// summary>
    /// 移動(dòng)
    /// /summary>
    /// param name="sender">/param>
    /// param name="e">/param>
    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      this.DragMove();
    }

    /// summary>
    /// 窗口加載
    /// /summary>
    /// param name="sender">/param>
    /// param name="e">/param>
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      m_Work.BegionWork();
    }

    /// summary>
    /// 執(zhí)行完成
    /// /summary>
    /// param name="sender">/param>
    /// param name="e">/param>
    void m_Work_BackgroundWorkerCompleted(object sender, BackgroundWorkerEventArgs e)
    {
      m_MainWindow.Show();
      this.Close();
    }

    /// summary>
    /// 賦值
    /// /summary>
    /// param name="text">/param>
    private delegate void SetProcessLabelDelegate(string text, double processValue);
    public void SetProcessValue(string text, double processValue)
    {
      if (!Dispatcher.CheckAccess())
      {
        Dispatcher.Invoke(DispatcherPriority.Send, new SetProcessLabelDelegate(SetProcessValue), text, processValue);
        return;
      }
      this.lblProcess.Content = text;
      this.ProgressControlRealValue.Value = processValue;
    }
  }
}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

您可能感興趣的文章:
  • WPF自定義控件和樣式之自定義按鈕(Button)
  • WPF的數(shù)據(jù)綁定詳細(xì)介紹
  • wpf將表中數(shù)據(jù)顯示到datagrid示例
  • 在WinForm和WPF中使用GMap.Net地圖插件簡(jiǎn)單教程
  • WPF彈出自定義窗口的方法
  • WPF實(shí)現(xiàn)漸變淡入淡出的登陸窗口效果
  • WPF如何自定義TabControl控件樣式示例詳解
  • C# WPF ListView控件的實(shí)例詳解
  • WPF應(yīng)用啟動(dòng)慢的問題解決

標(biāo)簽:廣西 開封 梧州 隨州 威海 西雙版納 烏蘭察布 甘孜

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266