Tag Archives: MVVM

Video Tutorial: Creating Your First Crosslight App

For those of you new to cross-platform development, fear not. Crosslight is here to the rescue. Building cross-platform apps has never been easier using Crosslight. Crosslight is a cross-platform toolset that helps you to create cross-platform apps using C#, leveraging the MVVM pattern. If you haven’t got a copy of Crosslight, grab one here!

To help you get started, we have just released a new video tutorial that will help you to create your first Crosslight app using the Crosslight Project Wizard and run the project on all four platforms: iOS, Android, Windows Phone and Windows Store.

Find out more about Crosslight at our Developer Center. Also checkout our newbie-proof Crosslight tutorial. If you have any questions, feel free to post it in our community forum, or drop us a mail.

Advertisements

Video Tutorial: Getting Started with Crosslight Reporting Service

Crosslight 2 provides a capability to view enterprise reports across multiple platforms including iOS, Android, Windows Phone and Windows 8. The reporting services is built upon the company’s flagship reporting, ClientUI Reporting, to produce identical reports across multiple platforms. You can quickly add reporting feature to your apps since Crosslight has encapsulated all of the functionality required to display the reports on multiple platforms natively. Check out the following video to learn how to configure the report server and display the report on Crosslight apps, which runs on four major platforms: iOS, Android, Windows Phone 8 and Windows Store.

You can also get the source code for this video from our new Git server: http://git.intersoftpt.com/projects/CROS-SUPP/repos/simple-reporting/browse. To learn more about Crosslight reporting service and how it works, see Viewing Enterprise Reports. If you have any questions, feel free to post it in our community forum, or drop us a mail.

Advanced Batch Update with MVVM support in Crosslight iOS

If you are iOS users, you might already familiar with the Mail app. When new messages arrived, notice that the new items are inserted smoothly with beautiful animation. The implementation for such insertion is in deed challenging since it needs to take account the actual insertion position based on the sorted data. Furthermore, multiple insertions are also supported with simultaneous animation which adds complexity to the logic. Technically, inserting an item will cause the index of remaining items invalid — and not to mention if there are deletion along the process. There are a lot of aspects that need to be considered. In this blog post, I will share how we support this user experience seamlessly in Crosslight 2.

iOS supports this simultaneous updates through a feature called batch updating. Implementing this feature requires deep knowledge of how iOS works — and typically what you want to avoid, particularly if your code is targeting multiple platforms. Thankfully, Crosslight has supported this batch update operation since its first version by allowing you to simply perform updates to your collection in .NET way. You don’t need to know about iOS even a bit.

Recently, we made several improvements to Crosslight for batch update support in a sorted collection. This allows you to simply adding items or deleting items on a sorted collection without concerning the actual position where it will be displayed or removed. The Crosslight’s table view component now takes care everything behind the scene, including the batch updating process, maintaining the delta changes and simultaneously animate the changes.

Consider you have a raw collection of countries: US, China, Japan, Netherlands, and Greece. When bound to the view, the collection is sorted by the country name in ascending with LINQ. At runtime, you then add few more countries: Indonesia, Brazil, and Mexico.

In ViewModel, the C# code looks like below.

public class BatchUpdateListViewModel : EditableListViewModelBase<Country>
{
    public DelegateCommand BatchUpdateCommand { get; set; }

    public BatchUpdateListViewModel()
    {
        var items = new List<Country>();
        items.AddRange(Countries.Create("US", "China", "Japan", "Netherlands", "Greece"));

        // just a plain .NET collection here
        this.SourceItems = items.ToObservable();

        this.BatchUpdateCommand = new DelegateCommand(ExecuteBatchUpdate);
    }

    private void ExecuteBatchUpdate(object parameter)
    {
        // Begin batch updating
        this.IsBatchUpdating = true;

        // Perform multiple inserts simultaneously to the source items
        this.SourceItems.Add(new Country("Indonesia"));
        this.SourceItems.Add(new Country("Brazil"));
        this.SourceItems.Add(new Country("Mexico"));

        // End batch updating
        // After this code, the bound list view will be automatically updated with smooth animation
        this.IsBatchUpdating = false;
    }

    protected override void OnSourceItemsChanged(ICollection<Country> items)
    {
        // Sort the source items by country name, and use the sorted collection for the display items
        this.Items = items != null ? items.OrderBy(country => country.Name) : null;
    }
}

Notice that the above code simply adds items to the source collection, you don’t have to concern the actual positions in the view. The only thing you need to ensure is to perform the collection changes while the IsBatchUpdating property value is true. As soon as the property is set to false, the bound view will be automatically notified and perform the animation simultaneously. This simple and intuitive code lets you to achieve the stunning user experience similar to Apple’s built-in Mail app when new items are added dynamically at runtime.

