Win8 发送邮件---Html 格式

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 添加引用
using Windows.ApplicationModel.DataTransfer;        // DataTransferManager,通过其他应用程序以编程方式启动内容的交换。  
using BPCompanion.Readings;
using BPCompanion.DataModel;
using Windows.UI.Popups;

namespace BPCompanion.Exports
{
    /// <summary>
    /// 基本页,提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class Export : BPCompanion.Common.LayoutAwarePage
    {
        DataTransferManager dtm;            //     通过其他应用程序以编程方式启动内容的交换。
        bool is_enable = false;
        DateTime startdate;
        DateTime enddate;
        List<tb_Profile> profileArray;
        tb_Profile select_Profile;
        tb_Setting setting;
        string dateformat = "MMM dd, yyyy";        // 日期输出格式
        string html_body = "zxc";
        int back_type = -1;
        DateTime reading_detail_date = DateTime.Now;

        public Export()
        {
            this.InitializeComponent();

            DateTime now = DateTime.Now;
            
            enddate = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
            now = now.AddMonths(-1);
            startdate = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
            profileArray = new List<tb_Profile>();
            select_Profile = ProfilesManager.getProfileIsSelected();
            setting = SettingManager.getSetting();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            object obj = e.Parameter;
            if (obj.GetType() == typeof(DateTime))
            {
                back_type = -2;
                reading_detail_date = (DateTime)obj;
            }
            else
            {
                back_type = -1;
            }

            UnitControl.Unit_ChangedHandlerEvent += Unit_Changed;
            Windows.UI.ViewManagement.ApplicationView.TryUnsnap();          // 尝试展开以前已对齐的应用程序。
            this.startpicker.Date_ChangedHandlerEvent += Start_Changed;
            this.endpicker.Date_ChangedHandlerEvent += End_Changed;

            dtm = DataTransferManager.GetForCurrentView();      //     返回与当前窗口关联的 DataTransferManager 对象。  
            dtm.DataRequested += dtm_DataRequested;             //     在共享操作开始时发生。  
            is_enable = false;

            Init_Data();
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dtm.DataRequested -= dtm_DataRequested;             //     在页面跳转走时发生。  
            this.startpicker.Date_ChangedHandlerEvent -= Start_Changed;
            this.endpicker.Date_ChangedHandlerEvent -= End_Changed;
            UnitControl.Unit_ChangedHandlerEvent -= Unit_Changed;
        }

        // Unit发生变化时调用
        private void Unit_Changed(int unit)
        {
            if (setting.unit != unit)
            {
                setting.unit = unit;
            }
        }

        public void Init_Data()
        {
            #region 绑定Profiles数据
            List<tb_Profile> items = ProfilesManager.getProfiles();
            
            foreach (var item in items)
            {
                profileArray.Add(item);
            }

            int index = 0;
            foreach (var item in profileArray)
            {
                if (select_Profile.id != item.id)
                {
                    index++;
                }
                else
                {
                    break;
                }
            }

            this.profile.ItemsSource = profileArray;

            if (profileArray.Count > 0)
            {
                this.profile.SelectedIndex = index;
            }
            #endregion

            this.startpicker.setDate(startdate);
            this.endpicker.setDate(enddate);
        }

        private void End_Changed(DateTime date)
        {
            enddate = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
        }

        private void Start_Changed(DateTime date)
        {
            startdate = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
        }

        // 发送邮件 时  准备数据
        private void dtm_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            // 共享带格式的Content/ HTML
            if (is_enable)
            {
                string HTMLSource = html_body;
                string HTMLTitle = "BP Data of " + select_Profile.name + " : ";
                HTMLTitle += startdate.ToString(dateformat) + "-" + enddate.ToString(dateformat);
                //string HTMLDescription = "Data from BP Compainion.";

                DataPackage data = args.Request.Data;
                data.Properties.Title = HTMLTitle;
                //data.Properties.Description = HTMLDescription;
                data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(HTMLSource));
                is_enable = false;
            }
        }

        // 退后按钮
        private void Go_Back(object sender, RoutedEventArgs e)
        {
            // 使用导航框架返回上一页
            if (-1 == back_type)
            {
                this.Frame.Navigate(typeof(Reading), -1);
            }
            else
            {
                this.Frame.Navigate(typeof(ReadingsDetail), reading_detail_date);
            }
        }

        private async void share_OnClick(object sender, RoutedEventArgs e)
        {
            if (startdate.CompareTo(enddate) > 0)
            {
                MessageDialog msg = new MessageDialog("Start date should be early than end date.", "Note:");
                msg.Commands.Add(new UICommand("OK"));

                await msg.ShowAsync();
                return;
            }

            // 准备数据
            var profile = ProfilesManager.getProfileById(select_Profile.id);
            if (profile == null)
            {
                return;
            }

            string unit = "";
			if (0 == setting.unit)
			{
				unit = @"(lbs)";
			}
			else 
			{
				unit = @"(kg)";
			}

            string map = "";
            string weight = "";
            string note = "";
            if (true == this.switch_map.IsOn)
            {
                map = @"<td width='75' style='border:solid 1px #b0b0b0'><font size=2>MAP (mmHg)</font></td>";
            }
            if (true == this.switch_weight.IsOn)
            {
                weight = @"<td width='75' style='border:solid 1px #b0b0b0'><font size=2>Weight " + unit + "</font></td>";
            }
            if (true == this.switch_note.IsOn)
            {
                note = @"<td width='150' style='border:solid 1px #b0b0b0'><font size=2>Note</font></td>";
            }

            string mailTop = @"<html><body><div><table cellpadding ='0' cellspacing='0' style='border:solid 0px #b0b0b0;text-align:center;border-collapse:collapse;width:480px'><tr style='background-color:#5da7b9;'><td width='75' style='border:solid 1px #b0b0b0'><font size=2>Date Time</font></td><td width='20' style='border:solid 1px #b0b0b0'><font size=2>Systolic (mmHg)</font></td><td width='75' style='border:solid 1px #b0b0b0'><font size=2>Diastolic (mmHg)</font></td>" + map + "<td width='75' style='border:solid 1px #b0b0b0'><font size=2>Heart Rate (bpm)</font></td>" + weight + note + "</tr>";

            string mailContent = "";
            List<tb_History> histories = HistoryManager.getHistoriesByPrifileId(profile.id);
            if (histories.Count > 0)
            {
                for (int i = 0; i < histories.Count; i++)
                {
                    tb_History history = histories[i];
                    if (history.createtime.CompareTo(startdate) < 0 || history.createtime.CompareTo(enddate) > 0)
                    {
                        continue;       // 跳出本次循环,继续下一次循环
                    }

                    string contentStr = "";
                    string tmp_date = history.createtime.ToString("d/M/yy h:mm t") + "M";
                    string tmp_systolic = history.systolic.ToString();
                    string tmp_diastolic = history.diastolic.ToString();
                    string tmp_heartrate = history.heartrate.ToString();

                    string tmp_map = @"<td style='border:solid 1px #b0b0b0'><font size=2>" + history.map.ToString(".0") + "</font></td>";
                    string tmp_weight = "";

                    if (0 == setting.unit)
                    {
                        tmp_weight = @"<td style='border:solid 1px #b0b0b0'><font size=2>" + history.weight.ToString(".0") + " </font></td>";
                    }
                    else
                    {
                        tmp_weight = @"<td style='border:solid 1px #b0b0b0'><font size=2>" + (history.weight * 0.4532f).ToString(".0") + " </font></td>";
                    }
                    string tmp_note = @"<td style='border:solid 1px #b0b0b0'><font size=2>" + history.note_text + "</font></td>";

                    if (history.note_text.Equals("Add note here."))
                    {
                        tmp_note = @"<td style='border:solid 1px #b0b0b0'><font size=2>" + "" + "</font></td>";
                    }

                    if (false == this.switch_map.IsOn)
                    {
                        tmp_map = "";
                    }
                    if (false == this.switch_weight.IsOn)
                    {
                        tmp_weight = "";
                    }
                    if (false == this.switch_note.IsOn)
                    {
                        tmp_note = "";
                    }

                    if (0 == i % 2)
                    {
                        contentStr = @"<tr height='30'><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_date + "</font></td><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_systolic + "</font></td><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_diastolic + "</font></td>" + tmp_map + "<td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_heartrate + "</font></td>" + tmp_weight + tmp_note + "</tr>";
                    }
                    else
                    {
                        contentStr = @"<tr height='30' style='background-color:#b5d7df'><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_date + "</font></td><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_systolic + "</font></td><td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_diastolic + "</font></td>" + tmp_map + "<td style='border:solid 1px #b0b0b0'><font size=2>" + tmp_heartrate + "</font></td>" + tmp_weight + tmp_note + "</tr>";
                    }

                    mailContent += contentStr;
                }

            }
            html_body = mailTop + mailContent + @"</table></div><br><br><br>The Data is created with Blood Pressure Companion.</body></html>" ;

            is_enable = true;
            try
            {
                DataTransferManager.ShowShareUI();    // 手动打开共享
            }
            catch (Exception)
            {
            }
        }

        private void profile_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                select_Profile = (tb_Profile)e.AddedItems[0];
            }
        }


    }
}

猜你喜欢

转载自blog.csdn.net/zhibudefeng/article/details/9171657
今日推荐