예제 #1
0
 private async void OpenOwnEndpoint_OnClick(object sender, RoutedEventArgs e)
 {
     this.OpenOwnEndpoint.IsEnabled = false;
     this.OpenOwnEndpoint.Cursor    = Cursors.AppStarting;
     try {
         var  dialog = new OpenFileDialog();
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             using (var fileStream = dialog.OpenFile()) {
                 var reader           = new BinaryReader(fileStream, Encoding.UTF8);
                 var addressBookEntry = reader.DeserializeDataContract <Uri>();
                 await this.SetEndpointAsync(await OwnEndpoint.OpenAsync(fileStream), addressBookEntry);
             }
         }
     } finally {
         this.OpenOwnEndpoint.Cursor    = Cursors.Arrow;
         this.OpenOwnEndpoint.IsEnabled = true;
     }
 }
예제 #2
0
        /// <summary>
        /// Creates a new local endpoint to identify the user, or opens a previously created one.
        /// </summary>
        /// <returns>A task whose result is the local user's own endpoint.</returns>
        private async Task <OwnEndpoint> CreateOrOpenEndpointAsync()
        {
            OwnEndpoint result;

            switch (MessageBox.Show("Do you have an existing endpoint you want to open?", "Endpoint selection", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            {
            case DialogResult.Yes:
                var openFile = new OpenFileDialog();
                if (openFile.ShowDialog() == DialogResult.Cancel)
                {
                    result = null;
                    break;
                }

                using (var fileStream = openFile.OpenFile())
                {
                    result = await OwnEndpoint.OpenAsync(fileStream);
                }

                break;

            case DialogResult.No:
                result = await this.OwnEndpointServices.CreateAsync();

                string privateFilePath = Path.GetTempFileName();
                using (var stream = File.OpenWrite(privateFilePath))
                {
                    await result.SaveAsync(stream);
                }

                Console.WriteLine("Private receiving endpoint: \"{0}\"", privateFilePath);
                break;

            default:
                result = null;
                break;
            }

            return(result);
        }