C# 使用按钮以编程方式填充功能区
为共享邮箱创建自定义“代表发送”按钮我想创建C# 使用按钮以编程方式填充功能区,c#,outlook,vsto,C#,Outlook,Vsto,为共享邮箱创建自定义“代表发送”按钮我想创建RibbonGroup和RibbonButton,并以编程方式将它们添加到RibbonTab 我的问题是,在CreateButtons()中,将按钮添加到RibbonGroup和RibbonTab将导致错误“集合为只读”。即使这些也在设计师中使用 RibbonGroupReply.Items.Add(tempButton); RibbonGroupNew.Items.Add(tempButton); this.tab_MainComplement.Gr
RibbonGroup
和RibbonButton
,并以编程方式将它们添加到RibbonTab
我的问题是,在CreateButtons()
中,将按钮添加到RibbonGroup
和RibbonTab
将导致错误“集合为只读”。即使这些也在设计师中使用
RibbonGroupReply.Items.Add(tempButton);
RibbonGroupNew.Items.Add(tempButton);
this.tab_MainComplement.Groups.Add(RibbonGroupNew);
我还尝试使用设计器中的其他方法,现在我可以添加到RibbonGroup
,但不能添加到RibbonTab
:
tab_MainComplement.SuspendLayout();
RibbonGroupReply.SuspendLayout();
this.SuspendLayout();
我认为没有出路,因为删除一个新的标签将引发相同的错误
this.Tabs.Add(新选项卡)
并在设计器中添加CreateButtons
方法InitializeComponent
会破坏布局,不会产生更好的结果
代码:
public partial class BtnSender
{
internal List<ButtonInfo> Buttons;
private void BtnSender_Load(object sender, RibbonUIEventArgs e)
{
LoadButtonsList();
CreateButtons();
}
private void CreateButtons()
{
//CreateNew Group
var buttonsNew = Buttons.Where(x => (x.Type & ButtonType.New) == 0);
if (buttonsNew.Any())
{
OutlookRibbon.RibbonGroup RibbonGroupNew = this.Factory.CreateRibbonGroup();
RibbonGroupNew.Label = "Nouveau Message";
RibbonGroupNew.Name = "Nouveau Message";
foreach (var butt in buttonsNew)
{
var tempButton = this.Factory.CreateRibbonButton();
tempButton.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
tempButton.Image = global::CustomExpeditor.Properties.Resources.basic_mail;
tempButton.Label = butt.Label;
tempButton.Name = butt.Name + butt.Label.Replace(" ", string.Empty) + "New";
tempButton.Description = butt.Address;
tempButton.ShowImage = true;
tempButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.Btn_SenderSI_Click);
RibbonGroupNew.Items.Add(tempButton);
}
this.tab_MainComplement.Groups.Add(RibbonGroupNew);
}
//CreateReply Group
var buttonsReply = Buttons.Where(x => (x.Type & ButtonType.Reply) == ButtonType.Reply);
if (buttonsReply.Any())
{
OutlookRibbon.RibbonGroup RibbonGroupReply = this.Factory.CreateRibbonGroup();
//tab_MainComplement.SuspendLayout();
//RibbonGroupReply.SuspendLayout();
//this.SuspendLayout();
RibbonGroupReply.Label = "Répondre à";
RibbonGroupReply.Name = "Répondre à";
foreach (var butt in buttonsNew)
{
var tempButton = this.Factory.CreateRibbonButton();
tempButton.ControlSize = RibbonControlSize.RibbonControlSizeLarge;
tempButton.Image = global::CustomExpeditor.Properties.Resources.basic_mail;
tempButton.Label = butt.Label;
tempButton.Name = butt.Name + butt.Label.Replace(" ", string.Empty) + "Reply";
tempButton.Description = butt.Address;
tempButton.ShowImage = true;
tempButton.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(Btn_ResponseSI_Click);
RibbonGroupReply.Items.Add(tempButton);
}
tab_MainComplement.Groups.Add(RibbonGroupReply);
}
}
private void LoadButtonsList()
{
// Will evolve to a more configurable list in the future.
Buttons = new[] {
new ButtonInfo{ Label="Mail Test", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply },
new ButtonInfo{ Label="Serv Info", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply },
new ButtonInfo{ Label="Serv Log", Address="MailTest@domain.not", Type=ButtonType.New & ButtonType.Reply },
new ButtonInfo{ Label="Titi", Address="MailTest@domain.not", Type=ButtonType.New }
}.ToList();
}
}
public class ButtonInfo
{
public string Name, Label, Address;
public ButtonType Type;
}
[Flags] public enum ButtonType { New = 1, Reply = 2 };
公共部分类BtnSender
{
内部列表按钮;
私有void BtnSender\u加载(对象发送方,RibbonUIEventArgs e)
{
LoadButtonsList();
CreateButtons();
}
私有void CreateButtons()
{
//创建新组
var buttonsNew=Buttons.Where(x=>(x.Type&ButtonType.New)==0);
if(按钮new.Any())
{
OutlookRibbon.RibbongGroup RibbongGroupNew=this.Factory.CreateRibbongGroup();
RibbonGroupNew.Label=“新消息”;
RibbonGroupNew.Name=“新消息”;
foreach(var对接按钮新建)
{
var tempButton=this.Factory.CreateRibbonButton();
tempButton.ControlSize=RibbonControlSize.RibbonControlSizeLarge;
tempButton.Image=global::CustomExpeditor.Properties.Resources.basic\u mail;
tempButton.Label=对接标签;
tempButton.Name=butt.Name+butt.Label.Replace(“,string.Empty)+“New”;
tempButton.Description=对接地址;
tempButton.ShowImage=true;
tempButton.Click+=新建Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(此.Btn\u SenderSI\u Click);
RibbonGroupNew.Items.Add(临时按钮);
}
this.tab_main complement.Groups.Add(RibbonGroupNew);
}
//CreateReply组
var buttonnsreply=Buttons.Where(x=>(x.Type&ButtonType.Reply)=ButtonType.Reply);
如果(按钮nsreply.Any())
{
OutlookRibbon.RibbongGroup RibbongGroupReply=this.Factory.CreateRibbongGroup();
//tab_main.SuspendLayout();
//RibbonGroupReply.SuspendLayout();
//这个.SuspendLayout();
RibbonGroupReply.Label=“Répondreá”;
RibbonGroupReply.Name=“Répondreá”;
foreach(var对接按钮新建)
{
var tempButton=this.Factory.CreateRibbonButton();
tempButton.ControlSize=RibbonControlSize.RibbonControlSizeLarge;
tempButton.Image=global::CustomExpeditor.Properties.Resources.basic\u mail;
tempButton.Label=对接标签;
tempButton.Name=butt.Name+butt.Label.Replace(“,string.Empty)+“Reply”;
tempButton.Description=对接地址;
tempButton.ShowImage=true;
tempButton.Click+=新建Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(Btn\u响应单击);
RibbonGroupReply.Items.Add(临时按钮);
}
tab_main complement.Groups.Add(RibbonGroupReply);
}
}
私有void LoadButtonsList()
{
//将在将来演变为更可配置的列表。
按钮=新[]{
新按钮信息{Label=“邮件测试”,地址=”MailTest@domain.not,Type=ButtonType.New&ButtonType.Reply},
新按钮信息{Label=“Serv Info”,地址=”MailTest@domain.not,Type=ButtonType.New&ButtonType.Reply},
新按钮信息{Label=“Serv Log”,地址=”MailTest@domain.not,Type=ButtonType.New&ButtonType.Reply},
新按钮信息{Label=“Titi”,地址=”MailTest@domain.not,Type=ButtonType.New}
}.ToList();
}
}
公共类按钮信息
{
公共字符串名称、标签、地址;
公共按钮类型;
}
[标志]公共枚举按钮类型{New=1,Reply=2};
这些按钮在初始化之后是只读的,就像组、选项卡等一样。在初始化之后动态添加它们不起作用
我通过在前面添加按钮,并用正确的标签动态填充按钮,多次解决了这个问题。某些控件类型确实允许动态按钮,如功能区库。看起来不仅按钮是只读的,而且还有组、选项卡和主容器。我将尽快试用ribbon。是的,所有这些都是只读的。所有这些都会生成到
Ribbon.xml
中。