为什么CommandParameter没有更新?

时间:2017-11-06 17:35:37

标签: c# wpf email dependency-properties

我们使用VS 2015编写了一个WPF应用程序。该应用程序在整个应用程序的几个位置都有一个错误图标,用户可以使用它来报告错误。它会调出他们的电子邮件客户端,以便向我们的帮助票务系统发送电子邮件。 (这很好。)我写了一个DLL,我打算用其他需要做同样事情的WPF应用程序,只使用MS Outlook,指定帮助票据电子邮件地址收件人,主题行,最后一些文本电子邮件的正文。电子邮件正文的文本意味着要更改,具体取决于用户在应用程序中的位置。这部分是不是正常工作,我不知道为什么不是。这是DLL中的方法:

    /// <summary>
    /// This will construct the basics of an email message. Then it will bring up Outlook for the user
    /// to add anything more they want to add, then send the email.
    /// 
    /// This uses version 15 of Microsoft.Office.Interop.Outlook.
    /// 
    /// This will raise an exception if anything goes wrong.
    /// </summary>
    /// <param name="To">List of email address(es)</param>
    /// <param name="Subject">The Subject line of the email message</param>
    /// <param name="Body">The body of the email message</param>
    public static void SendMail(string To, string Subject, string Body)
    {
        try
        {
            var OutlookApp = new Application();
            var mail = OutlookApp.CreateItem(OlItemType.olMailItem) as MailItem;
            mail.To = To;
            mail.Subject = Subject;
            mail.Body = Body;

            foreach (Recipient recipient in mail.Recipients)
            {
                recipient.Resolve();
            }

            mail.Display(); //bring up Outlook; let the user add whatever they want then send it
        }
        catch (System.Exception ex)
        {
            if (ex.InnerException != null)
            {
                throw new System.Exception(ex.Message, ex.InnerException);
            }
            else
            {
                throw new System.Exception(ex.Message);
            }
        }
    }

这是我尝试涉及的XAML:

<MenuItem Background="{x:Null}" Foreground="#FF706C6C" Command="{Binding SendOutlookEmailCommand}">
<MenuItem.ToolTip>
    <ToolTip Style="{StaticResource MenuItemToolTip}" />
</MenuItem.ToolTip>
<MenuItem.CommandParameter>
    <model:Email To="CreateHD.Ticket@mycompany.com" Subject="BUG:ACDC" Body="{Binding BugTabInfo}" />
</MenuItem.CommandParameter>
<MenuItem.Header>
    <Path Data="{StaticResource BugIconData}"
          Stretch="Uniform"
          Fill="#77000000"
          Width="20"
          RenderTransformOrigin="0.25,0.25"
          Height="20" />
</MenuItem.Header>

以下是Email类的Body属性:

public string Body
{
    get { return (string)GetValue(BodyProperty); }
    set { SetValue(BodyProperty, value); }
}
public static readonly DependencyProperty BodyProperty =
    DependencyProperty.Register("Body", typeof(string), typeof(Email));

最后,这是viewmodel中的BugTabInfo属性:

private string bugTabInfo;
public override string BugTabInfo
{
    get
    {
        //Debug.WriteLine($"BugTabInfo: {bugTabInfo}");
        return bugTabInfo;
    }
    set
    {
        bugTabInfo = string.Format("Bug on {1} {0}", value, CurrentTableName);
        RaisePropertyChanged("BugTabInfo");
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:3)

由于命令参数不在同一个可视树中Email需要Freezable才能正确更新绑定的主体。此行为的描述为in this post

只需对代码进行一些小改动,即可在执行命令时获得正确的Body

public class Email : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new Email();
    }
    public string Body
    {
        get { return (string)GetValue(BodyProperty); }
        set { SetValue(BodyProperty, value); }
    }
    public static readonly DependencyProperty BodyProperty =
        DependencyProperty.Register("Body", typeof(string), typeof(Email));
}