ฉันจะเพิ่มปุ่ม ออกจากระบบ เช่น ปุ่ม เปลี่ยนรหัสผ่าน ที่มุมขวาบนได้อย่างไร หากคำตอบคือทำไม่ได้ ฉันจะเพิ่มแถบคำสั่งบนหน้าจอได้อย่างไร
ปุ่มออกจากระบบสำหรับแอปพลิเคชัน LightSwitch Desktop (ไม่ใช่ Web App)
คำตอบ (2)
นี่คือวิธีที่ฉันทำวิธีที่ง่ายที่สุดที่หาได้
ไปที่โปรเจ็กต์ YourProject.server ของคุณ คลิกขวาแล้วคลิก "เพิ่ม--> รายการใหม่"
เลือก "แบบฟอร์มเว็บ" ตั้งชื่อเป็น "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>
หมายเหตุ: จดบันทึก "การเปลี่ยนเส้นทาง" คุณอาจต้องแก้ไข URL ขึ้นอยู่กับชื่อโปรแกรมของคุณ
ในตัวสำรวจโซลูชัน ถัดจาก "SLLogOut.aspx" คลิกลูกศรเพื่อขยาย
ลบทั้ง "SLLogOut.aspx.designer.vb" และลบ "SLLogOut.aspx.vb"
สร้างปุ่มบนหน้าจอที่คุณเลือก สำหรับโค้ดดำเนินการให้ใช้ดังต่อไปนี้:
Dispatchers.Main.Invoke(Sub()
System.Windows.Browser.HtmlPage.Window.Navigate(New Uri("../SLLogOut.aspx", UriKind.Relative))
End Sub)
หากมีโอกาสที่คุณจะใช้แอปนี้นอกเบราว์เซอร์ คุณควรเพิ่มโค้ด can_execute เพื่อปิดใช้งานปุ่มนี้ คุณสามารถค้นหาโค้ดนั้นได้ง่ายเพียงพอในบทช่วยสอนอื่นๆ ที่เกี่ยวข้องกับปุ่มออกจากระบบในเว็บไคลเอ็นต์ SL ฉันแค่ให้วิธีที่ง่ายที่สุดแก่คุณในการบรรลุเป้าหมาย ทดสอบแล้วทั้ง Chrome และ IE
ในขณะที่ฉันไม่ได้จัดการกับสิ่งต่าง ๆ ของ Silverlight มาระยะหนึ่งแล้ว (โดยมุ่งเน้นไปที่เส้นทาง HTML 5 LightSwitch มากขึ้น) อาจเป็นวิธีที่ง่ายที่สุดในประเภทไคลเอ็นต์เดสก์ท็อปคือการใช้ตัวเลือกการออก (ซึ่งในสาระสำคัญบังคับให้ออกจากระบบ) .
หากคุณใช้ปุ่ม คุณจะต้องใช้โค้ดต่อไปนี้: -
partial void Quit_Execute()
{
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() =>
{
System.Windows.Application.Current.MainWindow.Close();
});
}
หรือหากคุณต้องการมีตัวเลือกเมนู Quit ให้เพิ่มหน้าจอ 'ตัวยึดตำแหน่ง' ให้กับโปรเจ็กต์ของคุณ (หน้าจอข้อมูลใหม่จะทำ) และเขียนโค้ดต่อไปนี้ในวิธี Run ของหน้าจอ (อยู่ใน 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) แสดงหน้าจอเข้าสู่ระบบอีกครั้ง และอนุญาตให้เว้นว่างชื่อผู้ใช้และรหัสผ่านผ่านโมเดลมุมมอง แต่หน้าจอไม่ทำงานและฉันไม่สามารถ เพื่อกำหนดวิธีเปิดใช้งานอีกครั้งได้สำเร็จ
บางทีคนที่มีความรู้เชิงลึกเกี่ยวกับเรื่องนี้อาจจะสามารถนำแนวทางประเภทนี้ไปปรับใช้ได้สำเร็จ (ถ้าใครอยากลอง ฉันจะให้ข้อมูลเพิ่มเติมเล็กน้อยเกี่ยวกับสาเหตุที่หน้าจอใช้งานไม่ได้)