微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight之OOB模式下的一些事

本文简介:

1.为什么要使用OOB?使用OOB的作用?

2.如何实现OOB模式

3.对OOB进行一些设置;

4.检测OOB的安装状态;

5.更新应用程序;

6.Webbrowser控件;

7.桌面通知窗口;

8.记录窗体的状态;

9.提升信任;

10.子窗体(非ChildWindow,同时可响应的多个窗体);

11.Com组件访问;

12.在浏览器中提升信任。

1.理解Oob

    实现OOB之后将有如下的作用:
   (1)获得一个桌面的快捷方式;
   (2)可以实现离线访问(当然要结合本地存储);
   (3)获得更高的信任权限。

2.实现一个最简单的OOB应用程序
   (1)右键Silverlight应用程序选择Silverlight选项卡;
   (2)选中"Enable running application out of the browser"复选框;
   (3)点击"Out-of-browser Settings"(OOB设置)按钮,如下:
           可以在Title中输入自己想要的标题即可。
          


这样一个最简单的OOB程序就创建好了,赶紧运行看看吧。

3.自定义应用程序的图标
   在上图中的Icon填写处可以选择一个自定义图片来作为安装后的快捷方式的图标。

4.检测OOB的安装状态
   一般我们通过Application.Current.IsRunningOutOfbrowser来判断当前的程序是否是以OOB模式运行的,当然也可以判断当前OOB安装的状态;   Application.Current.InstallState表示当前OOB安装的状态,一共四个值:Not Installed(未安装),Installing(安装中),InstallFailed(安装失败),Installed(已安装)。
  在APP中添加如下代码,以检测安装状态的变化:

 this.InstallStateChanged += App_InstallStateChanged;

 事件代码如下:

            InstallPage page = this.RootVisual as InstallPage;
            if (page != null)
            {
                // Tell the root visual to show a message by calling a method
                // in InstallPage that updates the display.
                switch (this.InstallState)
                {
                    case InstallState.InstallFailed:
                        page.displayFailed();
                        break;
                    case InstallState.Installed:
                        page.displayInstalled();
                        break;
                }
            }

InstallPage为自定义安装界面,可自行实现:
InstallPage.Xaml:

    <Border @H_404_127@Margin="10"@H_404_127@ Padding="10"@H_404_127@ Background="LightSteelBlue"@H_404_127@
            BorderBrush="SteelBlue"@H_404_127@ BorderThickness="1"@H_404_127@ CornerRadius="3"@H_404_127@
            HorizontalAlignment="Left"@H_404_127@ VerticalAlignment="Top"@H_404_127@ MaxWidth="400">
    <Grid @H_404_127@x:Name="LayoutRoot">
        <Grid.RowDeFinitions>
            <RowDeFinition @H_404_127@Height="Auto"></RowDeFinition>
            <RowDeFinition @H_404_127@Height="Auto"></RowDeFinition>
        </Grid.RowDeFinitions>
        
        <TextBlock @H_404_127@Margin="5"@H_404_127@ x:Name="lblMessage"@H_404_127@ textwrapping="Wrap">You need to install this application to run it.</TextBlock>
        <Button @H_404_127@x:Name="cmdInstall"@H_404_127@ Grid.Row="1"@H_404_127@ Content="Install"@H_404_127@ HorizontalAlignment="Center"@H_404_127@ Padding="10"@H_404_127@ Margin="15"@H_404_127@ Click="cmdInstall_Click"></Button>
        </Grid>
    </Border>

