Xamarin Ma c добавить папку на боковую панель Finder - PullRequest
0 голосов
/ 22 апреля 2020

Мне нужно добавить папку на боковую панель Finder с помощью приложения, разработанного Xamarin. У меня уже есть код выполнения этого из Objective- C, но преобразование его в C# / Xamarin не выглядит легким.

Есть ли способ достичь этого с помощью следующего кода или есть другие способы? В любом случае помогите мне преобразовать код obj- c в C# / Xamarin.

+ (void)addItemNameToFinderSideBar:(NSString *)name
                          path:(NSString *)path
                         retry:(BOOL)retry{
NSURL *url = [NSURL fileURLWithPath:path];

CFStringRef itemName = (__bridge CFStringRef) name;

if (url) {
    CFURLRef urlRef = (__bridge CFURLRef)(url);

    // Create a reference to the shared file list.
    LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                               kLSSharedFileListFavoriteItems, NULL);

    if (favoriteItems) {
        BOOL foundItem = NO;

        AuthorizationRef auth = NULL;
        AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth);
        LSSharedFileListSetAuthorization(favoriteItems, auth);

        if (!foundItem) {
            //Insert an item to the list.
            LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems,
                                                                         kLSSharedFileListItemBeforeFirst, itemName, NULL,
                                                                         urlRef, NULL, NULL);
            if (item){
                CFRelease(item);
            } else {


                LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems,
                                                                             kLSSharedFileListItemBeforeFirst, itemName, NULL,
                                                                             urlRef, NULL, NULL);
                if (item){
                    CFRelease(item);
                } else {

                      [[self class] addItemNameToFinderSideBar:name
                                                            path:path
                                                           retry:NO];
                    } 
                }
            }

        }
        if (favoriteItems){
            CFRelease(favoriteItems);
        }
    }
}
}


+ (void)removeSideBarItemAtPath:(NSString*)inPath{
// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
                                                           kLSSharedFileListFavoriteItems, NULL);
UInt32 seedValue = 0;
CFArrayRef favoriteItemsArray = LSSharedFileListCopySnapshot(favoriteItems, &seedValue);

LSSharedFileListItemRef foundItem = nil;

for (int i = 0; i< CFArrayGetCount(favoriteItemsArray); i++){
    LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(favoriteItemsArray,i);
    CFURLRef itemRefUrl = nil;

    //Resolve the item with URL
    if (LSSharedFileListItemResolve(itemRef, kLSSharedFileListDoNotMountVolumes, &itemRefUrl, NULL) == noErr) {

        NSString * urlPath = [(__bridge_transfer NSURL*)itemRefUrl path];

        if ([urlPath compare:inPath] == NSOrderedSame) {
            foundItem = itemRef;
            break;
        }
    }
}

if (foundItem){

    OSStatus status =  LSSharedFileListItemRemove(
                                     favoriteItems,
                                        foundItem);
}
CFRelease(favoriteItemsArray);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...