The following illustration shows what happened to the sorted collection at runtime.

BatchUpdateIllustration

To demonstrate this feature, we have added a new Sorted Batch Update to the Data Samples which you can download from our Git server. The bits is under the batch-update-sorted branch. You’ll also need nightly build (revision 54 or higher) to try out this new improvement.

In addition to multiple insertions, the sample also demonstrates multiple deletion. And to add more complexity, it also modifies the items which cause its order to change. For your convenience, you can see how the sample works below.

Improved batch update support on sorted collection

 

Most cross-platform frameworks I found today provide just a set of API without thoughtful consideration of the user experience that native to the platform. That’s where Crosslight sets apart. More than just a set of API, we designed Crosslight with the simplicity you expected, while taking account many other important factors behind the scene which will make your apps shine.

Join us for a 3-day live webinars on June 10 – 12, and learn how you can build great cross-platform business apps with Crosslight. We will be demo’ing how to create a fully-functional CRM app in just mere minutes — live. Seat is limited, so reserve yours now.

Video Tutorials: Understanding MVVM Pattern and Building Simple Tip Calculator App with Crosslight

We have released two new video tutorials on Crosslight to help you better understand how to develop better mobile applications using Crosslight.

Understanding MVVM Pattern in Crosslight

This video tutorial outlines in general how the MVVM pattern works in Crosslight apps development. You will learn how Crosslight leverages the MVVM concept in order to produce cross-platform projects with a single shared application layer. By the end of this video, you will be able to grasp the MVVM pattern in general as well as getting the right mindset when building apps using Crosslight.

Building Simple Tip Calculator using Crosslight

This video tutorial explains how you can build a simple tip calculator app using Crosslight  within minutes that target all four major platforms at once (Android, iOS, Windows Phone, Windows 8). This video aims to highlight the data binding capabilities in Crosslight that leverages the MVVM pattern found in .NET applications to mobile apps development. You can find the source code for this video in our GitHub link: https://github.com/IntersoftSolutions/CrosslightSimpleTipCalculator

Should you have any further questions, feel free to contact us at technical@intersoftpt.com or raise a thread in our forums. Hopefully this video will give you a clearer concept how Crosslight works and how you can slash development time in more than half and increase your productivity. Stay tuned for more Crosslight video tutorials. Don’t forget to subscribe to our YouTube channel as well!

Cheers,
Nicholas Lie

Cross Platform Mobile Development with Crosslight – Part 2

This is the second post of cross-platform mobile development with Crosslight series. Continuing from the previous post , I am most pleased to share two of the exciting features shipped with Crosslight: unified navigation framework and powerful data binding.

Unified Navigation Framework

As navigation differs from one mobile framework to another, it’s a challenging task for mobile developers to streamline the navigation logic all in one place that can work consistently between platforms. Take iOS, Android, Windows Phone, and WinRT for example. iOS uses push navigation while Android uses Intent-based navigation while Windows Phone and WinRT use Page-based navigation. Gone are the days you will have to worry about these troubles with Crosslight. This section will outline the features supported by Crosslight navigation framework.

Basic Navigation

Crosslight navigation supports basic navigation to a page using the view model.  This is achieved using the navigation service from the view model and calling the Navigate method then specify the desired view model to navigate. It’s as simple as that.

Basic Navigation on iOS

At its simplest form, basic navigation can simply be done with the following simple calls from the view model:

private void ExecuteNavigateIdentifier(object parameter)
{
    this.NavigationService.Navigate("ViewModel1Derivative");
}

private void ExecuteNavigateParameter(object parameter)
{
    // You can pass any objects as parameter during navigation
    this.NavigationService.Navigate(new NavigationParameter(parameter));
}

private void ExecuteNavigateType(object parameter)
{
    this.NavigationService.Navigate();
}

Note that you can also navigate to the same view model but consumed by different views using the NavigationParameter as shown above.

List Navigation (Push Navigation)

Crosslight navigation service enables you to perform navigation from within the view model. You can navigate to a view model by providing the type, or a known identifier.

Unified List Navigation on WIndows Phone

The navigation view model looks as simple as this:

public class NavigationViewModel : ListViewModelBase
{
    public NavigationViewModel()
    {
        IApplicationContext context = this.GetService().GetContext();

        List items = new List();

        items.Add(new NavigationItem("Simple List", "Data View", typeof(SimpleListViewModel)));
        items.Add(new NavigationItem("Grouped List", "Data View", typeof(GroupListViewModel)));
        items.Add(new NavigationItem("Grouped List (Section)", "Data View", new NavigationTarget(typeof(GroupListViewModel), "GroupStyle")));
        items.Add(new NavigationItem("Grouped List with Index", "Data View", new NavigationTarget(typeof(GroupListViewModel), "GroupIndex")));
        items.Add(new NavigationItem("Searchable List", "Data View", typeof(FilterListViewModel)));
        
	...

        this.SourceItems = items;
        this.RefreshGroupItems();
    }