InstallPage.Xaml.cs:

 public InstallPage()
        {
            InitializeComponent();

            if (Application.Current.InstallState == InstallState.Installed)
            {
                lblMessage.Text = "This application is already installed. " +
                    "You cannot use the browser to run it. " +
                    "Instead,use the shortcut on your computer.";
                cmdInstall.IsEnabled = false;
            }
        }

        private void cmdInstall_Click(object sender,RoutedEventArgs e)
        {
            // Make sure that the application is not already installed.
            if (Application.Current.InstallState != InstallState.Installed)
            {
                // Attempt to install it.
                bool installAccepted = Application.Current.Install();

                if (!installAccepted)
                {
                    lblMessage.Text =
                      "You declined the install. Click Install to try again.";
                }
                else
                {
                    cmdInstall.IsEnabled = false;
                    lblMessage.Text = "The application is installing... ";
                }
            }
        }

        public void displayInstalled()
        {
            lblMessage.Text =
              "The application installed and launched. You can close this page.";
        }

        public void displayFailed()
        {
            lblMessage.Text = "The application Failed to install.";
            cmdInstall.IsEnabled = true;
        }

5.更新OOB应用程序
  更新OOB应用程序   

        private void Application_Startup(object sender,StartupEventArgs e)
        {
            if (Application.Current.IsRunningOutOfbrowser)
            {
                // Check for updates.
                Application.Current.CheckAndDownloadUpdateCompleted +=
                Application_CheckAndDownloadUpdateCompleted;
                Application.Current.CheckAndDownloadUpdateAsync();
                this.RootVisual = new MainPage();
            }
            else
            {
                this.RootVisual = new InstallPage();
            }
        }

上述代码添加了CheckAndDownloadUpdateCompleted 回调函数,即检查和下载应用程序之后执行

        void Current_CheckAndDownloadUpdateCompleted(object sender,CheckAndDownloadUpdateCompletedEventArgs e)
        {
            if (e.UpdateAvailable)
            {
                MessageBox.Show("发现了更新,请重新登陆!");
            }
            else if (e.Error != null)
            {
                if (e.Error is PlatformNotSupportedException)
                {
                    MessageBox.Show("哦,我去,出现了平台错误,需要一个新版本的Silverlight,请访问 "  +
                    " http://silverlight.net 进行更新.");
                }
                else
                {
                    MessageBox.Show("发现了应用程序更新,但是更新出现了错误。请删除当前的版本,然后重新安装新版本。 "); 
                }
            }
        }

6.Webbrowser控件的使用
   只需设置Source属性即可,切记要写上http://.

 <Webbrowser @H_404_127@Source="http://www.baidu.com"></Webbrowser>

当你在Silverlight中使用Webbrowser(请注意不是OOB),那么将会提示你Webbrowser只能在Silverlight提升了权限后才可以在浏览器内显示,那么就需要去属性中将"Require evevated trust when running in-browser"选中即可。
执行脚本方法

browser.InvokeScript("AddTwoNumbers",new string[] { "1","2" });

调用Webbrowser的InvokeScript方法,第一个参数为函数名称,第二个参数为脚本函数的参数。

7.桌面通知窗口

 即在Windows桌面的右下角弹出窗口,需要两步骤:
 (1)创建一个自定义通知窗体即一个UserControl

 <Grid>
        <Grid.RowDeFinitions>
            <RowDeFinition @H_404_127@Height="Auto"></RowDeFinition>
            <RowDeFinition></RowDeFinition>
        </Grid.RowDeFinitions>
        <Border @H_404_127@Background="LightGray"@H_404_127@ Height="20">
            <TextBlock @H_404_127@Margin="3"@H_404_127@ FontSize="10"@H_404_127@ Text="MyApplication Notification"></TextBlock>
        </Border>
        <Border @H_404[email protected]="1">
            <Border.Background>
                <LinearGradientBrush>
                    <LinearGradientBrush.GradientStops>
                        <GradientStop @H_404_127@Color="DarkOrange"></GradientStop>
                        <GradientStop @H_404_127@Color="Crimson"@H_404_127@ Offset="0.3"></GradientStop>
                        <GradientStop @H_404_127@Color="DeepPink"@H_404_127@ Offset="0.5"></GradientStop>
                        <GradientStop @H_404_127@Color="DarkOrange"@H_404_127@ Offset="0.7"></GradientStop>
                        <GradientStop @H_404_127@Color="Crimson"@H_404_127@ Offset="1"></GradientStop>
                    </LinearGradientBrush.GradientStops>
                </LinearGradientBrush>
            </Border.Background>
            <TextBlock @H_404_127@x:Name="lblMessage"@H_404_127@ Margin="10"@H_404_127@ FontWeight="Bold"@H_404_127@ FontSize="16"@H_404_127@ Foreground="White"@H_404_127@ textwrapping="Wrap"@H_404_127@
                   HorizontalAlignment="Center"@H_404_127@ VerticalAlignment="Center">Notification goes here.</TextBlock>
        </Border>
    </Grid>

