【问题标题】:Why isn't the CommandParameter being updated?为什么没有更新 CommandParameter?
【发布时间】:2017-11-06 17:35:37
【问题描述】:

我们有一个使用 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));

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

我做错了什么?

【问题讨论】:

  • 你能告诉我们你的 ViewModel 中的命令定义吗?
  • 你能不能试着更好地描述一下什么是有效的,什么是无效的?
  • @flyte,这是父类中命令的声明: public ICommand SendOutlookEmailCommand { get;私人套装;及其在父类的构造函数中的实现:SendOutlookEmailCommand = new GalaSoft.MvvmLight.CommandWpf.RelayCommand((em) => ExecuteSendOutlookEmail(em.To, em.Subject, em.Body));
  • @Fruchtzwerg,To 和 Subject CommandParameters 工作正常。如果我为 Body 分配一个静态字符串,它也可以正常工作。
  • 不要在评论中添加代码。而是编辑您的问题并在那里添加代码。

标签: c# wpf email dependency-properties


【解决方案1】:

由于命令参数不在同一个可视化树中,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));
}

【讨论】:

  • 哇,@Fruchtzwerg,成功了。我从未听说过 Freezeable 类。我看到它是 DependencyObject 的一个孩子,所以我的其余代码工作正常。对我来说最重要的是解释为什么我试图做的事情没有奏效。非常感谢您的解释以及 Freezeable 上的博客文章链接! +1,竖起大拇指等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2021-08-04
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多