    public override void RefreshGroupItems()
    {
        if (this.Items != null)
            this.GroupItems = this.Items.GroupBy(o => o.Group).Select(o => new GroupItem(o)).ToList();
    }
}

Modal Navigation

The integrated navigation service supports a special navigation type called as modal navigation. It is particularly useful to present a view that waits for user input; you can return a navigation result indicating whether the user provided the input or cancelled the input, as well as capturing additional data as the result of the modal navigation.

Modal Navigation on Android

To execute a modal navigation, in the view model, we can specify as follows:

private void ExecuteNavigateModal(object parameter)
{
    NavigationParameter navigationParameter = new NavigationParameter()
    {
        NavigationMode = NavigationMode.Modal
    };

    ...

    this.NavigationService.Navigate(
        new NavigationTarget(typeof(ViewModel3), navigationParameter),
        (result) =>
        {
            if (result.Action == NavigationResultAction.Cancel)
                this.MessagePresenter.Show("You cancelled the data input");
            else
                this.MessagePresenter.Show("You entered: " + result.Data.ToString());
        });
}

Notice that after modal navigation is executed, you can handle the result of the modal navigation through action callbacks.

Nested Modal Navigation

In addition to basic modal navigation, the Crosslight navigation service also supports advanced modal navigation that allows you to perform navigation within the modal view context. Called Nested Modal Navigation, this feature is particularly useful when you need to capture numerous data input that are split to multiple views (wizard-like).

Nested Modal Navigation on WIndows Phone

The navigation service manages the navigation stack made during the modal session. When the Close method is called, it automatically discards the entire modal navigation stack, and return to the initiator view.

Nested Modal Navigation Result on Windows Phone

It is quite simple to execute a nested modal navigation:

private void ExecuteNavigateModal(object parameter)
{
    NavigationParameter navigationParameter = new NavigationParameter(new RegistrationData())
    {
        NavigationMode = NavigationMode.Modal,
        EnsureNavigationContext = true
    };

    this.NavigationService.Navigate(
        new NavigationTarget(typeof(NestedStep1ViewModel), navigationParameter),
        (result) =>
        {
            if (result.Action == NavigationResultAction.Cancel)
                this.MessagePresenter.Show("You cancelled the data input");
            else
                this.ShowRegistrationData(result.Data as RegistrationData);
        });
}

Similar to the previous modal navigation, you can handle the result of the nested modal navigation through action callbacks through the nested modal view initiator.

Master-Detail Navigation

The Crosslight navigation service supports master-detail navigation, both when deployed to a phone device or a tablet device. On a phone device, the navigation would be interpreted as push navigation while on a tablet it would be treated differently based on the targeted platform.

On phone, it will look like this:

Master Detail Navigation on Phone

While on tablet:

Master Detail Navigation on Tablet

For iOS and Android, you would need a binding provider that specifies the detail view model when the item is tapped.

public class ListNavigationBindingProvider : BindingProvider
{
    #region Constructors

    public ListNavigationBindingProvider()
    {
        ItemBindingDescription itemBinding = new ItemBindingDescription()
        {
            DisplayMemberPath = "Name",
            DetailMemberPath = "Location",
            ImageMemberPath = "ThumbnailImage"
        };

        this.AddBinding("TableView", BindableProperties.ItemsSourceProperty, "Items");
        this.AddBinding("TableView", BindableProperties.ItemTemplateBindingProperty, itemBinding, true);
        this.AddBinding("TableView", BindableProperties.SelectedItemProperty, "SelectedItem", BindingMode.TwoWay);
        this.AddBinding("TableView", BindableProperties.DetailNavigationTargetProperty, new NavigationTarget(typeof(ItemDetailViewModel)), true);
    }

    #endregion
}

Tab Navigation

The navigation service also supports tab navigation using selected index. By inheriting a special view model called MultiPageViewModelBase, you can perform navigation setting the selected index of the navigation items. You can also send parameters and custom objects during tab navigation.

Tabbed Navigation on iOS

The TabViewModel that enables tab navigation:

public class TabViewModel : MultiPageViewModelBase
{
    #region Constructors

    public TabViewModel()
    {
        var items = new List();
        items.Add(new NavigationItem("Simple Page", typeof(SimpleViewModel)) { Image = "first.png" });
        items.Add(new NavigationItem("About", typeof(AboutNavigationViewModel)) { Image = "second.png" });

        this.Items = items.ToArray();
    }

