3 Replies Latest reply on Aug 3, 2018 12:01 AM by Darryn Campbell

    How to create a profile in Xamarin Forms, take 2

    Karen Cate

      Hello!

       

      Way back in April I tried to figure out how to automatically configure the profile to set up basic text formatting output. That thread is here: How to create a profile in Xamarin Forms?

       

      Since the configuration was relatively simple, I left that task until last, and I am just now getting to implementing the code. After three days of searching and trying, I am not able to get auto import to work in any way, shape, or form. I have tried both a profile, and a configuration. I used the same names that were created by the export. I've tried at least three methods of copying the file. Nothing has worked. Help?

       

      Here's my c# code:

       

      using System;
      using System.IO;
      using Java.IO;
      using System.Threading.Tasks;
      using Xamarin.Forms;
      using Android.Views;
      using Android.Content.Res;
      using Plugin.CurrentActivity;
      using Acr.UserDialogs;

       

      ...

       

              private bool setuptask()
              {

                  // Somewhere I saw a recommendation to not name the asset with a "known" suffix, use

                  // something like .dat instead. So I tried that...
                  string importpath = "/enterprise/device/settings/datawedge/autoimport/";
                  string tempfile = "/enterprise/device/settings/datawedge/datawedge.dat";
                  string importfile = "/enterprise/device/settings/datawedge/autoimport/datawedge.db";
                  string otherfile = "/enterprise/device/settings/datawedge/autoimport/dwprofile_ScanOrders.db";
                  string assetname = "datawedge.dat";
                  string filename = "datawedge.db";

                  try
                  {
                      // Make sure directory exists...
                      if (Directory.Exists(importpath) == false)
                      {
                          Directory.CreateDirectory(importpath);
                      }

                      // Clear out any old files... I have been switching back and forth

                      // between the two files, so I clean up both of them before re-copying.
                      if (System.IO.File.Exists(tempfile) == true)
                      {
                          System.IO.File.Delete(tempfile);
                      }

                      if (System.IO.File.Exists(otherfile) == true)
                      {
                          System.IO.File.Delete(otherfile);
                      }

       

                      // If I set a breakpoint here. The second time I call the function,

                      // the file exists. (I wait a minute or two before the 2nd call.)
                      if (System.IO.File.Exists(importfile) == true)
                      {
                          System.IO.File.Delete(importfile);
                      }

                      // Create temp file from our assets
                      Android.App.Activity context = CrossCurrentActivity.Current.Activity;
                      AssetManager assets = context.Assets;

                      using (BinaryReader br = new BinaryReader(assets.Open(assetname)))
                      {
                          using (BinaryWriter bw = new BinaryWriter(new FileStream(tempfile, FileMode.Create)))
                          {
                              byte[] buffer = new byte[2048];
                              int len = 0;
                              while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
                              {
                                  bw.Write(buffer, 0, len);
                              }
                          }
                      }

                    

                      // Move the file into place so auto import doesn't try to pick it up before

                      // the copy is complete.
                      System.IO.File.Move(tempfile, importfile);

               

                      // Set permissions, in case that was the problem. I was setting executable, but

                      // saw example code that the author claimed worked, and he only set readable and

                      // writable. So I took it back out.
                      using (var newfile = new Java.IO.File(importpath, filename))
                      {

                          // I added this bit so I could check that the file is actually

                          // created. I don't know if the contents are correct, but the copied

                          // file is ~9000 bytes long.
                          long len = newfile.Length();
                          if ((newfile.Exists() == false) || (len < 1.0))
                          {
                              // Error                   
                          }
                          else
                          {
                              newfile.SetReadable(true);
                              newfile.SetWritable(true);
                          }
                      }
                  }
                  catch (Exception e)
                  {
                      UserDialogs.Instance.Alert("Exception caught: " + e.Message, "Error");
                  }

                  return true;
              }