随笔 - 54  文章 - 0 评论 - 11 trackbacks - 0

IE整天爆漏洞,还经常假死,改投Firefox阵营。比起IE和Chrome来虽然多了那么点儿山寨味,但毕竟是没有微软和谷歌这样的巨头扶持下做得最好的浏览器了,简直神话一般。

总结一下平常不太注意但很有用的快捷键:

键盘

ctrl+T

打开一个新的tab

ctrl+shift+T

打开刚刚关闭的tab。能记忆多个。

/

quick find,轻量级的ctrl+F,好处是按键更方便,且find栏会自动消失不用费事去关。缺点是没有highlight all和大小写忽略选项。

F7

光标在网页中移动,用键盘来选择文本;

还可以记住光标在网页中的位置。

ctrl+L

到地址栏,键位比alt+D更方便

ctrl+K

到搜索栏,键位比ctrl+E更方便

查找页面上的超链接,例如输入“快捷键”会找到文字中包含“快捷键”的超链接。找到后按回车可直接打开链接。

ctrl+shift+D

收藏所有打开的tab,会让你起一个文件夹名,然后把所有已打开的tab都放进这个文件夹

ctrl+G

要到下一处匹配的快捷键,上一处匹配是ctrl+shift+G。经常与ctrl+F,/,这三个快捷键配合使用。

ctrl+S

保存网页,遗憾的是还不支持mht格式

ctrl+U

查看代码,对开发者比较有用

ctrl+0

缩放重置为100%,放大/缩小是ctrl+/-

ctrl+H

查看历史记录

ctrl+Enter

输入地址的时候自动补充http://www..com。例如在地址栏输入google,按下之后就补充成为http://www.google.com

ctrl+PageUp

前一个tab,比ctrl+shift+Tab舒服。下一个tabctrl+PageDown

ctrl+9

最后一个tab,比第9tab好多了:)

Firefox的一个插件Tab Mix Plus支持多行Tab,有了这个快捷键,同时打开好多个tab也不是太难管理。

ctrl+R

刷新网页,比F5方便,记成Reload也很

ctrl+J

打开已下载的文件列表(用浏览器下载的)

ctrl+shift+P

Private browsing

ctrl+shift+Del

Delete history

Del

删除地址栏当前选中的自动补全建议,可用方向键移动到要的建议条目上删除。

ctrl+I

open bookmarks

alt+DownArrow

换搜索引擎,非常好用!一些著名网站,例如Wiki、淘宝、MSDN应有尽有,按下alt+DownArrow之后,只要按首字母就可直接找过去,所以搜索引擎最好是英文开头的。

鼠标

在标签栏上(也就是标签那一行)的空白处双击,也能打开一个新的tab,比找那个加号按钮更方便。

页面中的任何东西,选择之后都可以拖动。选中一段文字,直接拖进搜索栏非常方便。

posted @ 2010-01-27 12:05 董超 阅读(10) | 评论 (0)编辑

WPF本身并没有内置的单体模式支持(以后的版本会支持),而WindowsFormsApplicationBase类中有对单体模式的支持(全名是Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase),所以我们用WindowsFormsApplicationBase做一个wrapper就可以实现单体模式了。
实现单体有三个知识点需要了解:

(*) The IsSingleInstance property enables a single-instance application. You set this property
to true in the constructor.
(*) The OnStartup() method is triggered when the application starts. You override this
method and create the WPF application object at this point.
(*) The OnStartupNextInstance() method is triggered when another instance of the application
starts up.

另外在实现的时候需要注意:

(*) 需要添加Microsoft.VisualBasic.dll的程序集引用

(*) the application needs to start with a traditional Main() method, rather than an App.xaml file.

代码实现

添加一个新的文件,例如名叫Startup.cs,在里面添加如下代码:

namespace 你的命名空间
{
   
public class Startup
    {
        [STAThread]
       
public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper wrapper
= new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
    }

   
public class SingleInstanceApplicationWrapper :
        Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
       
private App app; // 这才是真正的WPF Application

       
public SingleInstanceApplicationWrapper()
        {
           
this.IsSingleInstance = true;
        }

       
// 第一次打开调这个方法
        protected override bool OnStartup(
            Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            app
= new App();
            app.Run();

           
return false;
        }

       
// 再次打开调这个方法
        protected override void OnStartupNextInstance(
            Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e)
        {
           
// 当用户试图再次打开这个程序的时候
            MessageBox.Show("您正在运行该程序");
        }
    }

   
/// <summary>
   
/// Interaction logic for App.xaml
   
/// </summary>
    public partial class App : Application
    {
    }
}

 

注意:

