代碼: public override Ribbon Ribbon
{
get
{
if (Component is RibbonButton button)
{
return button.Owner;
}
return null;
}
}
中 這 句 : if (Component is RibbonButton button) 后面的button 是什么意思 不懂 還請 各位大蝦 幫助一下 .
說明一下 ,這些代碼是國外的一個ribbon界面開源 代碼.
全部代碼如下:
internal class RibbonButtonDesigner
: RibbonElementWithItemCollectionDesigner
{
public override Ribbon Ribbon
{
get
{
if (Component is RibbonButton button)
{
return button.Owner;
}
return null;
}
}
public override RibbonItemCollection Collection
{
get
{
if (Component is RibbonButton button)
{
return button.DropDownItems;
}
return null;
}
}
}
好多地方是這樣
namespace System.Windows.Forms
{
internal class RibbonButtonListDesigner
: RibbonElementWithItemCollectionDesigner
{
public override Ribbon Ribbon
{
get
{
if (Component is RibbonButtonList list) //這里 的 list
{
return list.Owner;
}
return null;
}
}
public override RibbonItemCollection Collection
{
get
{
if (Component is RibbonButtonList list)
{
return list.Buttons;
}
return null;
}
}
}
}
其他不懂 地方
private bool? _isopeninvisualstudiodesigner; 這里的?號 是什么意思,用法???
public Rectangle ScrollBarBounds => Rectangle.FromLTRB(ButtonUpBounds.Left, ButtonUpBounds.Top, ButtonDownBounds.Right, ButtonDownBounds.Bottom); \\像 =>這個又是什么鬼?????????
public RibbonMouseSensor(Control control, Ribbon ribbon)
: this()
{
Control = control ?? throw new ArgumentNullException(nameof(control));
Ribbon = ribbon ?? throw new ArgumentNullException(nameof(ribbon));
AddHandlers();
}
這 兩句
Control = control ?? throw new ArgumentNullException(nameof(control));
Ribbon = ribbon ?? throw new ArgumentNullException(nameof(ribbon));
?? 是什么意思,用法???
uj5u.com熱心網友回復:
從c# 7.0開始,is運算子支持型別模式(Type Pattern)。“使用型別模式執行模式匹配時,is 會測驗運算式是否可轉換為指定型別,如果可以,則將其轉換為該型別的一個變數”(https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/is)。以前版本的型別測驗就比較麻煩。比如:
// 舊版本:
if (Component is RibbonButton) // 測驗
{
RibbonButton button = (RibbonButton)Component; // 型別轉換
return button.Owner;
}
return null;
// c# 7.0版本:
if (Component is RibbonButton button) // 測驗,如果可以并轉換
{
return button.Owner;
}
return null;
建議你看看C#的新功能,比如bool?為可空數值型別,?? throw為throw運算式等等。
它們可有幫助增加編碼效率,是代碼更少但表達能力更強。
c# 7.0版本后還有7.1,7.2,7.3版,目前C#的最新版本已經是C# 8了。
uj5u.com熱心網友回復:
if (Component is RibbonButton button)相當于
if (Component is RibbonButton)
button = Component as RibbonButton;
uj5u.com熱心網友回復:
非常感謝 上面兩位 兄弟 幫助
還有些地方不明白,還請 再 幫助一下 :
public void AddTabVerb(object sender, EventArgs e)
{
if (Control is Ribbon r)
{
IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost;
RibbonTab tab = host?.CreateComponent(typeof(RibbonTab)) as RibbonTab; //這里?.怎么理解
if (tab == null) return;
tab.Text = tab.Site.Name;
Ribbon.Tabs.Add(tab);
r.Refresh();
}
}
/// <summary>
/// Gets the Ribbon of the designer
/// </summary>
public Ribbon Ribbon => Control as Ribbon; //這里=>怎么理解。
是 Expression-bodied 成員(C# 編程指南) 這里的知識嗎?
以上所有問題 在VS2015中 被當作錯誤處理。。
uj5u.com熱心網友回復:
搜索“C#新增功能”:
https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7
比如“=>”是C# 6.0的新增功能(2015年,已經5年了)
另外,安裝一個VisualStudio 2019社區版,免費的,且不會洗掉已經安裝的VS2015版。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/21164.html
標籤:C#