    #endregion
}

Powerful Data-Binding Features

Crosslight was designed with the powerful MVVM design pattern in mind. The MVVM design pattern itself is almost always associated with data-binding concept. .NET developers targeting Silverlight and WPF will be at home with the concept. However, this post will not cover the concepts of data-binding itself, but more on how Crosslight leverages this concept to the mobile development world, which were only available to platforms such as Silverlight and WPF. If you would like to learn more about data-binding concept, Microsoft has detailed a very good article here that covers the topic in-depth.

Property-to-Property Binding

Crosslight data-binding framework supports the very basic property-to-property data binding, enabling you to bind a property to another property using various information contained in BindingDescription using a BindingProvider class.

public class BindingModeBindingProvider : BindingProvider
{
    public BindingModeBindingProvider()
    {
        this.AddBinding("Label1", BindableProperties.TextProperty, "Text", BindingMode.OneTime);
        this.AddBinding("Label2", BindableProperties.TextProperty, "Text", BindingMode.OneWay);
        this.AddBinding("TextField1", BindableProperties.TextProperty, "Text", BindingMode.TwoWay);
        this.AddBinding("Label3", BindableProperties.TextProperty, "Amount", BindingMode.OneWay);
        this.AddBinding("Slider1", BindableProperties.ValueProperty, "Amount", BindingMode.TwoWay);
        this.AddBinding("TextField2", BindableProperties.TextProperty, "Amount", BindingMode.TwoWay);
        this.AddBinding("Stepper1", BindableProperties.ValueProperty, "Amount", BindingMode.TwoWay);
    }
}

Universal Data Management

Crosslight data binding framework supports automatic UI updates when bound to a collection. This allows for easier data management as well as saving you the trouble of updating the UI when the underlying data model changes. Consider the following view model:

private void ExecuteBatchUpdate(object parameter)
{
    // Begin updating
    this.IsBatchUpdating = true;

    // Perform multiple add and remove simultaneously
    var items = this.Items.ToObservable();
    var updatedItem = items.ElementAt(0);

    updatedItem.Name = "Modified at " + DateTime.Now.ToString("hh:mm:ss");
    this.OnDataChanged(updatedItem);

    items.Insert(1, new Item {Name = "New Item " + this.NewIndex++, Location = "New warehouse", ThumbnailImage = updatedItem.ThumbnailImage});
    items.Insert(3, new Item {Name = "New Item " + this.NewIndex++, Location = "New warehouse", ThumbnailImage = updatedItem.ThumbnailImage});
    items.Insert(6, new Item {Name = "New Item " + this.NewIndex++, Location = "New warehouse", ThumbnailImage = updatedItem.ThumbnailImage});

    items.Remove(items.ElementAt(5));
    items.Remove(items.ElementAt(7));

    this.ToastPresenter.Show("Added 3 items, removed 2 items, updated 1 item", null, null, ToastDisplayDuration.Immediate, ToastGravity.Center);

    // End updating
    this.IsBatchUpdating = false;
}

After you have finished inserting items to the model, the view updates accordingly.

Universal Data Management on iPhone

Binding to Nested Property

Aside from binding to a top-level property, you can also bind to a nested property.

Binding to Nested Property on WinRT

Case 1: Loan Calculator

Loan Calculator

A simple app that can be made using the data-binding framework is the loan calculator where you would input the amount of loan, the loan term in years, and the interest rate per year.

Loan Calculator Result on Android

Each of the textboxes and the button is bound using a BindingProvider.

public class LoanCalculatorBindingProvider : BindingProvider
{
    public LoanCalculatorBindingProvider()
    {
        this.AddBinding("AmountTextField", BindableProperties.TextProperty, "Amount", BindingMode.TwoWay);
        this.AddBinding("LoanTermTextField", BindableProperties.TextProperty, "LoanTerm", BindingMode.TwoWay);
        this.AddBinding("InterestRateTextField", BindableProperties.TextProperty, "InterestRate", BindingMode.TwoWay);
        this.AddBinding("MonthlyPaymentLabel", BindableProperties.TextProperty, new BindingDescription("MonthlyPayment") { StringFormat = "{0:c}" });
        this.AddBinding("CalculateButton", BindableProperties.CommandProperty, "CalculateCommand");
    }
}

After inputting the specified amount, loan term, and interest rate, and hit calculate, it calculates the monthly payment by calling the command specified in the view model, as follows:

private decimal GetCalculateMontlyPayment()
{
    return decimal.Round((this.Amount + (this.Amount * this.InterestRate * (decimal)this.LoanTerm)) / ((decimal)this.LoanTerm * (decimal)12),2);
}

Case 2: Currency Converter

