Laurence Moroney的'How to Check if Silverlight is installed.'文章不错分享给大家
Silverlight.js库提供了如何检查是否已安装Silverlight的方法。
Silverligh.available
可以检测Silverlight是否被安装,这个方法会返回true或者false。
当然你可能同时需要浏览器和操作系统的相关信息。
通过下面的变量可以获得浏览器信息
Silverlight.ua.Browser
返回的结果是:'MSIE', 'Firefox', 'Safari' 或者 'Unsupported'。
通过下面的变量可以获得操作系统信息
Silverlight.ua.OS
返回的结果是:'Windows', 'MacPPC', 'MacIntel', 'Unsupported'。
利用这些值可以让安装变得尽可能的灵活,同时控件自身的inplaceInstallPrompt 属性也大有用武之地。
[原文地址] 编译:大川
C#算法(一)选择排序
using System;
public class SelectionSorter
{
// public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
private int min;
// private int m=0;
public void Sort(int[] list)
{
for (int i = 0; i < list.Length - 1; ++i)
{
min = i;
for (int j = i + 1; j < list.Length; ++j)
{
if (list[j] < list[min])
min = j;
}
int t = list[min];
list[min] = list[i];
list[i] = t;
// Console.WriteLine("{0}",list[i]);
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
SelectionSorter ss = new SelectionSorter();
ss.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(二)插入排序
using System;
public class InsertionSorter
{
public void Sort(int[] list)
{
for (int i = 1; i < list.Length; ++i)
{
int t = list[i];
int j = i;
while ((j > 0) && (list[j - 1] > t))
{
list[j] = list[j - 1];
--j;
}
list[j] = t;
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
InsertionSorter ii = new InsertionSorter();
ii.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(三)希尔排序
public class ShellSorter
{
public void Sort(int[] list)
{
int inc;
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= list.Length; i += inc)
{
int t = list[i - 1];
int j = i;
while ((j > inc) && (list[j - inc - 1] > t))
{
list[j - 1] = list[j - inc - 1];
j -= inc;
}
list[j - 1] = t;
}
}
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
ShellSorter sh = new ShellSorter();
sh.Sort(iArrary);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
C#算法(四)快速排序
using System;
namespace QuickSorter
{
public class QuickSorter
{
private void Swap(ref int l, ref int r)
{
int s;
s = l;
l = r;
r = s;
}
public void Sort(int[] list, int low, int high)
{
int pivot;
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + 1)
{
if (list[low] > list[high])
Swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> 1;
pivot = list[mid];
Swap(ref list[low], ref list[mid]);
l = low + 1;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r--;
if (l < r)
Swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + 1 < r)
Sort(list, low, r - 1);
if (r + 1 < high)
Sort(list, r + 1, high);
}
}
public class MainClass
{
public static void Main()
{
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
QuickSorter q = new QuickSorter();
q.Sort(iArrary, 0, 13);
for (int m = 0; m <= 13; m++)
Console.WriteLine("{0}", iArrary[m]);
}
}
}
在上海的时候问过SL开发团队Timer的使用问题,他们给了一个很好用的timer,就是下文中介绍的,Google了一下找到这篇文章,跟大家分享。
定时器(Timer)是一个挺常见的功能。通过使用定时器, 可以每隔一段制定的时间后触发某一指定的事件,如刷新、定时提醒等等。那在Silverlight中怎么使用定时器呢?其实也很简单,且听我细细道来。:)
HtmlTimer类和Storyboard
这是最简单的实现定时器的方式。你可以在你的程序中直接使用HtmlTimer类,它位于System.Windows.Browser命名空间下,使用之前你需要在工程中先加入对System.Silverlight.dll的引用。它的使用方法很简单:
HtmlTimer timer = new HtmlTimer();
timer.Interval = 200; //200毫秒
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
void timer_Tick(object sender, EventArgs e)
{
//在这里处理定时器事件
}
但是很不幸,这个类被标记为obsolete,编译的时候会报一个warning,就是说今后的正式版本可能会移除对这个类的支持,因为这个定时器的精度不高,并不适合于间隔很短的动画。那么为了保证我们现在写的代码能够平稳的过渡到Silverlight1.1的正式版本,我们可以使用Storyboard来实现定时器的功能
大家可能利用Blend创建过动画,实际上你创建的动画在XAML文件中对应了Storyboard这个对象,因此我们可以利用它提供的Completed事件(动画结束之后触发)来模拟定时器。
首先在Page.xaml文件中添加一个Storyboard的资源:
<Canvas.Resources>
<Storyboardx:Name="timer"Completed="timer_Tick" />
</Canvas.Resources>
然后在代码中设置定时器的间隔,并开启动画,然后在Completed中重新开始Storyboard就可以模拟定时器的行为了:
timer.Duration = new TimeSpan(0, 0, 0, 0, 200); //200毫秒
timer.Begin();
void timer_Tick(object sender, EventArgs e)
{
//在这里处理定时器事件
timer.Begin();
}
定时器控件
现在大家可能已经知道如何用Storyboard来实现定时器的功能了,我们可以把它包装成一个控件,这样以后我们就可以很方便的在程序中使用定时器了。
为了方便从Xaml文件创建控件,我们先定义一个基类
public class XamlControl : Control
{
private readonly FrameworkElement m_Container;
protected FrameworkElement Container
{
get { return m_Container; }
}
protected XamlControl(string xamlName)
{
Stream stream = GetType().Assembly.GetManifestResourceStream(xamlName);
if (stream == null)
{
throw new ArgumentException("Xaml resource " + xamlName + " not present", "xamlName");
}
using (StreamReader sr = new StreamReader(stream))
{
string xamlData = sr.ReadToEnd();
m_Container = base.InitializeFromXaml(xamlData);
}
}
}
然后我们在工程中添加一个xaml文件,在Properties面板-Advanced-Build Action中选择”Embedded Resource”,这样我们就可以在代码里动态的加载这个xaml文件了。文件的内容如下
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Canvas.Triggers>
<EventTriggerRoutedEvent="Canvas.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboardx:Name='timer'>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Canvas.Triggers>
</Canvas>
下面是定时器控件的代码(需要将下面的SilverlightControl改成你自己建立的工程的名称):
public class Timer : XamlControl
{
private Storyboard m_Timer;
public Timer()
: base("SilverlightControl.Timer.xaml")
{
if (Container == null) return;
m_Timer = Container.FindName("timer") as Storyboard;
m_Timer.Completed += OnComplete;
}
public TimeSpan Interval
{
get
{
return m_Timer.Duration.TimeSpan;
}
set
{
m_Timer.Duration = new Duration(value);
}
}
public event EventHandler<EventArgs> Tick;
private void FireTick()
{
Tick(this, new EventArgs());
}
private void OnComplete(object sender, EventArgs e)
{
FireTick();
m_Timer.Begin();
}
}
这样我们就可以用类似于HtmlTimer的语法,很方便的操作定时器控件了
Timer timer = new Timer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
timer.Tick += timer_tick;
Children.Add(timer); //开始计时
如果要停止计时,只需要用Children.Remove(timer)即可
一点需要注意的地方,当你调用Storyboard.Begin的时候,如果这个Storyboard还没有被加入Xaml的DOM树中,如下图中右下角的红色节点所示,那么这时Silverlight会抛出异常。这在你将Storyboard内嵌在自定义控件里的时候要特别小心,如果你的控件不在当前Xaml的DOM树中,而你调用了这个控件内包含的Storyboard的Begin方法或者Stop方法,都会产生异常。

关于Timer的信息,还可以参考下文:http://advertboy.wordpress.com/2007/08/11/silverlight-timers-what-to-use/
原文链接: 在Silverlight中使用定时器(Timer)
Well, in VS2005 you can create a class library which will build against the Silverlight runtime, but it's a little work.
1. Create a Class Library.
2. Remove all references from it.
3. Right-click on the Project and pick Properties.
4. On the Build tab, click Advanced and check 'Do not reference mscorlib.dll'
5.
Manually add references to mscorlib, agclr, System, System.Core,
System.Silverlight, and System.Xml.Core from the Silverlight install
folder (\Program Files\Microsoft Silverlight\)
At this point your
build outputs should be Silverlight consumable binaries. It looks like
the equivalent command line call to csc.exe (the C# compiler) should
include the references to the mentioned binaries and the /nostdlib
option but may require others as well.
-Scott
这是silverlight开发组Scott Louvau一个回复中写道的
1.创建一个类库
2.删除其中所有引用
3.右击项目选择属性
4.在“生成”选项卡中点击高级,并勾选中‘不引用mscorlib.dll'一项
5.
从Silverlight安装中文件夹(\Program Files\Microsoft
Silverlight\)添加对mscorlib,agclr,System,System.Core,System.Silverlight和
System.Xml.Core的引用
6.在“生成”选项卡中,输出路径修改为ClientBin\
7.需要在iis中点击站点属性,在“HTTP Headers”标签页中,点击“MIME Types”,点击“新建”,加入以下条目(根据不同需要添加类似于mp4的条目):
mp4 video/mp4
xaml application/xaml+xml
dll application/x-msdownload
6和7是我自己补充的,这样就可以在vs2005下开发silverlight了,不过VS2005下不能够调试,如果实在需要调试,只好在虚拟机中安装Orcas,Orcas不可以跟VS2005并存也的确比较郁闷。
万事具备,最好在VS2005下还能有xaml的智能感知,其实这个很容易,只要找到Silverlight相应版本的SDK,在压缩包里可以得到一个schema (silverlight.xsd)文件,它对xaml文件作出了定义,只要将这个文件放入
C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas
就可以在写xaml文件的时候得到智能感知功能。
除了不能调试,VS2005几乎可以做一切Silverlight的工作。省却了庞大的Orcas,立刻Light up the web吧。
大家知道目前用Orcas来开发Silverlight还是很方便的,尽管智能提示做的还不是太完善。Orcas内置了Silverlight项目的模
板,在xaml中声明的对象可以在xaml.cs对应的类中直接使用。由于某些方面的需要,所以要将Orcas下的项目迁移到VS2005环境下,按照VS2005开发Silverlight 1.1的配置说明进行了设置,但是发现编译无法通过,主要提示说许多xaml中的元素对应的对象没有声明,这很令人费解。
最后仔细的看了Orcas下的项目中的文件,发现在obj文件夹中比一般的项目多生成出来一些类似于Default.g.cs这样的文件,打开发现是一个
Partial的类,里面也都是对xaml文件中元素的声明,此外还有一个InitializeComponent()方法,其中都是类似于下面这样的实
例化操作,
canvas = this.FindName("canvas") as Canvas;
很显然这就是
VS2005无法编译成功的原因了,将这些g.cs文件复制到VS2005中,编译成功。看起来很有可能是Orcas在编译的时候自动将xaml文件生成
一个Partial类,有点类似于aspx页面的原理,但是目前VS2005还不支持,所以只能手工调用this.FindName()方法。