我看过很酷的C64演示,在屏幕的边框区域显示精灵.这不应该是可能的; 我认为他们设法以某种方式欺骗了图形芯片.他们究竟是怎么做到的?
编译这个简单的程序:
class Program
{
static void Foo( Action bar )
{
bar();
}
static void Main( string[] args )
{
Foo( () => Console.WriteLine( "42" ) );
}
}
Run Code Online (Sandbox Code Playgroud)
没什么奇怪的.如果我们在lambda函数体中出错:
Foo( () => Console.LineWrite( "42" ) );
Run Code Online (Sandbox Code Playgroud)
编译器返回一条错误消息:
error CS0117: 'System.Console' does not contain a definition for 'LineWrite'
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在,让我们在调用中使用命名参数Foo:
Foo( bar: () => Console.LineWrite( "42" ) );
Run Code Online (Sandbox Code Playgroud)
这一次,编译器消息有点令人困惑:
error CS1502: The best overloaded method match for
'CA.Program.Foo(System.Action)' has some invalid arguments
error CS1503: Argument 1: cannot convert from …Run Code Online (Sandbox Code Playgroud) 所以写作:
if (obj.Collection == null)
obj.Collection = new Collection();
obj.Collection.Add(something);
Run Code Online (Sandbox Code Playgroud)
我想写作:
obj.Collection = obj.Collection ?? new Collection;
obj.Collection.Add(something);
Run Code Online (Sandbox Code Playgroud)
这种感觉有点不对,尤其是这部分"obj.Collection = obj.Collection ......"
你们有什么感想 ?
问候,
如果我有这样的位置记录:
public record Version(int Major, int Minor);
Run Code Online (Sandbox Code Playgroud)
看来我只能提供一个<summary>文档,像这样:
/// <summary>
/// Version record
/// </summary>
public record Version(int Major, int Minor);
Run Code Online (Sandbox Code Playgroud)
是否可以在保持简短记录语法的同时记录Major和Minor属性(也可能是构造函数参数)?
我有一个问题,DataContextChanged没有在我的自定义Panel控件的逻辑子上引发.我把它缩小到这个:
从向导生成的WPF应用程序开始,我添加:
private void Window_Loaded( object sender, RoutedEventArgs e )
{
var elt = new FrameworkElement();
this.AddLogicalChild( elt );
DataContext = 42;
Debug.Assert( (int)elt.DataContext == 42 );
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这是有效的,因为DataContext是一个可继承的依赖属性.
现在,我在Window(this)及其逻辑子代上为DataContextChanged添加事件处理程序:
this.DataContextChanged +=
delegate { Debug.WriteLine( "this:DataContextChanged" ); };
elt.DataContextChanged +=
delegate { Debug.WriteLine( "elt:DataContextChanged" ); };
Run Code Online (Sandbox Code Playgroud)
如果我运行它,只会执行第一个事件处理程序.为什么是这样?如果不是AddLogicalChild(elt),我会执行以下操作:
this.Content = elt;
Run Code Online (Sandbox Code Playgroud)
两个处理程序都将执行.但是在我的情况下这不是一个选项 - 我正在向我的控件中添加FrameworkContentElements,它们不应该是可视化的孩子.
这里发生了什么?除了AddLogicalChild()之外,我还应该做些什么来使它工作吗?
(幸运的是,有一个相当简单的解决方法 - 只需将元素的DataContext绑定到窗口的DataContext)
BindingOperations.SetBinding( elt, FrameworkElement.DataContextProperty,
new Binding( "DataContext" ) { Source = this …Run Code Online (Sandbox Code Playgroud) c# ×3
border ×1
c#-9.0 ×1
c64 ×1
coalesce ×1
datacontext ×1
lambda ×1
logical-tree ×1
record ×1
sprite ×1
wpf ×1
xml-comments ×1