Another simple example would be to make an offline currency converter. This example demonstrates Crosslight support for binding with full converter and UpdateSourceTrigger support.

Currency Converter

Full Converter Support

Just like the native binding found in Silverlight and WPF platforms, Crosslight also supports binding with converter, complete with converter parameter and converter culture.

UpdateSourceTrigger Support

Intersoft Crosslight provides four predefined UpdateSourceTrigger values, namely Default, Explicit, LostFocus, and PropertyChanged. By default, the update process to the bound property will be done after lost focus event is triggered. The following code shows how you can use the two-way BindingMode as well as setting the UpdateSourceTrigger to PropertyChanged.

public class CurrencyConverterBindingProvider : BindingProvider
{
    public CurrencyConverterBindingProvider()
    {
        CurrencyFormatConverter converter = new CurrencyFormatConverter();

        this.AddBinding("BaseCurrencyTextField", BindableProperties.TextProperty, new BindingDescription("USCurrency", BindingMode.TwoWay, UpdateSourceTrigger.PropertyChanged));
        this.AddBinding("BritishCurrencyTextField", BindableProperties.TextProperty, new BindingDescription("UKCurrency") { Converter = converter, ConverterParameter = "{0:c}", ConverterCulture = new CultureInfo("en-gb") });
        this.AddBinding("EuroCurrencyTextField", BindableProperties.TextProperty, new BindingDescription("EuroCurrency") { Converter = converter, ConverterParameter = "{0:c}", ConverterCulture = new CultureInfo("nl-NL") });
        this.AddBinding("SGCurrencyTextField", BindableProperties.TextProperty, new BindingDescription("SGCurrency") { Converter = converter, ConverterParameter = "{0:c}", ConverterCulture = new CultureInfo("zh-sg") });
        this.AddBinding("AUCurrencyTextField", BindableProperties.TextProperty, new BindingDescription("AUCurrency") { Converter = converter, ConverterParameter = "{0:c}", ConverterCulture = new CultureInfo("en-au") });
    }
}

As a result, the respective currencies will be converted in real time when the text is changed.

Currency Converter on iOS

StringFormat Support

Crosslight BindingProvider also supports binding with StringFormat support.

StringFormat Support on Windows Phone

public class StringFormatBindingProvider : BindingProvider
{
    public StringFormatBindingProvider()
    {
        this.AddBinding("Label1", BindableProperties.TextProperty, new BindingDescription("Amount", BindingMode.OneWay) { StringFormat = "{0:c0}" });
        this.AddBinding("Label2", BindableProperties.TextProperty, new BindingDescription("Amount", BindingMode.OneWay) { StringFormat = "{0:###,###.00}" });
        this.AddBinding("Label3", BindableProperties.TextProperty, new BindingDescription("Date", BindingMode.OneWay) { StringFormat = "{0:g}" });
        this.AddBinding("Label4", BindableProperties.TextProperty, new BindingDescription("Date", BindingMode.OneWay) { StringFormat = "{0:F}" });

        this.AddBinding("Slider1", BindableProperties.ValueProperty, "Amount", BindingMode.TwoWay);
        this.AddBinding("DatePicker1", BindableProperties.ValueProperty, "Date", BindingMode.TwoWay);
    }
}

Wrap-up

Navigation and data-binding are just some of the features Crosslight introduced to the mobile development world. It is important to note while all these features are shown platform specific, they work consistently across multiple platforms. The diverse pictures shown in this blog post is to give you an overview about the features described earlier were not platform-specific; they can be applied cross-platform. Some of the Crosslight API outlined in the code snippets are not explained in detail here; they will be covered comprehensively in our product documentation upon release. You might wonder, while these features sound too good to be true, in fact, they really are. These magical features will be available as soon as we hit our 2013 R1 milestone, which is going to be released very soon in early September.

Regards,

Nicholas Lie

New Line-of-Business Samples in ClientUI 7

Introducing the new data controls lineups such as UXPageableComboBox UXMultipleSelectionComboBox and UXTreeList, developers can now build large-scale data entry applications with blazing fast performance. UXGridView also adds with several enhancements and advanced features such as columns collection binding, column header binding and batch items validation. Click here to find out more about the new controls in ClientUI 7.

In this blog post, I will share some of the new samples demonstrating the new products, as well as reviewing the key features.