你刚开始建的项目类型可能是WPF,在项目的属性里,application标签下有个Startup Object,一开始默认应该是“你的namespace.App”,除此之外就只有Not Set了,没别的可选。是因为Main函数才是程序的入口,VS会根据Main函数去搜寻哪些可以作为Startup Object。但我们查看App这个类的代码会发现,默认情况下里面并没有Main函数,这是怎么回事呢?原来VS在编译时会结合xaml自动把代码补充完整。xaml里面的x:Class="你的namespace.App”,其中"你的namespace.App”会作为可选的Startup Object出现。
当在别的类里面新加入了Main函数,就会把这个类也列在Startup Object的可选列表中(有时候可能要重启VS才能在Startup Object里看到新的项)。 针对上面的例子(文件名是Startup.cs)则把“命名空间.Startup”作为Startup Object.

 

总结

以上实现的是真正的single instance,而不是仅仅通过查找有没有相同的进程名来查看程序是否已经启动。用查看相同进程名的方法实现single instance很不可靠。设想:如果有一个恶意程序,每秒查看一下所有的进程,看其中是否有a.exe(假如这恰好是你的程序名)如果没有,则启动一个a.exe,这个a.exe哪怕什么都不干,就为了在哪里占用这个进程名。那么你如果用查看相同进程名来实现single instance的话,除非在一秒钟之内杀死a.exe进程并启动你自己的a.exe,否则可能永远都启动不了你自己的a.exe。当然了,如果这个恶意程序执行的是:定期查看有没有这个进程,如果有则立即杀死,那就更麻烦了。

posted @ 2009-11-24 02:24 董超 阅读(41) | 评论 (0)编辑

SessionEnding

By default, an application shuts down when the Windows session ends, which occurs when a user logs off or shuts down.

You can detect when a session ends by handling the SessionEnding event. If an application needs to prevent the session from ending, the SessionEndingCancelEventArgs argument that is passed to the event handler exposes the Cancel that you set to true (the default value is false).

注意SessionEnding的适用范围:

SessionEnding is not raised by console applications or XAML browser applications (XBAPs) .

SessionEnding is raised only on the thread that creates the Application object.

例子:
在退出windows session的时候弹出对话框让用户选择是否退出。

    public partial class App : Application
    {
       
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
           
// Ask the user if they want to allow the session to end
            string msg = string.Format("{0}. End session?", e.ReasonSessionEnding);
            MessageBoxResult result
= MessageBox.Show(msg, "Session Ending", MessageBoxButton.YesNo);

           
// End session, if specified
            if (result == MessageBoxResult.No)
            {
                e.Cancel
= true;
            }
        }
    }

 

然后在XAML里配置,让上面的方法与SessionEnding Event相关联。

<Application x:Class="LearnWPF.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml%22
    StartupUri
="Window1.xaml"
    SessionEnding
="App_SessionEnding"> 

</Application>

 

 

DispatcherUnhandledException

Occurs when an unhandled exception occurs anywhere in your application (on the main application thread).
By responding to this event, you can log critical errors.

You can even choose to neutralize the exception and continue running your application by setting the
DispatcherUnhandledExceptionEventArgs.Handled property to true.

指定event handler的方法还是一样,先写好方法:

private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    MessageBox.Show(
"An unhandled " + e.Exception.GetType().ToString() +
   
" exception was caught and ignored.");
    e.Handled
= true; // 把该异常设置为已处理,以防止程序因此退出
}


再编辑XAML:

<Application x:Class="PreventSessionEnd.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri
="Window1.xaml"
    DispatcherUnhandledException
="App_DispatcherUnhandledException"
>
</Application>

 

用override的方式来指定event handler

重载哪个方法?很简单,event叫什么名字,在前面加个On就是了。例如OnStartup,OnSessionEnding。只有DispatcherExceptionUnhandled事件是个例外,没有OnDispatcherExceptionUnhandled方法,只有像上文那样在XAML中明确写出event handler了。

 

public partial class App : Application
{
   
private bool unsavedData = false;
   
public bool UnsavedData
    {
       
get { return unsavedData; }
       
set { unsavedData = value; }
    }
   
protected override void OnStartup(StartupEventArgs e)
    {
       
base.OnStartup(e); // call base first
        UnsavedData = true;
    }
   
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
    {
       
base.OnSessionEnding(e); // call base first
        if (UnsavedData)
        {
            e.Cancel
= true;
            MessageBox.Show(
           
"The application attempted to be closed as a result of " +
            e.ReasonSessionEnding.ToString()
+
           
". This is not allowed, as you have unsaved data.");
        }
    }
}
posted @ 2009-11-24 02:18 董超 阅读(34) | 评论 (0)编辑

 

有三种shutdown mode

Member name

Description

OnLastWindowClose

An application shuts down when either the last window closes, or Shutdown is called.

OnMainWindowClose

An application shuts down when either the main window closes, or Shutdown is called.

OnExplicitShutdown

An application shuts down only when Shutdown is called.


其中OnMainWindowClose这个挺有用,但首先要知道如何指定MainWindow。

指定MainWindow的两种途径:

1. 在代码中指定

 

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public App()
{
    Window oldMainWindow 
= this.MainWindow;// Get main window 

    Window newMainWindow 
= new Window1();
    
this.MainWindow = new Window();// Set mainwindow

 

2. 在XAML中指定

 

 

<Application 
    
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri
="StartupWindow.xaml"
    
>
  
<Application.MainWindow>
    
<NavigationWindow Source="MainPage.xaml" Visibility="Visible"></NavigationWindow>
  
</Application.MainWindow>
</Application>

 

配置Shutdown Mode
这里我们选择OnMainWindowClose:

<Application x:Class="TestApplication.App"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation%22
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml%22
    StartupUri
="Window1.xaml" 
    ShutdownMode
="OnMainWindowClose"
    
>
</Application>

 

posted @ 2009-11-23 12:59 董超 阅读(22) | 评论 (0)编辑

以前我所不知道而且还很有用的:

按键

上下文环境

功能

F2 Windows exploer 重命名
F10 app 菜单
Alt+Enter windows exploer 查看属性
Alt+Enter IE 全屏,相当于F11
Ctrl+I IE Favorite
Ctrl+H IE History
Ctrl+J IE Feed
Ctrl+T IE 新打开一个Tab
Ctrl+N IE 打开一个新window,并把当前页面复制到那个window
Ctrl+Q IE 打开/关闭平铺tab
当打开的tab一多,就数不过来,不知道该用ctrl+哪个数字来导航了。平铺后数起来容易些。
posted @ 2009-11-19 01:49 董超 阅读(14) | 评论 (0)编辑

如果先关service再关client

那么client的channel.Close()会报异常:

“The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.”

 

如果还未启动service就打开client

则client的channel.Open()会报异常:

“The endpoint was not found. Please ensure that you can connect to the internet using HTTP port 80 and TCP port 808.”

 

Client与Service两边的属性的Namespace不一致

例如:

Client端:

    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/")]
    
public interface IEchoContract
    {
        [OperationContract]
        String Echo(
string text);
    }


Server端:

     // 注意,最后多了一个Echo
    [ServiceContract(Name = "IEchoContract", Namespace = "http://samples.microsoft.com/ServiceModel/Relay/Echo")]
    
public interface IEchoContract
    {
        [OperationContract]
        String Echo(
string text);
    }

在Client执行方法channel.Echo(input)的时候会报异常:

"The message with Action 'http://samples.microsoft.com/ServiceModel/Relay/IEchoContract/Echo' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None)."

 

避免Contract和Behavior的paste&copy error

如果一个该用ServiceBehavior的地方用了ServiceContract,例如:

 

//should be [ServiceBehavior(Name = "EchoService", Namespace = http://samples.microsoft.com/ServiceModel/Relay/)]
[ServiceContract(Name = "IEchoContract", Namespace = http://samples.microsoft.com/ServiceModel/Relay/)]
public class EchoService : IEchoContract
{
    
public string Echo(string text)
    {
        Console.WriteLine(
"Echoing: {0}", text);
        
return text;
    }
}

则在new一个host的时候,ServiceHost host = new ServiceHost(typeof(EchoService), address); // 会抛出异常
异常信息:

"The service class of type Microsoft.ServiceBus.Samples.EchoService both defines a ServiceContract and inherits a ServiceContract from type Microsoft.ServiceBus.Samples.IEchoContract. Contract inheritance can only be used among interface types.  If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute.  Consider moving the ServiceContractAttribute on type Microsoft.ServiceBus.Samples.IEchoContract to a separate interface that type Microsoft.ServiceBus.Samples.IEchoContract implements."

posted @ 2009-11-09 20:18 董超 阅读(21) | 评论 (0)编辑
     摘要: The Service Bus allows a Windows Communication Foundation-based (WCF) application to listen at a public network address, even if the application is located behind a NAT or network firewall.Another cor...  阅读全文
posted @ 2009-11-05 00:55 董超 阅读(22) | 评论 (0)编辑
     摘要: WPF Layout Philosophy(*) Elements (like controls) should not be explicitly sized. Instead, they grow to fit theircontent. For example, a button expands as you add more text. You can limit controls toa...  阅读全文
posted @ 2009-11-04 19:39 董超 阅读(51) | 评论 (0)编辑
     摘要: (*) 窗口的摆放在一个窗口上点右键,有floating, dockable, tabbed document这三种摆放方式。floating: 浮在最上,任何地方。dockable: 浮在最上,任何地方,且可以锚定在一侧。有了dockable之后其实floating就成鸡肋了。tabbed document: 把窗口以tab的形式,和一般的源文件展示在一起。挺好用的一个功能。 (*) 读代码的好...  阅读全文
posted @ 2009-10-28 13:38 董超 阅读(38) | 评论 (0)编辑
     摘要: (*) 提问 VS的Help菜单,MSDN Forum,点开之后会打开msdn,只要用Windows Live ID登陆可以直接发帖提问了。 只要输入了标题,就会有出现已经有人问过的类似问题,非常友好: We've found questions similar to yours. 而且会有该问题所在的位置: Windows Azure Platform Developer Center >...  阅读全文
posted @ 2009-10-27 20:44 董超 阅读(16) | 评论 (0)编辑