代码比较简单,就是两行,第一行为一个TextBlock,第二行为一个Border,里边也是一个TextBlock,但是在后置代码中对TextBlock的Text进行了修改

   public string Message
        {
            get
            {
                return lblMessage.Text;
            }
            set
            {
                lblMessage.Text = value;
            }
        }

后置代码就这一句即可。

(2)修改App.xaml.cs代码

           if (Application.Current.IsRunningOutOfbrowser == true)
            {
                CustomNotification notification = new CustomNotification();
                notification.Message = "You have just been notified. The time is " +
                DateTime.Now.ToLongTimeString() + ".";
                NotificationWindow window = new NotificationWindow();
                window.Content = notification;
                window.Show(5000);
            }

在Application_Startup中加入以上代码即可,可以看到对Message属性就行了赋值,显示当前的时间,这样就可以在右下角显示通知窗口。

8.记录窗体的状态

 有时候用户希望保存下来最后窗体的状态(Top,Left, Height,Width, WindowState, TopMost),那么通过以下代码即可实现保存和读取这些值,当然这里用到了独立存   储,不了解的童鞋自行去复习哦。

(1)读取配置,还是在Application_Startup事件中加入如下代码

  if (Application.Current.IsRunningOutOfbrowser)
                {
                    IsolatedStorageFile store =
                    IsolatedStorageFile.GetUserStoreForApplication();
                    if (store.FileExists("window.Settings"))
                    {
                        using (IsolatedStorageFileStream fs =
                        store.OpenFile("window.Settings",FileMode.Open))
                        {
                            BinaryReader r = new BinaryReader(fs);
                            Application.Current.MainWindow.Top = r.ReadDouble();
                            Application.Current.MainWindow.Left = r.ReadDouble();
                            Application.Current.MainWindow.Width = r.ReadDouble();
                            Application.Current.MainWindow.Height = r.ReadDouble();
                            r.Close();
                        }
                    }
                }

(2)保存配置

        private void Application_Exit(object sender,EventArgs e)
        {
            if (Application.Current.IsRunningOutOfbrowser)
            {
                // Store window state.
                try
                {
                    IsolatedStorageFile store =
                    IsolatedStorageFile.GetUserStoreForApplication();
                    using (IsolatedStorageFileStream fs =
                    store.CreateFile("window.Settings"))
                    {
                        BinaryWriter w = new BinaryWriter(fs);
                        w.Write(Application.Current.MainWindow.Top);
                        w.Write(Application.Current.MainWindow.Left);
                        w.Write(Application.Current.MainWindow.Width);
                        w.Write(Application.Current.MainWindow.Height);
                        w.Close();
                    }
                }
                catch (Exception err)
                {
                }
            }
        }

9.提升Silverlight应用程序信任

   之所以要提升信任,最主要的原因是,不提升信任的情况下OOB程序的更新会出现问题,所以微软给提升了信任的应用程序提供了这样的功能,但是当提升了信任之后,我们需要做一个签名(虽然VS提供了,但是只是Test的,so...),这就要花钱购买了哦,在此不多说。

   右键Silverlight应用程序,在属性中,点击OOB设置,勾上"Require elevated trust when running outside the browser"即可。

   这时候在安装程序到本地的时候提示如下,可以看到发布者是未验证,也就是我们木有签名哦。

