コード例 #1
0
ファイル: LibVLC.cs プロジェクト: weinre/libvlcsharp
        /// <summary>
        /// Register callbacks in order to handle VLC dialogs.
        /// LibVLC 3.0.0 and later.
        /// </summary>
        /// <param name="error">Called when an error message needs to be displayed.</param>
        /// <param name="login">Called when a login dialog needs to be displayed.
        /// You can interact with this dialog by calling Dialog.PostLogin() to post an answer or Dialog.Dismiss() to cancel this dialog.</param>
        /// <param name="question">Called when a question dialog needs to be displayed.
        /// You can interact with this dialog by calling Dialog.PostLogin() to post an answer or Dialog.Dismiss() to cancel this dialog.</param>
        /// <param name="displayProgress">Called when a progress dialog needs to be displayed.</param>
        /// <param name="updateProgress">Called when a progress dialog needs to be updated.</param>
        public void SetDialogHandlers(DisplayError error, DisplayLogin login, DisplayQuestion question,
                                      DisplayProgress displayProgress, UpdateProgress updateProgress)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }
            if (question == null)
            {
                throw new ArgumentNullException(nameof(question));
            }
            if (displayProgress == null)
            {
                throw new ArgumentNullException(nameof(displayProgress));
            }
            if (updateProgress == null)
            {
                throw new ArgumentNullException(nameof(updateProgress));
            }

            var dialogCbs = new DialogCallbacks(
                displayError: (data, title, text) => error(title, text),
                displayLogin: (data, id, title, text, username, store) =>
            {
                var cts  = new CancellationTokenSource();
                var dlg  = new Dialog(new DialogId(id));
                _cts[id] = cts;
                login(dlg, title, text, username, store, cts.Token);
            },
                displayQuestion: (data, id, title, text, type, cancelText, firstActionText, secondActionText) =>
            {
                var cts  = new CancellationTokenSource();
                var dlg  = new Dialog(new DialogId(id));
                _cts[id] = cts;
                question(dlg, title, text, type, cancelText, firstActionText, secondActionText, cts.Token);
            },
                displayProgress: (data, id, title, text, indeterminate, position, cancelText) =>
            {
                var cts  = new CancellationTokenSource();
                var dlg  = new Dialog(new DialogId(id));
                _cts[id] = cts;
                displayProgress(dlg, title, text, indeterminate, position, cancelText, cts.Token);
            },
                cancel: (data, id) =>
            {
                if (_cts.TryGetValue(id, out var token))
                {
                    token.Cancel();
                    _cts.Remove(id);
                }
            },
                updateProgress: (data, id, position, text) =>
            {
                var dlg = new Dialog(new DialogId(id));
                updateProgress(dlg, position, text);
            });

            _dialogCbsPtr = Marshal.AllocHGlobal(MarshalUtils.SizeOf(dialogCbs));
            Marshal.StructureToPtr(dialogCbs, _dialogCbsPtr, true);
            Native.LibVLCDialogSetCallbacks(NativeReference, _dialogCbsPtr, IntPtr.Zero);
        }