LightSwitch桌面应用程序的注销按钮(非Web应用程序)

时间:2015-03-25 21:34:09

标签: visual-studio-lightswitch lightswitch-2013

如何在右上角添加注销按钮,如更改密码按钮? 如果答案是我不能,我该如何添加屏幕命令栏?

2 个答案:

答案 0 :(得分:0)

虽然我暂时没有解决Silverlight方面的问题(更多地关注HTML 5 LightSwitch路由),但桌面客户端类型中最简单的方法可能是实现一个退出选项(实质上是暴力)退出)。

如果您使用的是按钮,则可以使用以下代码执行此操作: -

partial void Quit_Execute()
{
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
    });
}

或者,如果您更愿意使用“退出”菜单选项,请在项目中添加“占位符”屏幕(新数据屏幕将执行此操作),并在屏幕的“运行”方法中编写以下代码(位于DesktopClient中) \ UserCode \ Application.cs类文件): -

partial void Quit_Run(ref bool handled)
{
    // Set handled to 'true' to stop further processing.
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
    });
    handled = true;
}

通过使用以下代码立即重新加载桌面客户端并自动重新显示登录屏幕,也可以扩展上述方法: -

partial void Quit_Run(ref bool handled)
{
    // Set handled to 'true' to stop further processing.
    Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
    {
        System.Windows.Application.Current.MainWindow.Close();
        dynamic shell = System.Runtime.InteropServices.Automation.AutomationFactory.CreateObject("Shell.Application");
        shell.ShellExecute(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs) + "\\" + System.Windows.Deployment.Current.OutOfBrowserSettings.ShortName.Split('.')[0] + ".appref-ms");
    });
    handled = true;
}

我怀疑以上是实现您的要求的最接近的方法。

我说这个的原因是,之前已经深入研究了重新显示登录界面的可能性,我能做到的最好的方法是以下不完整的方法: -

public partial class Application
{
    System.Windows.Controls.Frame _rootFrame;
    Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage _loginPage;

    partial void Application_Initialize()
    {
        Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
        {
            _rootFrame = ((System.Windows.Controls.Page)((System.Windows.Controls.ContentPresenter)System.Windows.Application.Current.RootVisual).Content).Content as System.Windows.Controls.Frame;
            if (_rootFrame != null)
            {
                _rootFrame.Navigated += new System.Windows.Navigation.NavigatedEventHandler(RootFrame_Navigated);
            }
        });
    }

    void RootFrame_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        if (e.Content is Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage)
        {
            _loginPage = (Microsoft.LightSwitch.Runtime.Shell.Internal.Implementation.LoginPage)e.Content;
            _rootFrame.Navigated -= RootFrame_Navigated;
        }
    }

    partial void Quit_Run(ref bool handled)
    {
        // Set handled to 'true' to stop further processing.
        Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
        {
            if (System.Runtime.InteropServices.Automation.AutomationFactory.IsAvailable == true) 
            {
                var loginViewModel = (Microsoft.LightSwitch.Runtime.Shell.ViewModels.Implementation.Login.LoginViewModel)_loginPage.DataContext;
                loginViewModel.UserName = "";
                loginViewModel.Password = "";
                _rootFrame.Content = _loginPage;
            }
        });
        handled = true;
    }
}

不幸的是,虽然上面的代码(在UserCode \ Application.cs文件中实现)确实重新显示登录屏幕并允许通过其视图模型消隐用户名和密码,但屏幕不起作用,我不知道无法确定如何成功重新激活它。

也许对这个领域有更深入了解的人能够成功实现这种方法(如果有人想尝试,我会很乐意提供一些关于为什么屏幕不起作用的背景知识)。

答案 1 :(得分:0)

这是我如何以最简单的方式找到它。

转到YourProject.server项目,右键单击并单击"添加 - >新项目"

选择" Web表单",将其命名为" SLLogOut.aspx"

使用以下内容替换新创建的aspx文件中的内容:

<%@ Page Language="VB" %>

<script runat="server">
    Protected Overrides Sub OnLoad(e As EventArgs)
        FormsAuthentication.SignOut()
        Session.Abandon()

        ' clear authentication cookie
        Dim cookie1 As New HttpCookie(FormsAuthentication.FormsCookieName, "")
        cookie1.Expires = DateTime.Now.AddYears(-1)
        Response.Cookies.Add(cookie1)

        ' clear session cookie 
        Dim cookie2 As New HttpCookie("ASP.NET_SessionId", "")
        cookie2.Expires = DateTime.Now.AddYears(-1)
        Response.Cookies.Add(cookie2)

        Response.Redirect("DesktopClient/default.htm")
    End Sub
</script>

<!DOCTYPE HTML>
<html>
<head>
    <title>Log out</title>
</head>
<body>
</body>
</html>

注意:记下&#34;重定向&#34;。您可能需要根据程序名称修改URL。

在解决方案资源管理器中,在&#34; SLLogOut.aspx&#34;旁边,单击箭头将其展开。

删除&#34; SLLogOut.aspx.designer.vb&#34;并删除&#34; SLLogOut.aspx.vb&#34;

在您选择的屏幕上创建一个按钮。对于其执行代码,请使用以下命令:

        Dispatchers.Main.Invoke(Sub()
                                    System.Windows.Browser.HtmlPage.Window.Navigate(New Uri("../SLLogOut.aspx", UriKind.Relative))
                                End Sub)

如果您有机会在浏览器外使用此应用,那么您应该添加can_execute代码来禁用此按钮。您可以在与SL Web客户端中的注销按钮相关的其他教程中轻松找到该代码。我只是给你最简单的方法来完成它。在chrome和IE中都进行了测试。

相关问题