[英]How to make Xamarin.Mac app “Open at Login”?
我有一個Xamarin.Mac應用程序,需要在登錄時自動打開。 我如何讓我的應用程序獲得此設置,而無需手動單擊它?
我可以給您一個提示,以編程方式進行操作。
對於這種方法,您需要通過DllImport
使用對本機庫的調用。
以下代碼將給您一個思路,如何進行:
//needed library
const string DllName = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices";
static LSSharedFileList ()
{
dllHandle = Dlfcn.dlopen (DllName, 0);
kLSSharedFileListSessionLoginItems = Dlfcn.GetStringConstant (dllHandle, "kLSSharedFileListSessionLoginItems");
kLSSharedFileListItemLast = Dlfcn.GetStringConstant (dllHandle, "kLSSharedFileListItemLast");
}
[DllImport(DllName)]
public static extern IntPtr LSSharedFileListCreate (
IntPtr inAllocator,
IntPtr inListType,
IntPtr listOptions);
[DllImport(DllName, CharSet=CharSet.Unicode)]
public static extern void CFRelease (
IntPtr cf
);
[DllImport(DllName)]
public extern static IntPtr LSSharedFileListInsertItemURL (
IntPtr inList,
IntPtr insertAfterThisItem,
IntPtr inDisplayName,
IntPtr inIconRef,
IntPtr inURL,
IntPtr inPropertiesToSet,
IntPtr inPropertiesToClear);
這是實際的代碼段:
public static void EnableLogInItem ()
{
IntPtr pFileList = IntPtr.Zero;
IntPtr pItem = IntPtr.Zero;
try
{
pFileList = LSSharedFileListCreate (
IntPtr.Zero,
kLSSharedFileListSessionLoginItems,
IntPtr.Zero);
pItem = LSSharedFileListInsertItemURL (
pFileList,
kLSSharedFileListItemLast,
IntPtr.Zero,
IntPtr.Zero,
NSBundle.MainBundle.BundleUrl.Handle,
IntPtr.Zero,
IntPtr.Zero);
}
finally
{
CFRelease (pItem);
CFRelease (pFileList);
}
}
請記住,這不是完整的解決方案,只是將應用程序放入登錄項列表的一小段。 當然,您必須處理錯誤,在每次調用后檢查IntPtr.Zero等,但這應該可以使您了解其工作原理。
希望有幫助!
由於Xamarin.Mac不支持LSSharedFileList庫,因此必須使用Xcode創建dylib,並將其綁定到Xamarin.Mac應用程序中。
1)在Xcode上創建一個Dylib項目。 添加此功能:
-(BOOL)AddLoginItem:(NSString *) AppPath{
// Your have to send this string as argument(i.e: on a textbox, write: /Applications/Calculator.app/)
// Get Login Items
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
NSLog(@"[DEBUG]Your Application Path:%@",AppPath);
// Covert String to CFURLRef (It adds "file://" to your itemUrl1)
CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:(NSString*) AppPath];
NSLog(@"[DEBUG] appUrl:%@", appUrl);
// Now we add the requested Login Item
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
// Confirm that your path was correct and that you got a valid Login Item Reference
NSLog(@"[DEBUG]Item:%@", itemRef);
if (itemRef) CFRelease(itemRef);
// Your item just got added to the user's Login Items List
CFRelease(loginItems);
return true;
}
return false;
}
2)創建一個帶有文本框,按鈕及其動作和出口控件的可可項目。 使用此鏈接可幫助您處理來自Objective-C DLL的C#綁定。
3)在Xamarin.Mac項目的MainWindow.cs中,添加以下代碼並進行必要的更改以適合您的代碼。 不要忘記添加在上一個鏈接上創建的.Net程序集引用,以及您的:using DLLloginItem;。 線。
// Some variables
private string TestItemName;
private bool rem;
// Button Click
partial void AddItemButton (Foundation.NSObject sender) {
LoginItemsDLL loginItem = new LoginItemsDLL();
// Enter this string on your TextBox TxtName /Applications/Calculator.app/
TestItemName = TxtName.StringValue;
rem=loginItem.AddLoginItem(TestItemName);
Console.WriteLine(rem);
}
為了輸入您的應用程序路徑,請使用另一個函數,該函數僅接收應用程序名稱並將其路徑返回到AddLoginItem參數中。 希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.