当然提升信任权限的原因不仅仅是上述的原因,提升后得到的功能如下:

(1)对文件的操作,如果不提升则只能操作 我的文档/我的图片/我的视频这几个文件夹,当提升后就可以像Winform那样操作本地的磁盘;

(2)无限制的全屏支持,再也不用看到"Enter esc to ..";

(3)无限制的访问WebService,即不会再出现跨域的情况;

(4)更少的用户同意限制,即当访问剪切板或者访问独立存储等功能的时候更少的显示通知用户是否同意;

(5)自定义窗体,如果你不喜欢窗体的认Border这样的东西,在信任的程序中你可以自定义,下文会提到;

(6)字窗体,如果是信任的程序则可以打开第二个字窗体;

(7)Com组件交互;

(8)平台调用 (P/Invoke),可以访问Windows API等。

9.1制作一个自定义的窗体

     无边框的窗体,分为两步。第一步,右键Silverlight应用程序,然后属性,然后到OOB设置,在Windows Style列表中选择"No Border"即可,这样就可以看到一个无边框的东西哦。

 第二步,可以给窗体加一个边框,在窗体的根节点(认应该是Grid)外放一个Border,如下,这样就有了一个边框。

<Border @H_404_127@x:Name="windowFrame"@H_404_127@ BorderBrush="DarkSlateBlue"@H_404_127@ BorderThickness="5" @H_404_127@CornerRadius="2"@H_404_127@ Margin="0,1,1">

虽然看起来蛮酷的,可是发现无法对窗体进行拖动和大小的修改,所以在操作中还是有点问题的。

9.2 自定义窗体进阶

第一步,给窗体制作一个工具栏

     <Border @H_404_127@x:Name="titleBar"@H_404_127@ Background="LightSteelBlue"@H_404_127@ MouseLeftButtonDown="titleBar_MouseLeftButtonDown">
                <Grid>
                    <Grid.ColumnDeFinitions>
                        <ColumnDeFinition></ColumnDeFinition>
                        <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                        <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                        <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                    </Grid.ColumnDeFinitions>
                    <TextBlock @H_404_127@Margin="5">Title Bar</TextBlock>
                    <Button @H_404[email protected]="1"@H_404_127@ x:Name="cmdMinimize"@H_404_127@ Width="24"@H_404_127@ Click="cmdMinimize_Click">
                        <Path @H_404_127@stroke="Black"@H_404_127@ strokeThickness="4"@H_404_127@ Data="M 1,10 L 13,10" />
                    </Button>
                    <Button @H_404[email protected]="2"@H_404_127@ x:Name="cmdMaximize"@H_404_127@ Width="24"@H_404_127@ Click="cmdMaximize_Click">
                        <Rectangle @H_404_127@stroke="Black"@H_404_127@ strokeThickness="3"@H_404_127@ Height="12"@H_404_127@ Width="12" />
                    </Button>
                    <Button @H_404[email protected]="3"@H_404_127@ x:Name="cmdClose"@H_404_127@ Width="24"@H_404_127@ Click="cmdClose_Click">
                        <Path @H_404_127@stroke="Black"@H_404_127@ strokeThickness="3"@H_404_127@ Data="M 2,2 L 12,12 M 12,2 L 2,12" />
                    </Button>
                </Grid>
            </Border>
   private Window currentwindow;
        public Window Currentwindow
        {
            get
            {
                if (currentwindow != null)
                {
                    // This is being used as a secondary (child) window.
                    return currentwindow;
                }
                else
                {
                    // This is being used as a main window (or as a child window,incorrectly).
                    return Application.Current.MainWindow;
                }
            }
            set
            {
                currentwindow = value;
            }
        }

        private void titleBar_MouseLeftButtonDown(object sender,System.Windows.Input.MouseButtonEventArgs e)
        {
            Currentwindow.DragMove();
        }

        private void cmdMinimize_Click(object sender,System.Windows.RoutedEventArgs e)
        {
            Currentwindow.WindowState = WindowState.Minimized;
        }

        private void cmdMaximize_Click(object sender,System.Windows.RoutedEventArgs e)
        {
            if (Currentwindow.WindowState == WindowState.normal)
            {
                Currentwindow.WindowState = WindowState.Maximized;
            }
            else
            {
                Currentwindow.WindowState = WindowState.normal;
            }
        }

        private void cmdClose_Click(object sender,System.Windows.RoutedEventArgs e)
        {
            Currentwindow.Close();
        }

 第二步,修改刚才我们的代码,即在Border中放一个StackPanel或者放一个两行的Grid,使工具栏和内容分别位于上下位置即可,效果如下。

