Esempio n. 1
0
        public static SerializableActivatedEventArgs Serialize(IActivatedEventArgs activatedEventArgs)
        {
            SerializableActivatedEventArgs serializableActivatedEventArgs = null;

            switch (activatedEventArgs)
            {
            case IProtocolActivatedEventArgs protocolActivatedEventArgs:
                serializableActivatedEventArgs = new SerializableProtocolActivatedEventArgs()
                {
                    Kind = activatedEventArgs.Kind,
                    PreviousExecutionState = activatedEventArgs.PreviousExecutionState,
                    SplashScreen           = activatedEventArgs.SplashScreen,
                    Uri = protocolActivatedEventArgs.Uri
                };
                break;

            case IFileActivatedEventArgs fileActivatedEventArgs:
                IList <IStorageItem> serializableFiles =
                    fileActivatedEventArgs.Files?
                    .Select <IStorageItem, IStorageItem>(x => new SerializableStorageItem()
                {
                    Attributes  = x.Attributes,
                    DateCreated = x.DateCreated,
                    Name        = x.Name,
                    Path        = x.Path
                })
                    .ToList();

                serializableActivatedEventArgs = new SerializableFileActivatedEventArgs()
                {
                    Kind = activatedEventArgs.Kind,
                    PreviousExecutionState = activatedEventArgs.PreviousExecutionState,
                    SplashScreen           = activatedEventArgs.SplashScreen,
                    Verb  = fileActivatedEventArgs.Verb,
                    Files = serializableFiles != null ? new ReadOnlyCollection <IStorageItem>(serializableFiles) : null
                };
                break;

            default:
                serializableActivatedEventArgs = new SerializableProtocolActivatedEventArgs()
                {
                    Kind = activatedEventArgs.Kind,
                    PreviousExecutionState = activatedEventArgs.PreviousExecutionState,
                    SplashScreen           = activatedEventArgs.SplashScreen
                };
                break;
            }
            return(serializableActivatedEventArgs);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            IActivatedEventArgs activatedEventArgs = AppInstance.GetActivatedEventArgs();

            using (Mutex mutex = new Mutex(false, AppUniqueGuid))
            {
                if (mutex.WaitOne(0, false))
                {
                    new Thread(CreateNamedPipeServer)
                    {
                        IsBackground = true
                    }
                    .Start();

                    s_application = new App();
                    s_application.InitializeComponent();
                    if (activatedEventArgs != null)
                    {
                        s_application.OnProtocolActivated(activatedEventArgs);
                    }
                    s_application.Run();
                }
                else if (activatedEventArgs != null)
                {
                    //instance already running
                    using (NamedPipeClientStream namedPipeClientStream
                               = new NamedPipeClientStream(NamedPipeServerName, AppUniqueGuid, PipeDirection.Out))
                    {
                        try
                        {
                            namedPipeClientStream.Connect(s_connectionTimeout);
                            SerializableActivatedEventArgs serializableActivatedEventArgs = Serializer.Serialize(activatedEventArgs);
                            s_formatter.Serialize(namedPipeClientStream, serializableActivatedEventArgs);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
        }