我已经玩WPF了一段时间,我遇到了一件有趣的事情.当我将DateTime对象绑定到Label内容时,我会看到日期的本地格式化表示.但是,当我绑定到TextBlock的Text属性时,我实际上看到了一个英文属性.
似乎TextBlock是使用某种转换器,而Label只是调用ToString方法,但我不确定.
如果是这样,为什么不Label使用转换器呢?
有人可以向我解释为什么它按照它的方式工作?我提供了一个简短的样本,让你们检查发生了什么:
// MainWindow.xaml
<Window x:Class="BindConversion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel HorizontalAlignment="Center" Margin="3">
<StackPanel>
<Label Content="{Binding Dt}"/>
<TextBlock Text="{Binding Dt}"/>
</StackPanel>
</StackPanel>
</Window>
// MainWindow.xaml.cs
using System;
using System.Windows;
namespace BindConversion
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public DateTime Dt { get; set; }
public MainWindow()
{ …Run Code Online (Sandbox Code Playgroud) 序列的签名是
sequence :: Monad m => t (m a) -> m (t a)
Run Code Online (Sandbox Code Playgroud)
但是我们可以实现为
sequence = traverse id
Run Code Online (Sandbox Code Playgroud)
要求m公正Applicative。如果monad是applicatives,那么为什么还要在类型级别上有这个约束呢?
我想要一个将任何可迭代类型转换为C[_]的Either[A, B]函数Either[C[A], C[B]]。
我让它工作了,但我使用了asInstanceOf方法,我觉得这种方法在某些情况下可能会失败(我还不知道那会是什么场景,因为我不太了解CanBuildFrom解决)。
我认为我应该使用自定义来实现它CanBuildFrom,但我希望有更简单的方法来做到这一点。
这是我的方法:
type IterableCollection[A[_], B] = A[B] with Iterable[B]
implicit class IterableEither[C[_], A, B](self: IterableCollection[C, Either[A, B]]) {
def accumulate: Either[IterableCollection[C, A], IterableCollection[C, B]] = {
val failures = self.collect { case x @ Left(_) => x.value }.asInstanceOf[IterableCollection[C, A]]
if (failures.nonEmpty) Left(failures)
else Right(self.collect { case x @ Right(_) => x.value }.asInstanceOf[IterableCollection[C, B]])
}
}
Run Code Online (Sandbox Code Playgroud)
我已经用 Scala 编程有一段时间了,但从未依赖过asInstanceOf,因此我有点害怕将这种代码引入生产环境。你们有没有想到一种无需演员就能做到这一点的方法?
我们来看看以下代码:
class C
{
static void Main(string[] args)
{
Task t = new Task(DoSthAsync);
t.Start();
t.Wait();
Console.WriteLine("Finished?");
}
static async void DoSthAsync()
{
using (StreamReader reader = new StreamReader("file.txt"))
{
int i = 1;
while (!reader.EndOfStream)
{
Console.WriteLine("{0}: {1}", i++, await reader.ReadLineAsync());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我真的很困惑.这个代码不应该同步工作,因为我直接等待异步方法吗?从1000行文件中我只打印了76行.这是为什么?
尝试执行此代码时,VS2015会抛出很多错误:
int a = 5;
int *p = &a;
std::vector<decltype(*p)> v;
Run Code Online (Sandbox Code Playgroud)
但是,当我检查这个decltype返回的类型时,我得到一个int!
typeid(decltype(*p)) == typeid(int) // returns true
Run Code Online (Sandbox Code Playgroud)
有谁可以向我解释一下?我通过简单地解除引用指针和decltyping我得到的值来完成解决方法.但是为什么不能通过直接取消引用指针来实现呢?
在进行我的项目期间,我遇到了一个奇怪的问题。当我在应用程序的ToolStrip中对ToolStripDropDownButton进行右对齐时,它会出现在窗口之外。甚至全屏显示在我的第二台显示器上。我以为我担心窗口是否正确对齐菜单,但似乎我错了。我使用Windows窗体。
这就是我在说的。这是一个简短的生成类似问题。
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of …Run Code Online (Sandbox Code Playgroud) 我必须调用两个API来获取一些值.然后我能够将结果呈现给用户.我通过回调进行顺序调用实现了这一点:
function firstApi() {
callFirstApi(function(firstValue) {
secondApi(firstValue);
});
}
function secondApi(firstValue) {
callSecondApi(function(secondValue) {
presentResults(firstValue, secondValue);
});
}
function presentResults(firstValue, secondValue) {
// do something...
}
Run Code Online (Sandbox Code Playgroud)
困扰我的问题是API调用可能是异步的.我想知道这种解决方案是否有任何问题:
var firstValue = null;
var secondValue = null;
function apiResult(value) {
if (firstValue === null) {
firstValue = value;
return;
}
secondValue = value;
presentResults();
}
function presentResults() {
// do something...
}
firstApiCall(apiResult);
secondApiCall(apiResult);
Run Code Online (Sandbox Code Playgroud)
JavaScript是单线程的,但我仍然不确定上下文切换可能发生的位置.换句话说,如果异步调用完成时有可能在执行过程中中断函数调用(因此,例如,firstValue对于两个执行路径都将传递空检查,并且永远不会设置第二个值).
c# ×3
asynchronous ×1
c++ ×1
decltype ×1
either ×1
haskell ×1
javascript ×1
scala ×1
winforms ×1
wpf ×1