这样可以在工具栏上按下左键进行拖动,也可以通过三个按钮进行最小化,最大化,关闭操作,此时看起来还算一个不错的自定义窗体,但是你会发现无法进行ReSize,接下来进行ReSize的处理。

 

上图就是一个可进行Resize效果的部件组成,在Content内容的四周均为Rectangle,当然这些Rectangle都是可以进行拉伸和缩放的,代码如下:

 <Border @H_404_127@x:Name="windowFrame"@H_404_127@ BorderBrush="DarkSlateBlue"@H_404_127@ BorderThickness="5"@H_404_127@ CornerRadius="2"@H_404_127@ Margin="0,1">
        <Grid @H_404_127@x:Name="resizeContainer">
            <Grid.ColumnDeFinitions>
                <ColumnDeFinition @H_404_127@Width="5"></ColumnDeFinition>
                <ColumnDeFinition @H_404_127@Width="*"></ColumnDeFinition>
                <ColumnDeFinition @H_404_127@Width="5"></ColumnDeFinition>
            </Grid.ColumnDeFinitions>
            <Grid.RowDeFinitions>
            <RowDeFinition @H_404_127@Height="5"></RowDeFinition>
            <RowDeFinition @H_404_127@Height="*"></RowDeFinition>
            <RowDeFinition @H_404_127@Height="5"></RowDeFinition>
            </Grid.RowDeFinitions>
            <Rectangle @H_404_127@x:Name="rect_TopLeftCorner"@H_404_127@ Grid.Row="0"@H_404_127@ Grid.Column="0"@H_404_127@ Cursor="SizeNWSE"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_TopEdge"@H_404_127@ Grid.Row="0"@H_404_127@ Grid.Column="1"@H_404_127@ Cursor="SizeNS"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_TopRightCorner"@H_404_127@ Grid.Row="0"@H_404_127@ Grid.Column="2"@H_404_127@ Cursor="SizenesW"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_LeftEdge"@H_404_127@ Grid.Row="1"@H_404_127@ Grid.Column="0"@H_404_127@ Cursor="SizeWE"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_RightEdge"@H_404_127@ Grid.Row="1"@H_404_127@ Grid.Column="2"@H_404_127@ Cursor="SizeWE"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_BottomLeftCorner"@H_404_127@ Grid.Row="2"@H_404_127@ Grid.Column="0"@H_404_127@ Cursor="SizenesW"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_BottomEdge"@H_404_127@ Grid.Row="2"@H_404_127@ Grid.Column="1"@H_404_127@ Cursor="SizeNS"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <Rectangle @H_404_127@x:Name="rect_BottomrightCorner"@H_404_127@ Grid.Row="2"@H_404_127@ Grid.Column="2"@H_404_127@ Cursor="SizeNWSE"@H_404_127@ Fill="Transparent"@H_404_127@ MouseLeftButtonDown="rect_Resize" />
            <StackPanel @H_404[email protected]="1"@H_404_127@ Grid.Column="1">
                <Border @H_404_127@x:Name="titleBar"@H_404_127@ Background="LightSteelBlue"@H_404_127@ MouseLeftButtonDown="titleBar_MouseLeftButtonDown">
                    <Grid>
                        <Grid.ColumnDeFinitions>
                            <ColumnDeFinition></ColumnDeFinition>
                            <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                            <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                            <ColumnDeFinition @H_404_127@Width="Auto"></ColumnDeFinition>
                        </Grid.ColumnDeFinitions>
                        <TextBlock @H_404_127@Margin="5">Title Bar</TextBlock>
                        <Button @H_404[email protected]="1"@H_404_127@ x:Name="cmdMinimize"@H_404_127@ Width="24"@H_404_127@ Click="cmdMinimize_Click">
                            <Path @H_404_127@stroke="Black"@H_404_127@ strokeThickness="4"@H_404_127@ Data="M 1,10" />
                        </Button>
                        <Button @H_404[email protected]="2"@H_404_127@ x:Name="cmdMaximize"@H_404_127@ Width="24"@H_404_127@ Click="cmdMaximize_Click">
                            <Rectangle @H_404_127@stroke="Black"@H_404_127@ strokeThickness="3"@H_404_127@ Height="12"@H_404_127@ Width="12" />
                        </Button>
                        <Button @H_404[email protected]="3"@H_404_127@ x:Name="cmdClose"@H_404_127@ Width="24"@H_404_127@ Click="cmdClose_Click">
                            <Path @H_404_127@stroke="Black"@H_404_127@ strokeThickness="3"@H_404_127@ Data="M 2,12" />
                        </Button>
                    </Grid>
                </Border>
                <Grid @H_404_127@x:Name="LayoutRoot"@H_404_127@ Background="White">
                    <Grid.RowDeFinitions>
                        <RowDeFinition @H_404_127@Height="*"></RowDeFinition>
                    </Grid.RowDeFinitions>
                    <Image @H_404_127@Source="2.jpg"></Image>
                </Grid>
            </StackPanel>
        </Grid>
            
        </Border>