Below are some of the top new samples that goes to my favorite list.

  1. Cabin Front Reservation

    UXPageableComboBox is an enhanced UXDataComboBox control featuring highly efficient server paging mechanism. The control addresses performance issues in large data scenario by retrieving only the required data on demand, and perform incremental data retrieval when the total item count exceeds the specified page size.

    In this sample, you are able to perform Multiple columns layout with built-in column type such as Text, Images and Templates. You can perform sorting and description text feature using this control. With description text, you can show additional information related to the selected item in the right edge of the control. It is used to allow you easily understand the meaning of the input value at a glance. Explore the sample.

    CabinFront

     

  2. Customer Order
    In this sample, you will see the tight integration between UXGridView and UXPageableComboBox. You can edit a column in UXGridView and bind it with UXPageableComboBox. With this feature, you can fully use all features available in UXPageableComboBox inside UXGridView, such as Multiple columns layout, Auto-complete behavior, advanced data grouping and sorting capability and many more. Explore the sample.

    CustomerOrder

  3. Mail Application
    UXMultipleSelectionComboBox is an advanced queryable ComboBox control specially designed to support multiple selection. It provides an intuitive data entry mode that allows users to quickly select multiple items through type-and-tab gesture. It supports both Editable and Readonly mode. This control has every features that are available in UXPageableComboBox.

    In this sample, editable mode is used. It means that you can directly update the selection through the text input element. The control filters the data instantaneously as you type ahead, and the selection will be added with a single Tab key. Also, the built-in smart filter feature automatically prevents any items that you have selected to appear on the list for the next selection. Explore the sample.

MailApplication

  1. Project Milestone
    UXTreeList is a unique data control used to display self-referencing hierarchical data. You can use the HierarchicalCollectionView to transform your self-reference table into a hierarchical self-reference data.

     
    UXTreeList combines all UXGrid features such as data editing, grouping and paging with UXTreeView’s expand/collapse functionality to perfectly present any hierarchical data.

    In this sample, some features demonstrated are variety of columns type, common data manipulation and data editing that includes drag and drop to re-arrange your data. Explore the sample.

    ProjectMilestone

  2. Chart of Account

    UXTreeList supports load on demand scenario using MVVM pattern where only the root items are loaded initially. The children will be loaded on demand as users expanded the item for the first time. This feature is particularly useful to improve the overall performance when the assigned data source is relatively large.

    In this sample, IsLoadOnDemand property is set to True in order to activate the load-on-demand feature. In this mode, a busy indicator will be automatically displayed in each expanded item during the loading progress. Explore the sample.

    ChartOfAccount

  3. Empty Row Visibility
    UXGridView lets you easily create beautiful data presentation with look and feel similar to popular desktop-based apps such as iTunes, Quick Books, or other business apps alike. With the new innovative rendering feature, UXGridView automatically fills the entire view port with alternating rows instead of leaving a large white empty space.

     
    This sample demonstrates the empty rows visibility feature in UXGridView. When this featuere is enabled the UXGridView will render all the empty rows within the view port. You can also insert a new row from bottom when NewRowPosition is set to Bottom and CanUserAddRows is set to True. Explore the sample.

    EmptyRow

  4. Bottom New Row Position
    This new feature brings flexibility to the way users interact with the grid. Users can now rapidly add new items by clicking the bottom part of the grid, just like they would do in Excel. Activate this feature in your apps with a simple property set.

    In this sample, you can see there are new row position indicator on the last row in UXGridView. To go to the new row you can click the empty rows or use keyboard down on the last item. Explore the sample.

    newrow

  5. Sequence Column
    Introducing the new UXGridViewSequenceColumn, you can now effortlessly display a column that automatically display sequential number to your data grid.

     
    In this sample, we have UXGridViewSequenceColumn which is used to show the row index of the current display. Note that the sequence column can not be sorted and will always show the row index regardless of the sorting or filtering state. Explore the sample.

    seq

For more information about ClientUI, you can explore our Live Samples or read through our Online Documentation.

Feel free to download your copy here. And while waiting for the download, be sure to check out the complete what’s new list here. Existing customers with valid subscription can obtain the latest WebUI Studio from Developer Network, under My Components shortcut.

If you have any questions regarding sales or licensing, you can directly email me at martin@intersoftpt.com. Any comments or feedbacks are welcome.

Thank you and have a nice day.

Regards,
Martin

UXFileUpload: Store Uploaded Files in Database

Handling client files which are uploaded to a server is a common requirement for application. Files can be uploaded either to the FileSystem of your server or directly to the database as BLOB.

However, there has been a lot of debate about the best way to store the files. I’m sure there are many people who have strong favor for why one option is better than the other and vice versa.

So there must be interesting differences between the two solutions. Let’s have a brief comparison between these two options.

FileSystem – Pros and Cons

Storing the files on disk is much easier and simple to implement. Having the files in the filesystem allow accesing it from many different standard applications such FTP, web browser etc. and users as well, independent of access to the database itself etc.

Another advantage is that files on disk are easy to backup; you just copy the files to another location. This also makes it easier to do incremental backups; files that have already been backed up don’t need to be copied again.

