Advertisement
onzulin

mi programa metro style

Jun 7th, 2012
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. private async void ButtonSave_Click(object sender, RoutedEventArgs e)
  2.         {
  3.  
  4.            
  5.             if (TextBoxName.Text.Length <= 2)
  6.             {
  7.                 MessageDialog msg = new MessageDialog("Names don't allow 2 or less characters", "Information");
  8.                 await msg.ShowAsync();
  9.                 return;
  10.             }
  11.  
  12.             //aqui introduce los datos de los textbox
  13.             //instanciamos la clase Activity
  14.             Activity Activity = new Activity();
  15.             Activity.Name = TextBoxName.Text;
  16.             Activity.Participant = TextBoxParticipant.Text;
  17.             //Activity.Map =
  18.             Activity.Description = TextBoxDescription.Text;
  19.             Activity.Type = TextBoxType.Text;
  20.             Activity.Price = TextBoxPrice.Text;
  21.             Activity.Difficulty = TextBoxDifficulty.Text;
  22.             Activity.Comments = TextBoxComment.Text;
  23.            
  24.             //serializar el objeto antes de salvarlo con el nombre que queremos
  25.            
  26.            
  27.             // aqui ya hacemos el proceso de salvado de datos
  28.             FileSavePicker fileSavePicker = new FileSavePicker();
  29.             //FileSavePicker savePicker = new FileSavePicker();
  30.             fileSavePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
  31.             // Dropdown of file types the user can save the file as
  32.             fileSavePicker.FileTypeChoices.Add("Activities Documents", new List<string>() { ".act" });
  33.             fileSavePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
  34.             // Default extension if the user does not select a choice explicitly from the dropdown
  35.             fileSavePicker.DefaultFileExtension = ".docx";
  36.             // Default file name if the user does not type one in or select a file to replace
  37.             fileSavePicker.SuggestedFileName = "New Document";
  38.             StorageFile savedFile = await fileSavePicker.PickSaveFileAsync();
  39.             string savedFilePath = savedFile.Path;
  40.             string savedFileName = savedFile.Name;
  41.            
  42.             // Get the output stream for the SessionState file.
  43.             savedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(savedFileName, CreationCollisionOption.ReplaceExisting);
  44.             IRandomAccessStream raStream = await savedFile.OpenAsync(FileAccessMode.ReadWrite);
  45.             //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("samplefile.dat", CreationCollisionOption.ReplaceExisting);
  46.             //IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
  47.  
  48.             IOutputStream outStream = raStream.GetOutputStreamAt(0);
  49.  
  50.             // Serializar objeto a fichero
  51.             //DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), new DataContractSerializerSettings() { PreserveObjectReferences = true });
  52.             DataContractSerializer serializar = new DataContractSerializer(typeof(Dictionary<string, object>), new DataContractSerializerSettings() { PreserveObjectReferences = true });
  53.             try
  54.             {
  55.                 serializar.WriteObject(outStream.AsStreamForWrite(), Activity);
  56.             }
  57.             catch (Exception ex)
  58.             {
  59.                 MessageDialog msg = new MessageDialog(ex.Message);
  60.             }
  61.             await outStream.FlushAsync();
  62.  
  63.         } // fin metodo ButtonSave_Click
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement