知乎周源微信_每周源代码12-黑色版

知乎周源微信

知乎周源微信

It's been a while since the last Weekly Source Code, but I have the holidays and the preponderance of baby poop as a fine formal excuse. I'm back from Paternity now and the Source Code will keep coming weekly as originally promised.

自上周的每周源代码以来已经有一段时间了,但是我有节假日​​和大量的婴儿便便作为正式的借口。 我现在已经从Paternity回来了,源代码将按照最初的承诺每周继续发布。

If you're new to this, each week I post some snippets of particularly interesting (read: beautiful, ugly, clever, obscene) source and the project it came from. This started from a belief that reading source is as important (or more so) as writing it. We read computer books to become better programmers, but unless you're reading books like Programming Pearls, you ought to peruse some Open Source projects for inspiration.

如果您是新手,我每周都会发布一些片段,这些片段特别有趣(阅读:美丽,丑陋,聪明,淫秽)和其来源。 这源于一种信念,即阅读源与编写源同样重要(甚至更重要) 。 我们阅读计算机书籍以成为更好的程序员,但是除非您阅读《 Programming Pearls》之类的书,否则您应该细读一些开放源代码项目以获取灵感。

And so, Dear Reader, I present to you the twelfth in a infinite number of posts of "The Weekly Source Code." Here's some source I was reading while changing diapers.

因此,尊敬的读者,我在“每周源代码”的无数帖子中向您介绍第十二个。 这是我在换尿布时正在阅读的一些资料。

  • Looking for applications that use WPF? They are out there and they don't always have "juicy buttons" and scream "I was written in WPF." Sometimes they are just clean, simple and functional. Witty is a Twitter (chat-esque) client written in WPF, a technology I have yet to fully grok.

    寻找使用WPF的应用程序吗? 他们在那里,他们并不总是有“多汁的按钮”和尖叫声“我是用WPF编写的”。 有时它们只是干净,简单和实用的。 Witty是用WPF编写的Twitter (聊天)客户端,我尚未完全了解过该技术。

    This little app has a simple Twitter Client Library that includes inside it a

    这个小应用程序有一个简单的Twitter客户端库,其中包含一个

    TinyUrl maker. (Google better buy TinyUrl or the whole web might get link rot) Sometimes it's fun to read source for the comments, but it's also cathartic to read code that you didn't write and say to yourself, "hey, this could use some refactoring." Perhaps a good time for you to offer to help an Open Source Project.

    TinyUrl制造商。 (Google最好购买TinyUrl,否则整个网络可能会丢失链接)有时,阅读注释源很有趣,但阅读未编写的代码并对自己说:“嘿,这可能需要一些重构。” 也许是您提供帮助开源项目的好时机。

    This example is the most trivial chunk of code in Witty, but somehow it made me smile. Regardless, the MainWindow of Witty also had some interesting stuff, particularly the background fetching of data. It's definitely easier than WinForms.

    这个例子是Witty中最琐碎的代码块,但是以某种方式让我笑了。 无论如何,Witty的MainWindow也有一些有趣的东西,特别是数据的后台获取。 绝对比WinForms容易。

    public void UpdateRelativeTime()
    {
         DateTime StatusCreatedDate = (DateTime)dateCreated;
    
        TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - StatusCreatedDate.Ticks);
        double delta = ts.TotalSeconds;
    
        string relativeTime = string.Empty;
    
        if (delta == 1)
        {
            relativeTime = "a second ago";
        }
        else if (delta < 60)
        {
            relativeTime = ts.Seconds + " seconds ago";
        }
        else if (delta < 120)
        {
            relativeTime = "about a minute ago";
        }
        else if (delta < (45 * 60))
        {
            relativeTime = ts.Minutes + " minutes ago";
        }
        else if (delta < (90 * 60))
        {
            relativeTime = "about an hour ago";
        }
        else if (delta < (24 * 60 * 60))
        {
            relativeTime = "about " + ts.Hours + " hours ago";
        }
        else if (delta < (48 * 60 * 60))
        {
            relativeTime = "1 day ago";
        }
        else
        {
            relativeTime = ts.Days + " days ago";
        }
    
        RelativeTime = relativeTime;
    }
      
  • Folks are poking at C# 3.0 trying to get lambdas and anonymous type declarations to feel and act like Ruby hash table declarations. It'll be interesting to see if Anders is already on top of this. I wonder if he and Matz hang out? If I invented a language, I'd make a really exclusive clubhouse. ;)

    人们嘲笑C#3.0,试图让lambda和匿名类型声明像Ruby哈希表声明一样发挥作用。 看看Anders是否已处于此之上将会很有趣。 我想知道他和Matz是否出去吗? 如果发明一种语言,我会成为一个真正的俱乐部。 ;)

    Folks are poking at C# 3.0 trying to get lambdas and anonymous type declarations to feel and act like Ruby hash table declarations. It'll be interesting to see if Anders is already on top of this. I wonder if he and Matz hang out? If I invented a language, I'd make a really exclusive clubhouse. ;) Eilon shows the difference between Dictionaries and anonymous types in his proposal:

    人们嘲笑C#3.0,试图让lambda和匿名类型声明像Ruby哈希表声明一样发挥作用。 看看Anders是否已处于此之上将会很有趣。 我想知道他和Matz是否一起吗? 如果发明一种语言,我会成为一个真正的俱乐部。 ;) Eilon在他的提案中展示了Dictionary和匿名类型之间的区别

    Folks are poking at C# 3.0 trying to get lambdas and anonymous type declarations to feel and act like Ruby hash table declarations. It'll be interesting to see if Anders is already on top of this. I wonder if he and Matz hang out? If I invented a language, I'd make a really exclusive clubhouse. ;) Eilon shows the difference between Dictionaries and anonymous types in his proposal:

    人们嘲笑C#3.0,试图让lambda和匿名类型声明像Ruby哈希表声明一样发挥作用。 看看Anders是否已处于此之上将会很有趣。 我想知道他和Matz是否出去吗? 如果发明一种语言,我会成为一个真正的俱乐部。 ;) Eilon在他的提案中展示了Dictionary和匿名类型之间的区别

    • This is some ugly code:

      这是一些难看的代码:

    Folks are poking at C# 3.0 trying to get lambdas and anonymous type declarations to feel and act like Ruby hash table declarations. It'll be interesting to see if Anders is already on top of this. I wonder if he and Matz hang out? If I invented a language, I'd make a really exclusive clubhouse. ;) Eilon shows the difference between Dictionaries and anonymous types in his proposal:

    人们嘲笑C#3.0,试图让lambda和匿名类型声明像Ruby哈希表声明一样发挥作用。 看看Anders是否已处于此之上将会很有趣。 我想知道他和Matz是否出去吗? 如果发明一种语言,我会成为一个真正的俱乐部。 ;) Eilon在他的提案中展示了Dictionary和匿名类型之间的区别

    Dictionary<string, string> values = new Dictionary<string, string>();
    values.Add("key1", "value1");
    values.Add("key2", "value2");
    values.Add("key3", "value3");
    GetHtmlLink("Click me", values);
     Dictionary<string, string> values = new Dictionary<string, string>();
    values.Add("key1", "value1");
    values.Add("key2", "value2");
    values.Add("key3", "value3");
    GetHtmlLink("Click me", values);

    My proposal: Have the method accept a parameter of type object. Callers could pass in a type that has properties with the appropriate names and values. They can use C#'s object initializer syntax to save some space:

    我的建议:让该方法接受类型为object的参数。 调用者可以传入具有适当名称和值的属性的类型。 他们可以使用C#的对象初始值设定项语法节省一些空间:

    MyParams myParams = new MyParams { Key1 = "value1", Key2 = "value2", Key3 = "value3" };
    GetHtmlLink("Click me", myParams);

    However, there was the added work of defining the MyParams type. Admittedly, it wasn't that hard with C# 3.0's automatic properties, but I hate defining types that are used in only one place. If the user can pass in an arbitrary object with properties, why not let that object be of an anonymous type? Here's the final code:

    但是,还有其他工作来定义MyParams类型。 诚然,C#3.0的自动属性并不难,但是我讨厌定义只在一个地方使用的类型。 如果用户可以传递带有属性的任意对象,为什么不让该对象成为匿名类型呢? 这是最终代码:

    GetHtmlLink("Click me", new { Key1 = "value1", Key2 = "value2", Key3 = "value3" });
          
     GetHtmlLink("Click me", new { Key1 = "value1", Key2 = "value2", Key3 = "value3" });
    

    Woah! We went from five lines of code with dictionaries to two lines of code with object initializers (minus the type definition), to just one line of code with anonymous types!

    哇! 我们从带有字典的五行代码到带有对象初始化程序的两行代码(减去类型定义),到只有带有匿名类型的一行代码!

  • Bill asks for better Dictionary Initializers, and Alex does it with lambdas in his post, giving him this clean syntax, and PhilHa weighs in with his thoughts. Bill says "Your inner Rubyist is nodding with approval."

    Bill要求使用更好的Dictionary Initializer ,而Alex在其帖子中使用lambda做到了这一点,为他提供了这种简洁的语法, PhilHa则考虑了自己的想法。 比尔说:“您内心的Rubyist正在点头同意。

    Dictionary<string, object> items = Hash<object>(Name => "alex", TargetType => typeof(Uri), Id => 10);
    Assert.AreEqual(10, items["Id"]);

    字典<string,object> items = Hash <object>(Name =>“ alex”,TargetType => typeof(Uri),Id => 10); Assert.AreEqual(10,items [“ Id”]);

  • If you're looking to do small GUI applications, you might take a moment to look at "Shoes" the tiny Ruby UI Tookit. Why? Well, if you're an old WinForms or new WPF guy like myself, (or an old Java guy, also like myself) it's helpful to other perspectives on what a Domain Specific Language for UI might look like. The sample source is here.

    如果您要制作小型GUI应用程序,则可能需要花一点时间来看看“ Shoes”小型Ruby UI Tookit 。 为什么? 好吧,如果您是像我这样的旧WinForms或新WPF家伙(或者也像我一样的旧Java家伙),则对于其他方面的UI界面视图很有帮助。 示例源在这里

    Here are

    这是

    four different snippets in four different language that show a single button with a click event handler that shows a message.

    四种不同语言的四种摘要,显示一个按钮,并带有一个显示消息的click事件处理程序。

    Here's the same thing in Shoes. Note that the

    这在鞋中也是一样。 请注意

    documentation for Shoes is available as a Ransom Note. The creator never uses the acronym "GUI" in the docs or materials, but prefers to think of Shoes as a "toy." It's a pretty near toy, take a look at the 2D animation examples.

    鞋子的文档可作为赎金票据获得。 创建者从不在文档或材料中使用首字母缩写“ GUI”,而是更喜欢将Shoes视为“玩具”。 这是一个非常接近的玩具,请看一下2D动画示例

    Shoes.app {
      button("Press Me") { alert("You pressed me") }
    }
        
  • Rob (and lots of other folks) are exploring what UserControls look like in an ASP.NET MVC world. He says " Your UserControl can be one of two things: A granular bit of UI that renders information passed from a Controller [or] a granular bit of UI that renders information from an application-wide data source." He calls them a "Viewlet" and offers these helper methods: Rendering a ViewUserControl
    The
    MVC Toolkit has a nice method called “RenderUserControl()” that allows you to process your ViewUserControl and output it’s result inline:
    Rob(和其他许多人)正在探索UserControl在ASP.NET MVC世界中的外观。 他说:“ 您的UserControl可以是以下两件事之一:呈现从控制器传递的信息的UI粒度[或]从应用程序范围的数据源呈现信息的UI粒度。 ”他称其为“ Viewlet ”,并提供以下辅助方法: 渲染一个ViewUserControl MVC工具包 有一个名为“RenderUserControl()”好方法,可以让你处理你ViewUserControl和输出它的结果联:
    <%=Html.RenderUserControl(“~/UserControls/UserList.ascx”)%>
    
        

    If the ViewUserControl is typed (say ViewUserControl<MyControllerData>), then it’s ViewData object will be filled for you, and your ViewPage and rendered control will share the same data.

    如果键入了ViewUserControl(例如ViewUserControl <MyControllerData>),则将为您填充它的ViewData对象,并且您的ViewPage和呈现的控件将共享相同的数据。

    If you want to be explicit about it, you can do that as well, specifying the data to pass:

    如果您想对此进行明确说明,也可以通过指定要传递的数据来做到这一点:

    <%=Html.RenderUserControl(“~/UserControls/UserList.ascx”,ViewData.Users)%>
    
        

    Finally, if you need to set properties on the ViewUserControl, you can do that as well by passing in an anonymous type:

    最后,如果需要在ViewUserControl上设置属性,则也可以通过传入匿名类型来做到这一点:

    <%=Html.RenderUserControl(“~/UserControls/UserList.ascx”,ViewData.Users, new {GroupID=2})%>
      

Enjoy, and keep reading code!

享受,并继续阅读代码!

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-12-back-in-black-edition

知乎周源微信

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值