首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

VSTO 3.0 實戰:打造整合 WPF 圖表與資料搜尋的 Outlook 2007 替換型表單區域

     編輯:關於.NET

專案目標

開發 Outlook 2007 增益集,以替換型表單區域 取代預設郵件表單,整合:

  • 客戶資料搜尋控制項 (Windows Forms)
  • 銷售趨勢圖表 (WPF UserControl)
  • 資料來源:Access 資料庫 (模擬 LOB 系統,實務建議用 WCF 代理)

四種表單區域類型比較

類型顯示位置適用場景
相鄰現有表單頁面底部/閱讀窗格底部補充資訊
單獨新增表單頁面獨立功能頁
替換取代第一頁所有內容,保留其他頁面自訂訊息類別,完整取代預設 UI
全部替換取代第一頁並刪除其他頁面極簡表單

建立替換型表單區域步驟

  1. VS 2008 建立 Outlook 2007 增益集專案
  2. 加入新項目 → Outlook 表單區域 → 命名 SalesData
  3. 精靈選擇:設計新表單區域 → 替換 類型
  4. 屬性設定:名稱 RecentSales、標題、描述、顯示模式
  5. 關鍵:自訂訊息類別 → IPM.Note.CustomerSalesData (繼承郵件類別)

專案結構

  • CustomOFR:Outlook 增益集主專案
  • SalesControlLibrary:Windows Forms 控制項庫 (搜尋控制項 + 資料繫結)
  • WpfChart:WPF UserControl 庫 (銷售長條圖)

資料存取層:強型別 DataSet + TableAdapter

資料結構

// SalesSearch 控制項核心查詢
private void executeSearch_Click(object sender, EventArgs e)
{
    companiesAdapter = new CompaniesTableAdapter();
    companiesBindingSource.DataSource = companiesAdapter.GetDataByCompanyAndIndustry(
        companyNameText.Text, industryText.Text); // 支援萬用字元 %
}

WPF 圖表控制項 (XAML 定義)

<UserControl x:Class="WpfChart.SalesChart" ...>
  <Grid Name="Grid1">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="80"/>
      <ColumnDefinition Width="190"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="30"/> <!-- Q1 -->
      <RowDefinition Height="30"/> <!-- Q2 -->
      <RowDefinition Height="30"/> <!-- Q3 -->
      <RowDefinition Height="30"/> <!-- Q4 -->
      <RowDefinition Height="30"/> <!-- Title -->
    </Grid.RowDefinitions>

    <TextBlock Name="Category1" Text="X1" Grid.Column="0" Grid.Row="0" .../>
    <Rectangle Name="Bar1" Fill="LightSteelBlue" RadiusX="5" RadiusY="5"
               Grid.Column="1" Grid.Row="0" Width="170" Height="18"/>
    <TextBlock Name="Label1" Text="$Y1" Grid.Column="1" Grid.Row="0" .../>
    <!-- Q2~Q4 同樣結構 -->
    <TextBlock Name="Title" FontSize="14" FontWeight="Bold" Grid.Column="1" Grid.Row="4"/>
  </Grid>
</UserControl>

圖表更新邏輯 (Code-behind)

public void UpdateWPFChart(string chartTitle, string[] xVals, double[] yVals)
{
    double maxY = yVals.Max();
    for (int i = 0; i < 4; i++)
    {
        if (i >= xVals.Length || i >= yVals.Length) break;

        ((TextBlock)FindName($"Category{i+1}")).Text = xVals[i];
        ((TextBlock)FindName($"Label{i+1}")).Text = yVals[i].ToString();
        var bar = (Rectangle)FindName($"Bar{i+1}");
        bar.Width = (yVals[i] / maxY) * Grid1.ColumnDefinitions[1].Width.Value;
    }
    Title.Text = chartTitle;
}

表單區域整合程式碼

partial class RecentSales
{
    string[] currentXData;
    double[] currentYData;
    string currentCompany;

    private void RecentSales_FormRegionShowing(object sender, EventArgs e)
    {
        salesChart1.Visibility = System.Windows.Visibility.Hidden; // 預設隱藏
    }

    private void UpdateChart()
    {
        if (showChart.Checked && currentXData != null && currentYData != null)
        {
            salesChart1.UpdateWPFChart(currentCompany, currentXData, currentYData);
            salesChart1.Visibility = System.Windows.Visibility.Visible;
        }
        else
        {
            salesChart1.Visibility = System.Windows.Visibility.Hidden;
        }
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e) => UpdateChart();

    private void salesSearch1_SalesDataChangeEvent(object sender, SalesSearchEventArgs e)
    {
        currentCompany = e.CompanyName;
        currentXData = e.XValues;
        currentYData = e.YValues;
        UpdateChart();
    }
}

執行效果

設計階段

執行階段:搜尋客戶、顯示 WPF 圖表

實務部署建議

  • 生產環境資料存取**必須透過 WCF 服務代理**,隔離資料庫連線與安全性
  • ClickOnce 部署增益集,支援自動更新
  • 登錄表設定 LoadBehavior=3 確保啟動載入
  • 表單區域 .ofs 檔案隨增益集自動註冊,無需手動安裝
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved