【WP7进阶】——扩展框架组件

 

 组件描述

    该组件为Windows Phone 7 本身C#框架扩展了一系列方法,可以使你在编写代码的时候减少重复复制,并且增加了许多通用功能,使你的编写代码的时候可以更加流畅和得以应手。

 

扩展类别

该组件是将我们日常常用到的数据类型或者集合等操作再一次封装成易于使用的静态方法,分类为如下几大类:

  • String 字符串扩展
  • DateTime 日期扩展
  • Guid 全局唯一标识符扩展
  • IEnumerable 集合扩展
  • Object 对象扩展
  • Stream 流扩展
  • Uri  统一资源标识符扩展
  • Bool  真假“是否”扩展
  • Int  整型扩展

扩展方法体

以下为每个静态类的扩展方法列表

StringExtensions

静态方法成员截图:

Format 代码:

 

public static string Format(this string self, params object[] args)
{
    
if (self == null)
    {
        
throw new ArgumentNullException("format");
    }
    
return string.Format(self, args);
}

 

 

 

HasValue 代码:

 

public static bool HasValue(this string self)
{
    
return !string.IsNullOrEmpty(self);
}

 

 

IsNullOrEmpty代码:

public static bool IsNullOrEmpty(this string self)
{
    
return string.IsNullOrEmpty(self);
}

 

IsValidEmailAddress代码:

public static bool IsValidEmailAddress(this string self)
{
    Regex regex 
= new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
    
return regex.IsMatch(self);
}

 

Split 代码:

 

public static IEnumerable<string> Split(this string self, char separator)
{
    
return self.Split(new char[] { separator });

 

}

 

 

 

public static IEnumerable<string> Split(this string self, string separator)
{
    
return self.Split(new string[] { separator }, StringSplitOptions.None);
}

 

 

ToInt 代码:

 

public static int ToInt(this string self)
{
    
int num;
    
if (!int.TryParse(self, out num))
    {
        
throw new InvalidOperationException("Value is not valid.");
    }
    
return num;
}

 

 

 

Trim 代码:

public static string Trim(this string self, char character)
{
    
return self.Trim(new char[] { character });
}

 

 

DateTimeExtensions

静态方法成员截图:

AddWeek 代码:

public static DateTime AddWeek(this DateTime dateTime)
{
    
return dateTime.AddDays(7.0);
}

 

ToUnixTimestamp代码:

public static long ToUnixTimestamp(this DateTime date)
{
    DateTime time 
= new DateTime(0x7b211000);
    TimeSpan span 
= (TimeSpan) (date - time);
    
return (long) span.TotalSeconds;
}

 

Tip:上面的time 是1/1/1970 12:00:00 AM

GuidExtensions

静态方法成员截图:

IsGuidEmpty 代码 :

public static bool IsGuidEmpty(this Guid self)
{
    
return (self == Guid.Empty);
}

 

RemoveHyphen 代码:

 

public static string RemoveHyphen(this Guid self)
{
    
return self.ToString().Replace("-""");
}

 

 

IEnumerableExtensions

静态方法成员截图:

ExistsIn<T> 代码:

public static bool ExistsIn<T>(this T obj, IEnumerable<T> collection)
{
    
return Enumerable.Contains<T>(collection, obj);
}

 

 

ForEach<T> 代码:

public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
    
if (sequence == null)
    {
        
throw new ArgumentNullException("The secuence is null!");
    }
    
if (action == null)
    {
        
throw new ArgumentNullException("The action is null!");
    }
    
foreach (T local in sequence)
    {
        action(local);
    }
}

 

 

IsNullOrEmpty 代码:

public static bool IsNullOrEmpty<T>(this IEnumerable<T> obj)
{
    
if (!obj.IsNull())
    {
        
return (Enumerable.Count<T>(obj) == 0);
    }
    
return true;
}

ToObservableCollection<T> 代码:

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
{
    ObservableCollection
<T> observables = new ObservableCollection<T>();
    source.ForEach
<T>(new Action<T>(observables.Add));
    
return observables;
}

 

ObjectExtensions

静态方法成员截图:

In 代码:

 

public static bool In(this object self, IEnumerable enumerable)
{
    
return (enumerable.IsNotNull() && Enumerable.Contains<object>(Enumerable.Cast<object>(enumerable), self));
}

 

 

IsNotNull 代码:

 

public static bool IsNotNull(this object self)
{
    
return (self != null);
}

 

 

IsNull 代码:

 

public static bool IsNull(this object self)
{
    
return (self == null);
}

 

NullTolerantEquals 代码:

 

public static bool NullTolerantEquals(this object self, object obj)
{
    
if (self.IsNull() && obj.IsNotNull())
    {
        
return false;
    }
    
if (self.IsNotNull() && obj.IsNull())
    {
        
return false;
    }
    
return ((self.IsNull() && obj.IsNull()) || self.Equals(obj));
}

 

 

StreamExtensions

静态方法成员列表截图:

EqualsStream 代码:

 

public static bool EqualsStream(this Stream originalStream, Stream streamToCompareWith)
{
    
return originalStream.EqualsStream(streamToCompareWith, Math.Max(originalStream.Length, streamToCompareWith.Length));
}

 

 

 

public static bool EqualsStream(this Stream originalStream, Stream streamToCompareWith, long readLength)
{
    originalStream.Position 
= 0L;
    streamToCompareWith.Position 
= 0L;
    
for (int i = 0; i < readLength; i++)
    {
        
if (originalStream.ReadByte() != streamToCompareWith.ReadByte())
        {
            
return false;
        }
    }
    
return true;
}

 

 

ReadAllText 代码:

 

public static string ReadAllText(this Stream stream)
{
    
using (StreamReader reader = new StreamReader(stream))
    {
        
return reader.ReadToEnd();
    }
}

 

 

ToByteArray 代码:

 

public static byte[] ToByteArray(this Stream stream)
{
    MemoryStream writeStream 
= new MemoryStream();
    StreamHelper.CopyStream(stream, writeStream, 
true);
    
return writeStream.ToArray();
}

 

 

UriExtensions

静态方法成员列表截图:

Parameters 代码:

 

public static Dictionary<stringstring> Parameters(this Uri self)
{
    
if (self.IsNull())
    {
        
throw new ArgumentException("Uri can't be null.");
    }
    
if (string.IsNullOrEmpty(self.Query))
    {
        
return new Dictionary<stringstring>();
    }
    
if (CS$<>9__CachedAnonymousMethodDelegate2 == null)
    {
        CS$
<>9__CachedAnonymousMethodDelegate2 = new Func<stringstring>(null, (IntPtr) <Parameters>b__0);
    }
    
if (CS$<>9__CachedAnonymousMethodDelegate3 == null)
    {
        CS$
<>9__CachedAnonymousMethodDelegate3 = new Func<stringstring>(null, (IntPtr) <Parameters>b__1);
    }
    
return Enumerable.ToDictionary<stringstringstring>(self.Query.Substring(1).Split(new char[] { '&' }), CS$<>9__CachedAnonymousMethodDelegate2, CS$<>9__CachedAnonymousMethodDelegate3);
}

 

 

BoolExtensions

静态方法成员列表截图:

IsFalse 代码:

 

public static bool IsFalse(this bool self)
{
    
return !self;
}

 

 

IsTrue 代码:

 

public static bool IsTrue(this bool self)
{
    
return self;
}

 

 

IntExtensions

静态方法成员列表截图:

 

 

IsWithin 代码:

 

public static bool IsWithin(this int self, int minimum, int maximum)
{
    
if (minimum > maximum)
    {
        
throw new ArgumentException("minimum must be of less value than maximum.");
    }
    
return ((self >= minimum) && (self <= maximum));
}

 

 

 

组件下载:Extension

转载于:https://www.cnblogs.com/TerryBlog/archive/2011/02/27/1966479.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275646.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我是一个喜欢桌游的前端女,跟朋友一起做了个桌游交流系统。在自己的系统里直播开发生活,希望得到更多交流...

大家好&#xff0c;我平时非常喜欢玩桌游&#xff0c;同时也是一个正在努力学习中的前端&#xff0c;因为不算很年轻了&#xff0c;所以不敢自称妹子(_ _)。与另一位程序员朋友做了这样一个应用&#xff1a;1.可以聊天交流2.登录了全世界的桌游信息&#xff0c;还可以自己开频道…

尤大是如何发布vuejs的,学完可以应用到项目

大家好&#xff0c;我是若川。本文是读者NewName 投稿&#xff0c;看了我推荐的vuejs如何发布的源码&#xff08;200余行&#xff09;&#xff0c;并成功写了一个小工具。推荐的当晚看到挺晚&#xff0c;这执行力这努力程度超过很多人啊。我本来是打算自己写一篇这个文章的&…

php ip2long 32 64位,詳談php ip2long 出現負數的原因及解決方法

php提供了ip2long與long2ip方法對ip地址處理。1、ip2long — 將一個IPV4的字符串互聯網協議轉換成數字格式int ip2long ( string $ip_address )參數&#xff1a; ip_address 一個標准格式的地址。返回值&#xff1a; 返回IP地址轉換后的數字 或 FALSE 如果 ip_address 是無效的…

(转)从零实现3D图像引擎:(6)向量函数库

1. 数学分析 1) 基本定义&#xff1a; 向量由多个分量组成&#xff0c;2D/3D向量表示一条有向线段。下面的ux,uy就是两个分量。 向量u <ux, uy>&#xff0c;如果从点P1(x1, y1)指向点P2(x2, y2)&#xff0c;则&#xff1a; U p2 - p1 (x2-x1, y2-y1) <Ux, Uy> …

chrome黑暗模式_黑暗模式-并非时尚

chrome黑暗模式In this post I’ve shared my research and views on how the extremely popular “Dark Mode” has moved beyond it’s initial label of “The App Design Fad of 2019”.在这篇文章中&#xff0c;我分享了我的研究和看法&#xff0c;探讨了非常受欢迎的“黑…

A.华华听月月唱歌

链接&#xff1a;https://ac.nowcoder.com/acm/contest/392/A 题意&#xff1a; 月月唱歌超级好听的说&#xff01;华华听说月月在某个网站发布了自己唱的歌曲&#xff0c;于是把完整的歌曲下载到了U盘里。然而华华不小心把U盘摔了一下&#xff0c;里面的文件摔碎了。月月的歌曲…

花了一天精选了20多篇好文,只为与你分享

大家好&#xff0c;我是若川。很多小伙伴因工作繁忙而没有很多自己的时间去学习新知识&#xff0c;更多的是通过一些碎片化的时间来阅读一些他人的技术文章来提升自己的技术视野以及扩展自己的知识储备。这次我精心整理了一批大佬们的优秀文章&#xff0c;感兴趣的可以阅读关注…

matlab判断电话播键音,MATLAB电话拨号音的合成与识别

1.实验目的1.本实验内容基于对电话通信系统中拨号音合成与识别的仿真实现。主要涉及到电话拨号音合成的基本原理及识别的主要方法&#xff0c;利用 MATLAB 软件以及 FFT 算法实现对电话通信系统中拨号音的合成与识别。并进一步利用 MATLAB 中的图形用户界面 GUI 制作简单直观的…

jquery插件之无缝循环新闻列表

一、效果图&#xff1a; tips源码下载&#xff1a;http://files.cnblogs.com/waitingbar/newslist.rar 二、jquery源码: (function($){$.fn.extend({newsList:function(options){var defaults {actName:li, //显示条数名&#xff1b;maxShowNum:6, //最多的显示…

素描的几大基础知识点_2020年让您感到惊奇的5大素描资源

素描的几大基础知识点Sketch is my favorite stand-alone software that I use every day. It is simple, stable, and fast. During my working process, I use other resources that allow me to create UX/UI design faster. These tools have a different direction, but s…

ESMap+Html5+SpringBoot+FastDFS实现导航导购App

github链接 项目实现的简要概述 服务器部分 测试阶段使用的是双系统的开发模式&#xff0c;在Linux服务器上部署了轻量级分布式文件系统fastdfs&#xff0c;且整合了高性能的HTTP和反向代理服务器nginx&#xff1b;在本地的服务器上使用Spring Boot框架&#xff0c;使用其内置的…

你不知道的 Chrome DevTools 玩法

大家好&#xff0c;我是若川。今天再分享一篇 chrome devtools 的文章。之前分享过多篇。Chrome DevTools 全攻略&#xff01;助力高效开发 前端容易忽略的 debugger 调试技巧‍笔者在前段时间的开发时&#xff0c;需要通过 Chrome DevTools来分析一个接口&#xff0c;调试中发…

matlab拟合四次函数表达式,用matlab编写程序求以幂函数作基函数的3次、4次多项式的最小二乘曲线拟合,画出数据散点图及拟合曲线图...

共回答了18个问题采纳率&#xff1a;83.3%x[0.0 0.1 0.2 0.3 0.5 0.8 1.0]; %输入数组>> y[1.0 0.41 0.50 0.61 0.91 2.02 2.46];>> f1inline(poly2sym(polyfit(x,y,3))); %polyfit拟合得到系数,poly2sym由系数得到多项式,inline转换内联函数>> f2inline(pol…

排版人员 快速排版_选择排版前应了解的事项

排版人员 快速排版Design is everywhere, and with design comes text and the content that you’re trying to reach the user with. But before creating your design and choosing what font you want to use, there are some things you should know that will help you a…

matlab光顺拐点,基于MATLAB的最大误差双圆弧逼近曲线的算法及实现.pdf

基于MATLAB的最大误差双圆弧逼近曲线的算法及实现.pdf第31卷第6期 基于MⅢB的最大误差双圆弧逼近曲线的算法及实现文章编号&#xff1a;1004—2539120町】06一唧一∞基于MAⅡ&#xff0e;AB的最大误差双圆弧逼近曲线的算法及实现淮海工学院机械工程系&#xff0c;扛苏连云港笠a…

若川诚邀你加源码共读群,帮助更多人学会看源码~

小提醒&#xff1a;若川视野公众号面试、源码等文章合集在菜单栏中间【源码精选】按钮&#xff0c;欢迎点击阅读&#xff0c;也可以星标我的公众号&#xff0c;便于查找。回复pdf&#xff0c;可以获取前端优质书籍。最近我创建了一个源码共读的前端交流群&#xff0c;希望尝试帮…

体育木地板的施工

文章来源&#xff1a;http://www.bjfhrd.com 体育木地板上有许多暗门&#xff0c;以制造特殊效果&#xff0c;如火焰、烟雾&#xff0c;使房屋、树木、山或人物在一瞬间出现或销售。这种特殊的要求&#xff0c;对于专业体育木地板德施工就有了一定的要求。 专业体育木地板施工&…

imessage_重新设计iMessage以获得更好的用户体验— UX案例研究

imessage体验设计 (EXPERIENCE DESIGN) Communication is a vital part of our everyday lives. We almost don’t even have to think about it. With social media and our devices as prime tools, we’re constantly finding new ways to stay connected. Instant messagin…

mysql 生成时间轴,MYSQL 时间轴数据 获取同一天数据的前3条

创建表数据CREATE TABLE praise_info (id bigint(20) NOT NULL AUTO_INCREMENT COMMENT ID,pic_id varchar(64) DEFAULT NULL COMMENT 图片ID,created_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间,PRIMARY KEY (id),KEY pic_id (pic_id) USING BTREE) ENGINEInn…

【招聘】永辉招前端

大家好&#xff0c;我是若川。这应该招聘第六期。友情帮好友宣传招聘。之前在跟各位读者朋友分享下公众号运营策略 文中提到 公众号主旨是帮助5年内前端小伙伴提升&#xff0c;找到好工作&#xff0c;所以有招聘文。上海 高级前端 本科 25k-50k 16薪岗位职责&#xff1a;1、…