Docstoc

Customization_Examples

Document Sample
Customization_Examples Powered By Docstoc
					    MobileCRM Customizations Examples
1. Example : Adding a view to the form via code

   -   Interfaces (IDetailView interface) and implementation (DetailView class)

   private IDetailView m_DetailView;

                   protected override void Initialize()
                   {
                          base.Initialize();
                          m_DetailView = this.Form.AddView<IDetailView>("test", "Test
   Detail");
                   }
   -   You can add different type of views (DetailView, ListView, GridView, DocumentView,
       YourOwnView)
   -   You have to add this code into initialize – because each Form is created once and then
       re-used every time you open it (as opposed to load)
   -   Run to see where the detail was added
   -   Return and check the definition of AddView – see how it is defined and how
       AddDetailView and AddFilteredView is defined
   -   Add method for adding a view on a certain spot :

   m_DetailView = (IDetailView)this.Form.CreateView(typeof(IDetailView), "test",
   "Test Detail");
                       this.Form.InsertView(1, m_DetailView);
   -   Run to see how it works

   -   Return and add icon to Images.Platform.Tabs :          and name it test.png
   -   Click Show all files and add the icon to the project
   -   Set the icon to embedded resource
   -   Run to check the icon.



2. Example : Adding items to detail view from code

   -   MAKE SURE THERE IS FAMILY STATUS CODE ON CONTACT IN DEMO DB
   -   Start by adding FamilyStatusCode property in resco.xcrm file to Contact entity
   -   Add method code for adding items to DetailView :

   private IDetailItemTextBox m_TextBox;
                private IDetailItemCheckBox m_CheckBox;

                   private void AddItemsToDetailView(IDetailView view)
                   {
                          m_CheckBox = view.CreateItem<IDetailItemCheckBox>();
                          m_CheckBox.Label = "CheckBox";
                          m_CheckBox.Value = true;
                          m_CheckBox.IsNullable = false;
                          view.AddItem(m_CheckBox, null);
                           m_TextBox = view.CreateItem<IDetailItemTextBox>();
                           m_TextBox.Kind = ItemTextBoxKind.Text;
                           m_TextBox.Label = "TextBox";
                           m_TextBox.Value = "Test";
                           view.AddItem(m_TextBox, null);

                       var combobox = DetailViewBuilder.CreateDetailItem(view,
   "ComboBox", typeof(ContactFamilyStatus), null, false, false);
                       combobox.DataMember =
   Schema.Contact.Properties.FamilyStatusCode;
                       view.InsertItem(1, combobox, null);
                }
   -   At first we are adding a checkbox, manually, not bound to enything.
   -   Similarly we are adding textbox, we have to set all the properties and AddItem to view
   -   Finally, we are adding combobox. We are calling different method, which will set a lot of
       values for us. We are also inserting it after the checkbox.
   -   We append this code at the end of Load :

   m_DetailView.DataSource = this.Entity;

   -   We the event handler in Initialize()


                       m_DetailView.ItemChanged += new
   EventHandler<DetailViewItemArgs>(m_DetailView_ItemChanged);
                       AddItemsToDetailView(m_DetailView);



   We also add code for ItemChanging :

                   void m_DetailView_ItemChanged(object sender, DetailViewItemArgs e)
                   {
                          var item = e.Item;
                          if (item == m_CheckBox)
                          {
                                 m_TextBox.IsVisible = (bool)m_CheckBox.Value;
                          }
                   }
   -   Run to see how it works. It shows new functionality for showing / hiding an Item
       (Textbox)
   -   If you save the contact, you will see that its family status is filled – while we have set the
       DataSource of the detailview to the Contact entity.



3. Example : Adding a signature view to the form

   -   We will add a view (document view) that will show Signatures directly on a form
   -   Add following code to Initialize :

   m_SignatureView = this.Form.AddView<IDocumentView>("signature", "Signature");
   -   Run to see we have already added functional signature taking
   -   Now, we have to add code, that will load the Signature from database or create a new
       one.
-   Signatures are stored as Annotations attachments, so we will do FetchXml query on the
    Annotations.
-   Add following code to Contact.cs

private Annotation m_Signature;
             public Annotation Signature
             {
                    get
                    {
                           if (m_Signature == null)
                           {
                                  // try to get the signature from database
                                  MobileCrm.Data.FetchXml.Fetch fetch = new
Data.FetchXml.Fetch(Schema.Annotation.Name);


         fetch.Entity.AddAttribute(Schema.Annotation.Properties.DocumentBody,
true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.MimeType, true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.FileName, true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.AnnotationId,
true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.ObjectId, true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.Subject, true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.IsDocument, true);

         fetch.Entity.AddAttribute(Schema.Annotation.Properties.FileSize, true);


       fetch.Entity.Filter.Where(Schema.Annotation.Properties.ObjectId,
this.RowGuid);

       fetch.Entity.Filter.Where(Schema.Annotation.Properties.FileName,
"signature.svg");

                                  m_Signature =
MobileCrm.Data.DynamicRepository.ExecuteFetch<Annotation>(fetch,
true).FirstOrDefault();

                                      // signature not stored in database - create
new one
                                  if (m_Signature == null)
                                  {
                                         m_Signature =
Annotation.CreateNew<Annotation>();
                                         m_Signature.MimeType = "image/svg+xml";
                                         m_Signature.FileName = "signature.svg";
                                         m_Signature.Subject = "Singature";
                                         m_Signature.ObjectId = this;
                                  }
                           }

                              return m_Signature;
                       }
                       set
                       {
                               m_Signature = value;
                       }
               }
-   Now we have the Signature stored in a property on Contact entity. Let’s load the
    Singature data into the Document View
-   Add following line at the end of Load method in Contact Form :
-   LoadSignature()
-   And add following method after that :

               private void LoadSignature()
               {
                      var signature = this.Entity.Signature;

                    if (signature != null)
                    {
                           if (!string.IsNullOrEmpty(signature.DocumentBody))
                           {
                                  var input =
Convert.FromBase64String(signature.DocumentBody);
                                  using (var stream = new
System.IO.MemoryStream(input))
                                  {

       m_SignatureView.Load(signature.FileName, signature.MimeType, stream);
                                  }
                           }
                     }
                     else
                     {
                           m_SignatureView.ExecuteAction(DocumentAction.Clear);
                     }
              }



-   Even though we would be able to load the signature from database, we would have to
    save the signature to database first.
-   To do so, add override the save method on ContactForm like this :

protected override void Save()
             {
                    SaveSignature();
                    base.Save();
             }
-
-   Then, add SaveSignature method :

private void SaveSignature()
             {
                    var signature = this.Entity.Signature;

                       if (!m_SignatureView.IsEmpty)
                       {
                              using (var stream = new System.IO.MemoryStream())
                              {
                                     m_SignatureView.Save(stream);

                                       var documentBody = stream.ToArray();
                                     signature.FileSize = documentBody.Length;
                                     signature.DocumentBody =
   Convert.ToBase64String(documentBody);
                              }
                       }
                }
   -   The final step for this to take effect is to set when the Contact entity is Diry (and needs to
       be saved)

   protected override bool IsDirty
                {
                       get
                       {
                              return base.IsDirty || this.m_SignatureView.IsDirty;
                       }
                       set
                       {
                              this.m_SignatureView.IsDirty = value;
                              base.IsDirty = value;
                       }
                }
   -
   -   Of course when the Contact is dirty, we have to make sure it saves also the signature.
   -   So we will override the method SaveChildren on Contact.cs

   protected override void SaveChildren(bool recursive)
                {
                       base.SaveChildren(recursive);

                           if( m_Signature != null )
                                  m_Signature.Save();
                   }
   -   Run it, to see everything working.



4. Example : Creating custom control.

   -   We will create a custom control – a simple progress view
   -   At first we start by implementing an interface – this interface will be used for
       comunication with our custom control troughout all platforms
   -   Create new file IProgess view in Resco.UI folder

   namespace Resco.UI
   {
          /// <summary>
          /// Progress view interface.
          /// </summary>
          public interface IProgressView
          {
                 /// <summary>
                 /// Sets progress bar to a percentual value.
                 /// </summary>
                 /// <param name="percent">Percentual value.</param>
                 void SetProgress(int percent);
                 /// <summary>
                 /// Sets title.
                 /// </summary>
               /// <param name="title">A title.</param>
               /// <remarks>Title is drawn above progress bar.</remarks>
               void SetTitle(string title);
               /// <summary>
               /// Sets message.
               /// </summary>
               /// <param name="message">A message.</param>
               /// <remarks>Message is drawn under progress bar.</remarks>
               void SetMessage(string message);
        }
}
-   Now we will start with Windows Tablet version and will create a Windows Tablet
    Progress control
-   Add file ProgressView.cs to Resco.UI.Windows/Controls folder

using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;

namespace Resco.UI.WindowsMobile
{
       public class ProgressView : Panel, IProgressView
       {
              private ProgressBar m_progress;
              private Label m_percentText;
              private Label m_label;

               public ProgressView()
               {

                      this.m_label = new System.Windows.Forms.Label();
                      this.m_progress = new System.Windows.Forms.ProgressBar();
                      this.m_percentText = new System.Windows.Forms.Label();
                      this.SuspendLayout();

                      //
                      // content
                      //
                      this.BackColor = ListStyle.Colors.ListBackground;
                      this.Controls.Add(this.m_percentText);
                      this.Controls.Add(this.m_progress);
                      this.Controls.Add(this.m_label);

                      this.m_label.Font = new Font("Tahoma", 12F,
FontStyle.Bold);
                      this.m_label.Bounds = new Rectangle(0, 43, 240, 35);
                      this.m_label.Bounds = new System.Drawing.Rectangle(3, 78,
237, 30);
                      this.m_label.Bounds = new System.Drawing.Rectangle(3, 108,
237, 30);

                    this.m_progress.Bounds = new Rectangle(3, 201, 190, 20);
                    //
                    // m_percentText
                    //
                    this.m_percentText.Font = new System.Drawing.Font("Tahoma",
9F, System.Drawing.FontStyle.Bold);
                       this.m_percentText.Bounds = new Rectangle(195, 203, 43,
20);
                    this.m_percentText.Text = "0%";
                    this.m_percentText.TextAlign =
System.Drawing.ContentAlignment.TopRight;

                       //
                       this.ResumeLayout(false);
               }



               public void SetProgress(int percent)
               {
                      if (m_progress.Value != percent)
                      {
                             m_progress.Value = percent;
                             m_progress.Update();

                               int disp;
                               if (m_progress.Maximum < 10000)
                                      disp = percent * 100 / m_progress.Maximum;
                               else
                                      disp = percent / (m_progress.Maximum / 100);

                               this.m_percentText.Text = string.Format("{0}%",
disp);

                       }
               }

               public void SetTitle(string title)
               {
                      this.m_label.Text = title;
               }

               public void SetMessage(string message)
               {
                      throw new NotImplementedException();
               }
         };
}
-
-   It is simple WindowsForms code – you can utilize platform controls here
-   Now we have control and interface, but we need to be able to put the control on a form
-   In the BaseForm.cs in method CreateViewInstance add as another type of views this :

else if (viewType == typeof(IProgressView))
                           view = new ProgressView();
-   Now when we add following code to the end of ContactForm Initialize method

m_ProgressView = this.Form.AddView<IProgressView>("progress", "Progress");
                    m_ProgressView.SetProgress(50);
                    m_ProgressView.SetTitle("This is my progress bar.");
-   We will be able to see the progress on the form as a separate tab
-   Run the application to see the result.
-   Now we will create Android version of the same control.
-   While we were working with a W7 version and this one does not have a template, we will
    add the files to the project manually and you will see that the contvertors are not really
    that necessary
-   Open MonoDroid project
-   Click show all files and include in the project IProgressView.cs
-   Now add new ProgressView.cs file to the Controls folder of
    Resco.UI.MonoDroid.MonoDroid project

using System;
using Android.Content;
using Android.Widget;

namespace Resco.UI.MonoDroid
{
       public class ProgressView : LinearLayout, IProgressView, INamedView
       {
              #region Private Fields and Constants
              private const int TitleViewId = 1;
              private const int ProgressBarId = 2;
              private const int MessageViewId = 3;
              #endregion

               #region Constructor
               /// <summary>
               /// Creates and initializes new <see cref="ProgressView"/>
instance.
               /// </summary>
               /// <param name="context"></param>
               public ProgressView(Context context)
                      : base(context)
               {
                      Initialize();
               }

               private void Initialize()
               {
                      this.SetWillNotDraw(false);
                      this.Orientation = Android.Widget.Orientation.Vertical;

                    var r = 10f *
this.Context.Resources.DisplayMetrics.Density;
                    var shape = new
Android.Graphics.Drawables.Shapes.RoundRectShape(new float[] { r, r, r, r, r,
r, r, r }, null, null);
                    var bkgDr = new
Android.Graphics.Drawables.ShapeDrawable(shape);
                    bkgDr.Paint.Color = Android.Graphics.Color.Rgb(214, 232,
255);

                       SetBackgroundDrawable(bkgDr);

                       var titleView = new TextView(this.Context) { Id =
TitleViewId, };
                    titleView.SetSingleLine();
                    titleView.SetTextColor(Android.Graphics.Color.Black);
                    titleView.SetTextSize(0, 20f *
this.Context.Resources.DisplayMetrics.ScaledDensity);

       titleView.SetTypeface(Android.Graphics.Typeface.DefaultBold, 1);
                     titleView.SetPadding((int)r, (int)r, (int)r, (int)r);
                    AddView(titleView, new
Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.FillP
arent, Android.Views.ViewGroup.LayoutParams.WrapContent));

                    var progressBar = new ProgressBar(this.Context, null,
Android.Resource.Attribute.ProgressBarStyleHorizontal) { Id = ProgressBarId,
Indeterminate = false, Max = 100, };
                    progressBar.SetPadding((int)r, 0, (int)r, 0);
                    AddView(progressBar, new
Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.FillP
arent, Android.Views.ViewGroup.LayoutParams.WrapContent));

                    var messageView = new TextView(this.Context) { Id =
MessageViewId, };
                    messageView.SetTextColor(Android.Graphics.Color.Black);
                    messageView.SetTextSize(0, 12f *
this.Context.Resources.DisplayMetrics.ScaledDensity);
                    messageView.SetTypeface(Android.Graphics.Typeface.Default,
0);
                    messageView.SetPadding((int)r, (int)r, (int)r, (int)r);
                    AddView(messageView, new
Android.Views.ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.FillP
arent, Android.Views.ViewGroup.LayoutParams.WrapContent));
             }
             #endregion

             #region IProgressView
             /// <summary>
             /// Sets progress bar to a percentual value.
             /// </summary>
             /// <param name="percent">Percentual value.</param>
             public void SetProgress(int percent)
             {
                    var progressBar = FindViewById<ProgressBar>(ProgressBarId);
                    if (progressBar != null)
                           progressBar.Progress = percent;
             }
             /// <summary>
             /// Sets title.
             /// </summary>
             /// <param name="title">A title.</param>
             /// <remarks>Title is drawn above progress bar.</remarks>
             public void SetTitle(string title)
             {
                    var titleView = FindViewById<TextView>(TitleViewId);
                    if (titleView != null)
                           titleView.Text = title;
             }
             /// <summary>
             /// Sets message.
             /// </summary>
             /// <param name="message">A message.</param>
             /// <remarks>Message is drawn under progress bar.</remarks>
             public void SetMessage(string message)
             {
                    var messageView = FindViewById<TextView>(MessageViewId);
                    if (messageView != null)
                           messageView.Text = message;
             }
             #endregion

             public string Name
               {
                       get;
                       set;
               }

               public string Label
               {
                      get;
                      set;
               }
       }
}
-
-   Again, we have to add code for adding the view to the form in BaseForm.cs in
    CreateView method – this time it is the BaseForm for Android

else if (viewType == typeof(IProgressView))
                           view = new ProgressView(this.Context);



-   We don’t have to add anything to ContactForm because this is already added to the main
    project
-   So we can run it and see the result

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:8/27/2012
language:
pages:10