Probably the most problematic issue is the loosely coupled nature of the files on disk as they have no strong relation with a record in the database. So, when you delete, say, a customer from the database, you may end up with an orphaned customer’s image/photo. There is no direct way to do a JOIN between the customer table and your images folder to determine what orphaned files you have left.

Nevertheless, to store uploaded files on disk, your web server needs permissions to write to the file system. This is easy to overcome by when you run your own server, but may prove to be more problematic in an ISP scenario.

Database – Pros and Cons

The loosely coupled nature of the files on disk problem will not happen when the uploaded files are stored in database. They have strong relation with a record in the database. It will automatically gets deleted when record of an employee is terminated.

Another advantage of storing files in a database is the fact that all data is contained in a single location. Make a backup of your database and you’re ready to go. No need to copy files, set up permission and so on.

Performance is probably one of the disadvantages of storing files in a database. Storing files in a database somehow degrading the performance as putting binary data into the database has obviously some overhead.

Another cons is that whenever you make a full backup of your database, all the files are included, whether they have been changed or not. If you copy your backups to a different machine or network for safety reasons, you need to move the entire backup file. With a file based solution, you can use diff programs that can determine which files have been changed since the last backup, and only download those.

Realizing that both methods have their pros and their cons, the question is: what to choose? Personally, due to its easier and simpler to be implemented, I’d prefer to store the uploaded files on FileSystem.

However, in this occasion, I will not discuss further on the pros and cons of the file storage matter. I’m going to present the solution on how to store the uploaded files of UXFileUpload ClientUI control to database.

UXFileUpload Introduction

UXFileUpload is a feature-rich file upload control supporting Silverlight 3, Silverlight 4, and WPF. It includes all standard features you expected in a file upload control, plus a multitude of innovative features that unique to UXFileUpload such as multiple upload worker process, comprehensive MVVM and commanding support, smart file chunk algorithm, very large file upload support, file-level cancellation, drag-and-drop files from operating system, and more.

Despite of the large number of features, UXFileUpload is designed for an ultimate ease-of-use. The simplest UXFileUpload control can be defined with only two properties set, the ServiceUrl and the TargetFolder property. You set the ServiceUrl property to a value that determines the absolute web address where the server-side handler is configured to accept the file upload requests. The TargetFolder determines where the files should be stored in the server.

ClientUI includes a built-in ASP.NET server-side handler that you can use to accept the file upload requests from the UXFileUpload control. When using the built-in server-side handler, you can set the TargetFolder to a relative path in your web server, for an instance, ~/Upload.

Creating a Simple UXFileUpload Control

The following code shows the most basic configuration of a UXFileUpload control.

<Intersoft:UXFileUpload ServiceUrl="http://localhost:9041/UXFileUploadHandler.ashx"
                        TargetFolder="~/ClientBin/Assets/Documents" />

You need to register the server-side upload handler in your ASP.NET web project in order for the UXFileUpload control to work properly. For more information configuring the server-side handler for the upload control, see How-to: Configure ASP.NET Server-side Handler for UXFileUpload.

When viewing the control in either design or runtime, you will find the results similar to the illustration in the following:

image

With the upload control shown above, users can start to add files immediately by clicking the Add Files command in the toolbar and click on the Start Upload command to start uploading the selected files.

Once the Start Upload command is invoked, the file upload request will be handled by UXFileUploadHandler.ashx server-side upload handler. The uploaded files will be stored inside the Upload folder of the server.

In order to store the uploaded files to database instead of keep them in file system, we need to prepare following items.

  • Add and configure database which will be used as file storage.
  • Add and configure custom UXFileUpload handler.

Adding and Configuring Database

A database, called Files.mdf, is added into the App_Data folder of the web project. This database has Files table which consist of following fields.

Column Name Data Type Allow Nulls
Id uniqueidentifier False
FileData varbinary(MAX) True
OriginalName nvarchar(50) False
DateCreated datetime False

A Stored Procedure, sprocFilesInsertSingleItem, will be used to insert a single item of file into Files.mdf database.

INSERT INTO Files
(
	Id,
	FileUrl,
	FileData,
	OriginalName
)
VALUES
(
	@id,
	@FileUrl,
	@FileData,
	@originalName
)

Adding and Configuring Custom UXFileUpload Handler

UXFileUpload implements a smart file chunk logic where multiple files can fit into a single upload request thus minimizing the client-server requests. With the smart file chunk logic, UXFileUpload allows you to upload very large files without have to worry about the performance and memory consumption in both client and server side. It is not necessary to change the maximum upload request length or other configuration in the server-side to accommodate the large files upload using the UXFileUpload control.

