Back To .NET Standard Tutorials

SpreadsheetGear Engine for .NET Tutorials

Xamarin.iOS Single View App - Spreadsheet Calculations using Xamarin Studio for Mac

Follow these steps to create a simple Xamarin.iOS Single View App for iPhone/iPad with Xamarin Studio for Mac that utilizes SpreadsheetGear Engine for .NET to perform a basic formula calculation.

Create a new iOS Single View App

  1. Launch Xamarin Studio.
  2. On the File menu, navigate to New Solution...
  3. Select App under the iOS Category on the left.
  4. Under the General section, select "Single View App" and click Next.
  5. On the next screen, provide an App Name of "SGPhoneApp" and customize the other options as desired. Click Next.
  6. Provide additional details on the next screen as desired and click Create.

Add SpreadsheetGear Engine for .NET to your project

  1. On the Project menu, select Add NuGet Packages... to launch the Add Packages dialog.
  2. Search for "SpreadsheetGear" in the search box.
  3. In the list of NuGet packages, select "SpreadsheetGear Engine for .NET" and click Add Package.
  4. You will be prompted to accept SpreadsheetGear's License Agreement as well as License Agreements of other Microsoft packages that SpreadsheetGear Engine for .NET depends on. Click Accept if you wish to proceed.
  5. Xamarin Studio should begin the installation process and add a reference to SpreadsheetGear in your Project's References folder, under From Packages.

Create iOS App's UI

  1. From the Solution Pad, open Main.storyboard.
  2. Search for "button" in the Toolbox Pad's search box, then drag and drop a Button Control near the upper-left corner of the Main.storyboard design surface.
  3. Give the Button Control a name of "EarthDiameterButton" from the Properties Pad under Widget → Identity → Name.
  4. Give the Button Control a title of "Calculate Earth's Diameter" from the Properties Pad under Widget → Button → Title. Increase the width of the Button to fit the new Title text (approximately 200).
  5. Search for "text field" in the Toolbox Pad's search box, then drag and drop a Text Field Control below the Button Control added in the previous step.
  6. Give the Text Field Control a name of "EarthDiameter" from the Properties Pad under Widget → Identity → Name. Also increase the width of the Text Field to approximately 200.
  7. Save your changes by selecting Save All on the File menu.

Manually add System.Runtime

  1. You must manually add a reference to System.Runtime when using a .NET Standard NuGet package in a Xamarin.iOS project in Xamarin Studio.
  2. In the Solution Pad, right-click on the "SGPhoneApp" project and select Reveal in Finder.
  3. Locate and right-click "SGPhoneApp.csproj", then select Open With → Other... →
  4. Locate an "<ItemGroup>" section of "SGPhoneApp.csproj" that contains a number of <Reference Include="..."> elements. It should look something like the following, where the highlighted System.Runtime reference needs to be manually added (be sure to add "straight" double quotes and not slanted quotes around System.Runtime):
    <Reference Include="System" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Core" />
    <Reference Include="Xamarin.iOS" />
    <Reference Include="System.IO.Compression" />
    <Reference Include="System.Net.Http" />
    <Reference Include="SpreadsheetGear" />
    <Reference Include="System.Runtime" />
  5. Save SGPhoneApp.csproj and close the text editor.
  6. Switching back to Xamarin Studio should prompt you to reload SGPhoneApp because it detected a change from an external application. Click Reload.

Add Button event handler and SpreadsheetGear calculations

  1. Double-click on the EarthDiameterButton Button Control, which should open ViewController.cs and prompt you to choose where to add an even handler. Choose the desired location and hit Enter.
  2. Replace the event handler's code with the following:
    partial void EarthDiameterButton_TouchUpInside(UIButton sender)
        // Create a new empty workbook in a new workbook set.
        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
        // Get a reference to the first worksheet.
        SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
        // Get a reference to the top left cell of Sheet1.
        SpreadsheetGear.IRange a1 = worksheet.Cells["A1"];
        // Set a formula.
        a1.Formula = "=24901.55 / PI()";
        EarthDiameter.Text = a1.Value + " miles";

Build and run the application

  1. From the Run menu, select Start Without Debugging.
  2. An iPhone emulator should launch, as well as your SGPhoneApp.
  3. Clicking on the "Calculate Earth's Diameter" should result in a calculated value in the Text Field of:
    7926.40954629997 miles