在外围的Border中有一个Grid,共为三行和三列,第一行和第三行以及第一列和第二列均为Rectangle的位置,而中间区域为内容,另外为每一个Rectangle添加了鼠标按下左键事件,代码如下:

 private void rect_Resize(object sender,MouseButtonEventArgs e)
        {
            if (sender == rect_TopLeftCorner)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.TopLeft);
            }
            else if (sender == rect_TopEdge)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.Top);
            }
            else if (sender == rect_TopRightCorner)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.TopRight);
            }
            else if (sender == rect_LeftEdge)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.Left);
            }
            else if (sender == rect_RightEdge)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.Right);
            }
            else if (sender == rect_BottomLeftCorner)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.BottomLeft);
            }
            else if (sender == rect_BottomEdge)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottom);
            }
            else if (sender == rect_BottomrightCorner)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.Bottomright);
            }
        }

效果图对比如下:

认大小。

手动调整后的大小。

10.子窗体

    在普通的Silverlight程序中我们可以通过ChildWindow来实现子窗体,可是仍然是只有一个窗体可响应,在这里要说的子窗体是可以同时响应的,效果如下:

最下边的窗体为主窗体,顶层的两个为创建的子窗体,怎样是不是很像winform中的那样呢,开始进入代码

主窗体代码

    <Grid @H_404_127@x:Name="LayoutRoot"@H_404_127@ Background="White">
        <StackPanel @H_404_127@Margin="20">
            <Button @H_404_127@x:Name="btnCreateBasicWindow"@H_404_127@ Content="创建一个简单的子窗体"@H_404_127@ Margin="2"@H_404_127@ Click="btnCreateBasicWindow_Click"></Button>
            <Button @H_404_127@x:Name="btnCreateFancyWindow"@H_404_127@ Content="创建一个自定义的子窗体"@H_404_127@ Margin="2"@H_404_127@ Click="btnCreateFancyWindow_Click"></Button>
            <Button @H_404_127@x:Name="btnCloseWindow"@H_404_127@ Content="关闭窗体"@H_404_127@ Margin="2"@H_404_127@ Click="btnCloseWindow_Click"></Button>
        </StackPanel>
    </Grid>

后置代码

        private Window winSimple;
        private Window winFancy;
        private void btnCreateBasicWindow_Click(object sender,RoutedEventArgs e)
        {
            Grid grid = new Grid();
            grid.Background = new SolidColorBrush(Colors.White);
            TextBlock textContent = new TextBlock();
            textContent.Text = "Here's some content!";
            grid.Children.Add(textContent);
            winSimple = new Window();
            winSimple.Content = grid;
            winSimple.Width = 300;
            winSimple.Height = 100;
            winSimple.Title = "Simple Window";
            winSimple.Visibility = Visibility.Visible;
        }

        private void btnCreateFancyWindow_Click(object sender,RoutedEventArgs e)
        {
            winFancy = new Window();
            MainPage userControl = new MainPage();
            winFancy.Width = 200;
            winFancy.Height = 300;
            winFancy.Content = userControl;
            winFancy.Title = "Custom Window";
            //winStandard.Show();和下述代码效果一样
            winFancy.Visibility = Visibility.Visible;
        }

        private void btnCloseWindow_Click(object sender,RoutedEventArgs e)
        {
            winSimple.Visibility = System.Windows.Visibility.Collapsed;
            winFancy.Visibility = System.Windows.Visibility.Collapsed;
        }

 代码原理很简单,手动创建一个Window,然后将其Content设置为创建的Usercontrol,然后设置下高度和宽度,最后要让其Visible,这样就ok了;关闭也就是让其Collapsed。

问题来了,当你弹出自定义的窗体之后,并且试图去进行Resize的时候,你会发现效果并不是之前设想的,拖动之后改变的大小是主窗体的而非自己的,这是因为我们在窗体中修改的大小是Application.Current.MainWindow,所以改变的就是主窗体了,当然我们可以进行修改,如下:

            winFancy = new Window();
            MainPage userControl = new MainPage();
            userControl.Currentwindow = winFancy;
            winFancy.Width = 300;
            winFancy.Height = 200;
            winFancy.Content = userControl;
            winFancy.Title = "Custom Window";
            //winStandard.Show();和下述代码效果一样
            winFancy.Visibility = Visibility.Visible;

改动只有两处,一处就是上述代码添加了"userControl.Currentwindow = winFancy;",Currentwindow,为我们在自定义窗体中定义的属性,表示当前的主窗体,将主窗体设置为外围的容器窗体,这样进行Resize就可以同步了哦。

 

11.Com组件的访问

下面是一个访问Word的Com组件调用方式:

            using (dynamic word = AutomationFactory.CreateObject("Word.Application"))
            {
                dynamic document = word.Documents.Add();
                dynamic paragraph = document.Content.Paragraphs.Add;
                paragraph.Range.Text = "heading 1";
                paragraph.Range.Font.Bold = true;
                paragraph.Format.SpaceAfter = 18;
                paragraph.Range.InsertParagraphAfter();
                paragraph = document.Content.Paragraphs.Add;
                paragraph.Range.Font.Bold = false;
                paragraph.Range.Text = "This is some more text";
                word.Visible = true;
            }

 下面这个是访问计算器的一个Com组件:

            using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                shell.Run("calc.exe");
            }

 

12.在浏览器中提升权限

 有时候我们希望在浏览器中也是可以操作桌面的一些功能,比如上述中德操作Com组件,那么通过提升权限也是可以做到的,并不需要一定是OOB模式。

好了,到此 OOB下的一些事情说得挺多的,当然还有很多没有提到,大家可以多搜索,多总结,多提意见和建议。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