The built-in UXFileUpload server handler is designed with a great level of customizability, allowing you to inherit the UXFileUploadHandler class and override the properties as necessary. We can also customize the upload post processing logic, by overriding the provided methods.

We will create a custom UXFileUpload handler that will store the uploaded files into the database by overriding the OnUploadCompleted method. Within this method, we can find all the information we need, such as: the number of uploaded files; the file path of uploaded files; saved name of the uploaded files; etc. In OnUploadCompleted we have files that has been completely uploaded. This is important to ensure that there is no file problem such as corrupted files or incomplete files during the saving of the files to the database.

Generally, the custom handler will process each of uploaded files and do the following:

  • Read and manipulate the file using FileStream class.
  • Invoke sprocFilesInsertSingleItem stored procedure to store the file on Files.mdf database.
  • Delete the specified file from the location specified in TargetFolder property of UXFileUpload.

Open the UXFileUpload project and add a class library project, named as ExtendedUXFileUploadHandler.

image

Add the following code into the DatabaseFileUploadHandler.cs file.

public class DatabaseFileUploadHandler : UXFileUploadHandler
{
    protected override void OnUploadCompleted(List<FileUploadInfoResponse> uploadedFiles)
    {
        if (uploadedFiles.Count > 0)
        {
            int FilesNumber = uploadedFiles.Count;

            for (int i = 0; i < FilesNumber; i++)
            {
                byte[] fileData = ReadFile(uploadedFiles[i].TargetFilePath);
                string originalName = uploadedFiles[i].SavedName;

                using (SqlConnection mySqlConnection = new SqlConnection(
                      @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Files.mdf;
                           Integrated Security=True;Connect Timeout=30;User Instance=True")) { // Set up the Command object SqlCommand myCommand = new SqlCommand("sprocFilesInsertSingleItem", mySqlConnection); myCommand.CommandType = CommandType.StoredProcedure; // Set up the ID parameter SqlParameter prmId = new SqlParameter("@id", SqlDbType.UniqueIdentifier); prmId.Value = uploadedFiles[i].ID; myCommand.Parameters.Add(prmId); // Set up the FileData parameter SqlParameter prmFileData = new SqlParameter("@fileData ", SqlDbType.VarBinary); prmFileData.Value = fileData; prmFileData.Size = fileData.Length; myCommand.Parameters.Add(prmFileData); // Set up the OriginalName parameter SqlParameter prmOriginalName = new SqlParameter("@originalName", SqlDbType.NVarChar, 50); prmOriginalName.Value = uploadedFiles[i].SavedName; myCommand.Parameters.Add(prmOriginalName); // Execute the command, and clean up. mySqlConnection.Open(); bool result = myCommand.ExecuteNonQuery() > 0; mySqlConnection.Close(); } File.Delete(uploadedFiles[i].TargetFilePath); } } base.OnUploadCompleted(uploadedFiles); } private static byte[] ReadFile(string filePath) { byte[] buffer; FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); try { int length = (int)fileStream.Length; // get file length buffer = new byte[length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) { sum += count; // sum is a buffer offset for next reading } } finally { fileStream.Close(); } return buffer; } }

The ReadFile function will reads the source file into a byte array. The Read method of FileStream object returns zero only after reaching the end of the stream. Otherwise, Read always reads at least one byte from the stream before returning. Finally, close the current stream and releases any resources associated with the current stream when Read method returns zero.

After completing the read process and set up the required parameter, fileData byte array will then be inserted into Files table by invoking the sprocFilesInsertSingleItem stored procedure.

Finally, delete the uploaded file from the specified path after execute the stored procedure.

Before running the solution, build the project and add the reference of ExtendedUXFileUploadHandler.dll into the web project. Then re-define the handler in Web.Config file into the following.

<configuration>
  ...
  <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <httpHandlers>
        <add verb="*" path="UXFileUploadHandler.ashx"
             type="ExtendedUXFileUploadHandler.DatabaseFileUploadHandler, ExtendedUXFileUploadHandler"/> </httpHandlers> </system.web> <system.webServer> <handlers> <add name="UXFileUpload" verb="*" path="UXFileUploadHandler.ashx"
           type="ExtendedUXFileUploadHandler.DatabaseFileUploadHandler, ExtendedUXFileUploadHandler" /> </handlers> </system.webServer> ... </configuration>

Save all the changes and run the project.

Add files to be uploaded and click Start Upload. After the uploading process completed, the uploaded files is no longer available in the TargetFolder location. The file is now stored in database.

image

image

In this post, we have learnt how to store uploaded files of UXFileUpload to database by overriding the UXFileUpload handler.

Click here to download the sample project and feel free to drop me a line in the comment box if you find that this post useful, or have any questions and feedback regarding this topic.

Regards,
Yudi Ariawan