c# - 为什么不更新 CommandParameter?

标签 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));

最后是 View 模型中的 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");
    }
}

我做错了什么?

最佳答案

由于命令参数不在同一个可视化树中,Email 需要 Freezable 才能正确更新绑定(bind)主体。此行为描述为 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));
}

关于c# - 为什么不更新 CommandParameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47142634/

相关文章:

c# - 从数据库反序列化二进制数组 C#

amazon-web-services - 如何创建通过 HTTP 发送 Amazon SES 电子邮件所需的 HMAC 签名?

javascript - 使用多个地址填充电子邮件

c# - 用于检测 ISO 语言代码的正则表达式

c# - Xaml - 字体很棒的微调器隐藏/显示不起作用

.net - 如何在未输入任何内容时禁止验证

wpf - x :Name in xaml (WPF) and its use with Storyboard 是什么意思

c# - 如何检测是否按下了任何键

api - 使用 Aweber API 从网站创建列表

c# - 如何将 Entity Framework ICollection 更改为 ObservableCollection?